{

Programming Style and Practice

Use Top-Down Design!

(a)
Keep all functions very short, say at most a 25-line screenfull.

(b)
Most functions, including main(), should contain a number of calls to further functions.

(c)
Variables and functions should have meaningful names. This way one can glance through a program and get a quick overview of what actions are done.

(d)
Make sure that each function does what the name you've given it implies--no more and no less. If you have a function named ClearScreen(), for example, make sure that (i) it does clear the screen, and (ii) it does not do some unrelated task, such as reading in the name of an input file.

Use Meaningful Indenting!

The following is C convention. Use it!

Every compound statement, i.e. a set of statements enclosed by { and } (analogous to begin/end in Pascal) should be indented further to the right than its ``parent'' statement (the latter being one of if, else, for or while). The { and } are arranged in a ``triangular'' form. E.g. for a while loop:

while (...) {
   statement
   statement
   statement
   ...
}

This is used in the same way for for loops and if-then-else:

for (...; ...; ...) {
   statement
   statement
   statement
   ...
}

if (...) {
   statement
   statement
   statement
   ...
}
else {
   statement
   statement
   statement
   ...
}

For example, if I have a rather long while loop, someone who reads my program should be able to easily find the end of the loop, simply by going straight down from the `w' in `while'.

Include Lots of Comments!

Every major variable declaration should have a comment explaining what that variable does. Most functions should have comments at the beginning, explaining what the functions do.

Do These Things from the Beginning--Not Just When You Submit the Program for Grading!

Employ these devices--top-down style, indenting and comments--in all your programs, from the moment you start writing them, not just when you are done writing! You will save yourself a lot of time, both in writing and in debugging. Use of top-down style is especially important. It will help your thinking process tremendously during the time you are writing the program. You may have been told in the past that this is to help other people, i.e. to help other people read your program; that is true, but it is also to help yourself! It will save you time!

Use Windows!

Make full use of X11 windows if you at a workstation console; otherwise use the `screen' program to get windows. And in either case, use emacs, with its nice windowing features; e.g. when editing a program file and adding a call to a given function, you can have one window at the call and another window at the function, so that you can check that the parameters match up correctly.

There Is No Need to Use Any Paper!

If you are using top-down design, comments and windowing properly, you should not need to use any paper:

(i)
During the time you are doing your initial writing of the program, you should not write your program on paper. You can compose your program right there at the terminal, using the editor, not paper. First, enter the declarations of your major global variables, with comments for each one, explaining what the variables are for. Then write your function main(); most of it should consist of calls to other functions.

(ii)
During the time you are debugging your program, do not make a printed copy of your program. With good top-down design, you should not need a hard copy to find where in the program you perform a given task. With the use of windows, you should not need a hard copy so that you can look at two different parts of the program at the same time.



Norm Matloff
Wed Nov 8 17:35:17 PST 1995