MATH7601: Common Programming Errors
(and how to avoid them....to an extent!!) 1. Misspelling variable names / not declaring variables: The error message should say something about IMPLICIT NONE. 2. Not setting the value of a variable before using it: Do not depend on the computer to assign a value to your variables. 3. Confusing single line IF statements with IF ( ) THEN...END IF blocks: A single line IF statement does not require ‘THEN’. For IF ( ) THEN...END IF blocks nothing follows THEN on the first line. Do not put anything after ELSE either, unless you use ELSE IF ( ) THEN. 4. Mis-aligning IF blocks and/or DO loops: Use indentation to make the structure clear. 5. Infinite Loops: Check that the condition in your IF ( ) EXIT line will eventually come true! Use Ctrl-C to exit an infinite loop. 6. Going beyond the bounds of an array: For example, if a variable is declared INTEGER:: monthsize(12) then referring to monthsize(0) or monthsize(21) will exceed the bounds of the array. The resulting error message will be something like ‘segmentation fault’.
Program Organisation
1. ALWAYS write small segments of code (e.g. a subroutine or function) and then test thoroughly to see if (A) it compiles, and (B) it does what you think it should, before proceeding. Nothing is more depressing than many pages of code that simply don’t work. 2. If a calculation is repeated, place it in a subroutine or function, which can be called each time it is needed. This leads to shorter programs that are much easier to follow. 3. A single subroutine or function usually performs a single task. 4. Usually, it is better to avoid READ and WRITE statements in subroutines and functions, unless their sole purpose is to read in some complex input, or format some output. 5. Complex calculations should not usually take place in the main program, its main purpose is usually to organise input and output, and call subroutines and functions to do the hard work!