next up previous contents
Next: Formatting the Code Up: Programming Style Previous: Design Issues   Contents


Organization of Source Code

It is almost universally agreed that breaking the program up into several files is good style. An 11,592 line Fortran program, for example, is very inconvenient to work with, for several reasons: first, it can be difficult to locate a particular function1 or statement; second, every recompilation during debugging involves compiling the entire file. Having several small files generally makes it easier to find a particular piece of code, and only source files which have been modified need to be recompiled, greatly enhancing the efficiency of the programmer during the debugging process. For smaller programs, it is recommended that the programmer have one file for each subroutine, giving each file the name of the subroutine (abbreviated filenames may be specified if the function names are too long). For larger programs, it may be helpful to group similar functions together into a single file.

In C programs, we also consider it a good idea to place all the #include statements in a file such as includes.h, which is subsequently included in each relevant C source file. This is helpful because if a new header file needs to be added, it can simply be added to includes.h. Furthermore, if a source file suddenly needs to have access to a global variable or function prototype which is already present in one of the header files, then no changes need to be made; the header file is already included. A downside to this approach is that each header file is included in every source file which includes includes.h, regardless of whether a particular header file is actually needed by that source file; this could potentially lead to longer compile times, but it isn't likely to make a discernable difference, at least in C.2

Along similar lines, it is helpful to define all global variables in one location (in the main program file, or else within globals.c), and they should be declared within another standard location (perhaps globals.h, or common.h).3Similarly, if functions are used in several different source code files, the programmer may wish to place all function prototype declarations in a single header file, with the same name as the program or library, or perhaps called protos.h.


next up previous contents
Next: Formatting the Code Up: Programming Style Previous: Design Issues   Contents
psi 2003-01-07