![]() |
Department of
Mathematics and Computer Science |
|
Thus there an emphasis that programs should be modular (that is, make use of short segment calling other segments [i.e., functions, procedures, subroutines]), be well documented (that is, use comments within the code, mnemonic variable names, and labeled output), properly indented be at least minimally efficient (that is, avoid multiple algebraic computations or function calls), and use tested routines (that is, make use of well-known common routines, such as searching or sorting codes).
(See Math 10 Notes N1 (section 16) and Math 10 Notes N3 for additional information about programming style. See Math 10 Notes N7 about avoiding global declaration of variables. See Math 10 Notes N15 about the two styles to use when classes include member functions.
Modularity means that a larger program is written in several smaller segments. As a general rule, each segment should be only about a page long and should usually do only one major thing (e.g., sort, readin, printout, search).
Modularity implies that the main program is often only a series of calls to other program sections, for example:
int main()
{
dataline list[100];
Infile.open("input.dat");
Outfile.open("output.dat");
readin(list[],Infile);
process(list[]);
sort(list[]);
printout(list[],Outfile);
Infile.close();
Outfile.close();
return 0
}
This style of programming makes it clear what the flow of
the program is and can assist the programmer (or an associate) in debugging.
(See Math 61 Notes 1 for comments on "Writing Longer Programs.")
It is also standard practice to include similar information in the output, so that if the output is separated from the program code, it is obvious who the author is and what the output is doing.
Any subprogram can call any other subprogram, but normally this should be avoided if it would be the last step in a subprogram. The reason for this prohibition is simple.
If the main program calls A, and the last step in A is to call B, and the last step of B is to call C, then an analysis of the main program leads to the erroneous conclusion that the main program merely invokes a single routine to do one segment of the program. On the contrary, the actual program flow is that there are three segments that are, in fact, invoked by the main program, A, B, and C, but the invocations of B and C are "hidden" because the less-than-ideal programming style. This practice in a sense amount to a pseudo-recursive type of subprogram calling, in that when the last subprogram completes its action, it must return to the previous one which in turn returns to the previous one, etc., before it returns to the main program.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
Last changed: 24 April 1999.