Im grateful to everyone at No Starch Press, especially Keith Fancher and Alison Law, who edited, shaped, and shepherded the book throughout its production. I must also thank Bill Pollock for his decision to sign me up in the first placeI hope he is as pleased with the result as I am. The folks at No Starch have been unfailingly kind and helpful in their correspondence with me. I hope one day to meet them in person and see to what degree they resemble their cartoon avatars on the company website.
Dan Randall did a wonderful job as technical editor. His numerous suggestions beyond the technical review helped me strengthen the manuscript in many areas.
On the home front, the most important people in my life, Mary Beth and Madeline, provided love, support, and enthusiasmand, crucially, time to write.
Finally, to all the students of programming Ive had over the years: Thank you for letting me be your teacher. The techniques and strategies described in this book were developed through our joint efforts. I hope weve made the journey easier for the next generation of programmers.
Introduction
Do you struggle to write programs, even though you think you understand programming languages? Are you able to read through a chapter in a programming book, nodding your head the whole way, but unable to apply what youve read to your own programs? Are you able to comprehend a program example youve read online, even to the point where you could explain to someone else what each line of the code is doing, yet you feel your brain seize up when faced with a programming task and a blank screen in your text editor?
Youre not alone. I have taught programming for over 15 years, and most of my students would have fit this description at some point in their instruction. We will call the missing skill problem solving , the ability to take a given problem description and write an original program to solve it. Not all programming requires extensive problem solving. If youre just making minor modifications to an existing program, debugging, or adding testing code, the programming may be so mechanical in nature that your creativity is never tested. But all programs require problem solving at some point, and all good programmers can solve problems.
Problem solving is hard. Its true that a few people make it look easythe naturals, the programming worlds equivalent of a gifted athlete, like Michael Jordan. For these select few, high-level ideas are effortlessly translated into source code. To make a Java metaphor, its as if their brains execute Java natively, while the rest of us have to run a virtual machine, interpreting as we go.
Not being a natural isnt fatal to becoming a programmerif it were, the world would have few programmers. Yet Ive seen too many worthy learners struggle too long in frustration. In the worst cases, they give up programming entirely, convinced that they can never be programmers, that the only good programmers are those born with an innate gift.
Why is learning to solve programming problems so hard?
In part, its because problem solving is a different activity from learning programming syntax and therefore uses a different set of mental muscles. Learning programming syntax, reading programs, memorizing elements of an application programming interfacethese are mostly analytical left brain activities. Writing an original program using previously learned tools and skills is a creative right brain activity.
Suppose you need to remove a branch that has fallen into one of the rain gutters on your house, but your ladder isnt quite long enough for you to reach the branch. You head into your garage and look for something, or a combination of things, that will enable you to remove the branch from the gutter. Is there some way to extend the ladder? Is there something you can hold at the top of the ladder to grab or dislodge the branch? Maybe you could just get on the roof from another place and get the branch from above. Thats problem solving, and its a creative activity. Believe it or not, when you design an original program, your mental process is quite similar to that of the person figuring out how to remove the branch from the gutter and quite different from that of a person debugging an existing for
loop.
Most programming books, though, focus their attention on syntax and semantics. Learning the syntax and semantics of a programming language is essential, but its only the first step in learning how to program in that language. In essence, most programming books for beginners teach how to read a program, not how to write one. Books that do focus on writing are often effectively cookbooks in that they teach specific recipes for use in particular situations. Such books can be quite valuable as time savers, but not as a path toward learning to write original code. Think about cookbooks in the original sense. Although great cooks own cookbooks, no one who relies upon cookbooks can be a great cook. A great cook understands ingredients, preparation methods, and cooking methods and knows how they can be combined to make great meals. All a great cook needs to produce a tasty meal is a fully stocked kitchen. In the same way, a great programmer understands language syntax, application frameworks, algorithms, and software engineering principles and knows how they can be combined to make great programs. Give a great programmer a list of specifications, turn him loose with a fully stocked programming environment, and great things will happen.
In general, current programming education doesnt offer much guidance in the area of problem solving. Instead, its assumed that if programmers are given access to all of the tools of programming and requested to write enough programs, eventually they will learn to write such programs and write them well. There is truth in this, but eventually can be a long time. The journey from initiation to enlightenment can be filled with frustration, and too many who start the journey never reach the destination.
Instead of learning by trial and error, you can learn problem solving in a systematic way. Thats what this book is all about. You can learn techniques to organize your thoughts, procedures to discover solutions, and strategies to apply to certain classes of problems. By studying these approaches, you can unlock your creativity. Make no mistake: Programming, and especially problem solving, is a creative activity. Creativity is mysterious, and no one can say exactly how the creative mind functions. Yet, if we can learn music composition, take advice on creative writing, or be shown how to paint, then we can learn to creatively solve programming problems, too. This book isnt going to tell you precisely what to do; its going to help you develop your latent problem-solving abilities so that you will know what you should do. This book is about helping you become the programmer you are meant to be.
My goal is for you and every other reader of this book to learn to systematically approach every programming task and to have the confidence that you will ultimately solve a given problem. When you complete this book, I want you to think like a programmer and to believe that you are a programmer .
About This Book
Having explained the necessity of this book, I need to make a few comments about what this book is and what it is not.