Your first priority is likely to be that your program actually works properly, but you should also give attention to making your programs readable and easy to understand. This will make it much easier to share your programs with other people (and to get help from them if you have problems). Moreover, you will often need to re-use a program some time after it is written. You can do this more easily if you don't have to spend a lot of time deciphering what you did a few weeks or months ago.
Following are some suggestions to help make your programs more understandable.
The Fortran standard allows up to six characters for variable names. Within this constraint, try to make your variables reflect what they actually mean. For example, if you are comparing yields for various crops you might have variables with names like SOY or MAIZE rather than X or Y .
You can use blank spaces and lines to make your program clearer. The compiler ignores any blank spaces and blank lines (collectively termed "whitespace" by computer geeks). For example, the following three lines of code look the same to the compiler:
PROGRAM MAINPROGRAMMAINPRO GRAMMA INObviously, the first is easier to read. A less trivial example would be a mathematical formula:
A=B-2.*C+4.*A*C-2.*A*B*CAs mentioned earlier, we can also use parentheses to group operations so that a formula is more readable. By combining the judicious use of blank spaces with parentheses, we can make the formula much easier to understand at a glance:
A = B - (2. * C) + (4. * A * C) - (2. * A * B * C)
Comments are used to add notes that describe something about the program. You might insert comments near the beginning of your program that describe the overall purpose of the program, and other comments that explain specific parts of your calculations. Your comments should resemble plain English, though you don't need to use complete sentences and formal paragraphs.
The comments are displayed in your source code, but are essentially ignored by the system. Adding comments does not slow down the execution of your program.
A comment is indicated by the letter C in the first column. Some systems will accept other characters (such as an asterisk) to indicate comments, but this is non-standard and is strongly discouraged.
C This is an example of a comment statementComments are more readable if they stand out from the rest of the program. Some suggestions are:
- Use blank lines before and after the comments to separate them from the surrounding instructions.
- Don't type your comments in all uppercase letters, since the rest of your program is likely to be in uppercase.
- Likewise, start the text of your comments somewhere other than the 7th column, since most lines of your program will probably begin with the 7th column.
Here is an example of a program where attention has been given to making the program easily understandable.
PROGRAM MAIN C This program computes the circumference and C area of a circle for a given radius. REAL PI REAL RADIUS, CIRCUM, AREA PI = 3.1416 RADIUS = 5. CIRCUM = 2. * PI * RADIUS AREA = PI * RADIUS**2 C Calculations are done. Print the results. PRINT *, 'THE RADIUS OF THE CIRCLE IS', RADIUS PRINT *, 'THE CIRCUMFERENCE OF THE CIRCLE IS', CIRCUM PRINT *, 'THE AREA OF THE CIRCLE IS', AREA STOP END
Compare with the following version of the same program, which violates most of the rules for readability of programs:
PROGRAM MAIN C HOMEWRK PGM REALA,B,C,D A2=5. A1=3.1416 A4=2. A3=A4*A1*A2 AA=A1*A2*A2 PRINT*,A2,A3,AA STOP END
Use the suggestions given above to improve the readability of the program you wrote in lesson 6.