Top-down design


Which functions and procedures should I write?

A well written program is broken into small functions and procedurs that are easy to understand. But just which functions and procedures are the right ones for a particular program? It is not always obvious.

There is a way to decide just which functions and procedures you need. Begin writing the main algorithm, whether it be a program or just a sketch. You will encounter problems where you think, "I wish there were a function that did this." Write just a contract for the function that you wish you had. Continue on your main algorithm under the assumption that you actually have the desired functions and procedures. That way, you stay focused on the task at hand.

When the main algorithm is finished, you have unfulfilled obligations, defining the functions and procedures that you thought would be useful. Attack them one at a time. Use the same idea for them. If you encounter a situation where you wish that you had a particular function or procedure, just write a contract for it and keep working on the current algorithm.

Ultimately, you will be able to finish the program, by fulfilling all of the obligations. Some of the functions and procedures will be very simple, and you will not need any others for them.