So far, all the programs you've seen have been pretty simple. The idea of planning any of them formally on paper probably seems like overkill. It's not. Planning your programs, even the small ones, will almost always result in time (and often frustration) saved.
Programming is a lot like construction. So, imagine a contractor building a house for you without a blueprint. Yikes! You're liable to end up with a house that has 12 bathrooms, no windows, and a front door on the second floor. Plus, it will cost you 10 times the estimated price.
Programming is the same way. Without a plan, you'll likely struggle through the process, wasting time. You might even end up with a program that doesn't quite work.
Program planning is so important that there's an entire field of software engineering dedicated to it. But even a beginning programmer can benefit from a few simple planning tools and techniques.
An algorithm is a set of clear, easy-to-follow instructions for accomplishing some task. An algorithm is like a blueprint for your program. It's something you planned out, before programming, to guide you along as you code.
An algorithm isn't just a goal—it's a concrete list of steps to be followed in order. So, for example, "Be a millionaire" is not really an algorithm. It's more like a goal. But a worthy one. So I wrote the Make a Million Dollars algorithm. Here it is:
if you can think of a new and useful product then that's your product otherwise repackage an existing product as your product make an infomercial about your product show the infomercial on TV charge $100 per unit of your product sell 10,000 units of your product
There you go. It's a clear series of finite steps that can be followed to achieve the goal.
Algorithms are generally written in something called pseudocode, and mine is no exception. Pseudocode falls somewhere between English and a programming language. Anyone who understands English can understand my algorithm. But at the same time, my algorithm should feel vaguely like a program. The first four lines resemble an if-else structure, and that's intentional.
Like any outline or plan, your algorithm might not be finished after one draft. Often, algorithms need multiple passes before they can be implemented in code. Stepwise refinement is one process used to rewrite algorithms so that they're ready for implementation. Stepwise refinement is pretty simple. Basically, it means "make it more detailed." By taking each step in an algorithm and breaking it down into a series of simpler steps, the algorithm becomes closer to programming code. In stepwise refinement, you keep breaking down each step until you feel that the entire algorithm could be fairly easily translated into a program. As an example, take a step from the Make a Million Dollars algorithm:
create an infomercial about your product
This might seem like too vague a task. How do you create an infomercial? Using stepwise refinement, the single step can be broken down into several others. So, it becomes the following:
write a script for an infomercial about your product rent a TV studio for a day hire a production crew hire an enthusiastic audience film the infomercial
If you feel that these five steps are clear and achievable, then that part of the algorithm has been thoroughly refined. If you're still unclear about a step, refine it some more. Continue with this process and you will have a complete algorithm and a million dollars.