Previous: 2.3.1 Debugging the Program
Up: 2.3 Testing the Program
Previous Page: 2.3.1 Debugging the Program
Next Page: 2.4 Input: Reading Data

2.3.2 Documenting the Code

As a programmer, there are several ``good'' habits to develop for translating an algorithm into a source code program which support debugging as well as general understanding of the code. These habits fall under the topic of ``coding for readability''. We have already mentioned a few of these such as commenting the code and good choices of names for variables and functions. With good naming, the syntax of the C language allows for relatively good self documenting code; i.e. C source statements which can be read and understood with little effort.

Well documented code includes additional comments which clarify and amplify the meaning or intention of the statements. A good source for comments in your code are the steps of the algorithm you designed for the program. A well placed comment identifying which statements implement each step of the algorithm makes for easily understood programs.

Another good habit is to include judicious amounts of white space in your program. The C compiler would accept your program all written on one line; however, this would be very difficult for someone to read. Instead, space out your statements, separating groups of statements that perform logically different operations. It is also good to indent the statements in your program so that blocks are clearly identified at a glance. You will notice we have done that in Figure 2.1 and will continue throughout this text. There is no standard for indenting code, so you should choose a convention that is natural for you, as long as it is clear and you are consistent.

One last point: even though we have concentrated on the documentation of the code at the end of our discussion on this program, good documentation should be considered throughout the programming process. A bad habit to get into is to write the code and document it after it is working. A good habit is to include documentation in the code from the beginning.

In this section we have looked in detail at a C program that solves our simplified version of the payroll problem. The program in file pay0.c is not very useful since it can only be used to calculate pay for a specified set of data values because the data values are assigned to variables as constants in the program itself. If we needed to calculate the pay with some other employee, we would have to modify the program with new values and recompile and execute the program. For a program to be useful, it should be flexible enough to use any set of data values. In fact, the user should be able to enter a set of data during program execution, and the program should read and use these data values.



Previous: 2.3.1 Debugging the Program
Up: 2.3 Testing the Program
Previous Page: 2.3.1 Debugging the Program
Next Page: 2.4 Input: Reading Data

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994