Next: Separate Files Up: Some Notes on Previous: Introduction

Modular Programming

Modular programming is subdividing your program into separate subprograms such as functions and subroutines. Use these. For example, if your program needs initial and boundary conditions, use subroutines to set them. Then if someone else wants to compute a different solution using your program, only these subroutines need to be changed. This is a lot easier than having to read through a program line by line, trying to figure out what each line is supposed to do and whether it needs to be changed. And in ten years from now, you yourself will probably no longer remember how the program worked.

Subprograms make your actual program shorter, hence easier to read and understand. Further, the arguments show exactly what information a subprogram is using. That makes it easier to figure out whether it needs to be changed when you are modifying your program. Forgetting to change all occurrences of a variable is a very common source of errors.

Subprograms make it simpler to figure out how the program operates. If the boundary conditions are implemented using a subroutine, your program can be searched for this subroutine to find all places where the boundary conditions are used. This might include some unexpected places, such as in the output, or in performing a numerical check on the overall accuracy of the program.

Subprograms reduce the likelyhood of bugs. Because subprograms can use local variables, there is less change that the code in the subroutine interferes with that of the program itself, or with that in other subprograms. The smaller size of the individual modules also makes it easier to understand the global effects of changing a variable.

Perform your output using a subprogram. You tend to become more careless when programming a `noncritical' program part such as output. Also, output often requires additional variables not of interest to the rest of the program. The code for output tends to be lengthy, hence your program will become shorter and easier to read if you move output to a subroutine.

Use the correct type of subprogram. For example, if your program uses an exact solution, cast it into the form of a function subprogram, not a subroutine. Clarity and ease of use improve.

Next: Separate Files Up: Some Notes on Previous: Introduction