A Four-Step Programming Model
◀ Working on Paper First versus Working on Computer▶ Divide and Conquer Amazon
ou are probably not convinced how working on paper makes it easier to write a program, but there are several things you should do before you plunge into coding. I devised these steps and they have helped me finish every single programming assignment since then. Here are the four steps of developing a program that I adopt:
- Step 1: write a skeleton of the program, including function prototypes
- Step 2: write a summary of variables and refine the skeleton of the program as necessary
- Step 3: determine interrelations, states, and scopes of variables
- Step 4: code, test, and optimize; go back to any previous step as necessary
step says that you need to write the pseudo code of the program so that you know what needs to be done at what point in the program. Before you do that you should think about the program and get some idea first. In this process you should include function prototypes as well.
Don’t forget to think about preconditions and postconditions of each function.
For example if you feel that you keep doing the same thing in the pseudo code it may be a good idea to turn it into a function. Another example is that if you feel you are writing a bunch of text in your pseudo code it’s probably a good idea to break it into several blocks, each turning into a function.
step tells you that you need to identify variables you will be using to store data, including objects of classes and user-defined types and arguments to each function. While doing this, you may want to refine the skeleton you made during step 1.
step suggests that you need to hone the definitions of variables and to draw relations among them. They are usually independent of one another, but in large programs, they can be highly dependent upon each other; modifying one’s value affects others’ results. You need to inspect each variable carefully so that you know if modifying a variable creates desired effects.
On the other hand, you need to be clear on the state of variables at any point while the program is running.
For example, let’s say you are using a string array to store all words of a dictionary. At some point you may need to modify the array in order to acquire some critical information; later you may want to restore it to its previous state.
Programmers can be careless about the state of variables until major bugs hit them. Whenever you intend to manipulate a variable, make sure its contents are what you expect.
Another example is whenever you use a loop, make sure that at the start of each iteration you assign desired values to variables used in the loop. For instance, you may need to assign 0 to a variable at the start of every iteration so that at the end of each iteration you get desired value stored in the variable.
Lastly, you want to give each variable an appropriate scope. Most of the time you should avoid using global variables but in situations where you need a variable that should never change, you can consider using a global constant variable. As you grow experienced, you will see more clearly what variable deserves which scope.
When you are done with step 3, you should be able to visualize how the program works and be confident that your program will eventually be completed.
If not, keep working on this step or return to earlier steps because if you jump to step 4 without having a proper design of the program, chances are that things go wrong at some point and you end up having to perform major changes or fixing major bugs.
To sum up, you need to be clear on the following things:
- interrelations among variables
- state of each variable at any point of the program
- scope of each variable
Of course not every variable deserves such critical attention. As you become more experienced you will know how each variable should be handled. This step is a very, very crucial step in making your program work.
, the final step, is self-explanatory. You begin coding on a computer in your favorite IDE according to the skeleton you have devised. Make sure you include necessary headers.
Along the way, you may want to test the completed part to make sure you are on the right track. A common dangerous thing a software engineer does is write everything out; then debug. This practice usually works with small programs, but trust me, once you get your hands on a larger program, individual unit testing becomes imperative.
In addition, after the program is done, you may want to see if you can apply any optimization to it so that it is shorter, more readable, and more efficient. We will see a couple of examples later.
These four steps of programming are meant to help you become more efficient with programming. They may be blurry to you now but in the following chapters we’ll see exactly how we can apply them to each programming task!
◀ Working on Paper First versus Working on Computer▶ Divide and Conquer