Be consistent: Be consistent with the code styling you already see in the code!
Implicit none: All modules and subroutines must contain implicit none. This forces all variables to be explicitly declared, hence there is no ambiguity regarding their type.
Fortran 90/95 syntax: Use Fortran 90/95 syntax for all new code. For example, use do--end do, >=, ==, etc. Do not use goto statements; use loop control statements such as cycle and exit instead.
Modules: If you are adding many related subroutines, consider grouping them into a Module. However, try to avoid making modules too big.
Variable Intent: Use intent(in) and intent(out) in the declaration of subroutine input and output variables, respectively. Use intent(inout) if a variable is used as both an input and output. These declarations help catch inadvertent attempts to overwrite input variables. Be careful with intent(out) when using structures; the value of a field which has already been set in the structure may be wiped out when passed in as an out variable. Use inout when you need to keep some of the values in the structure.
Error Handling: Whenever program execution must be stopped due to a failed test, an error statement should be written to the standard output, i.e., write (*,*). This statement must contain:
which function found the error, and;
what test failed.
#include: Do not use #include statements. The variables used by a subroutine should be passed in as arguments. Global variables should be used only as a last resort. If the number of arguments becomes large, consider the need to introduce a structure.
Local Variables: Avoid declaring “large” local variables, e.g., arrays with dimensions on the order of the mesh size. Try to use variables that have already been defined.
Code Lifetime: Do not assume that you are the only one that will use a subroutine that you have coded; keep the code consistent. Moreover, if you find a section of code that is unclear and uncommented, add somecomments so the next person will not have to spend as long figuring out the code.
Comments: Add comments freely, but be concise. Explain what every block of code is doing, but not necessarily every line. Describe the purpose of a subroutine immediately after its interface. Explain what each input variable holds, and what each output variable should hold upon return. Put your initials at sections of the code that you have added to the original subroutine. Issue a warning if the code has not been tested for certain cases. Try to follow the template on page 12.
Naming: Use subroutine and variable names that describe what the subroutine does and the variable holds.
Maximum Column Width: Although Fortran 90/95 lines may extend to 132 characters, try to restrict line widths to 72 characters (Fortran 77 standard). The narrower code is more readable and fits on printed pages better (no wrapped text).
Indentation: Use the indentation shown in the template as a guideline. The indentation for the template can be obtained in emacs by adding the lines below to your .emacs file (this file is located in your home directory).
;; f90 stuff
(setq f90-beginning-ampersand nil ; no "&" at *start* of continuations
f90-indented-comment-re "!--" ; "!--" indented to code
f90-comment-region "! " ; string to comment regions
f90-program-indent 6 ; PROG, MOD, SUB, FUNC
f90-type-indent 3 ; TYPE, INTERFACE, BLOCK DATA
f90-do-indent 3 ; DO
f90-if-indent 3 ; IF, SELECT CASE, WHERE, FORALL
f90-continuation-indent 5
f90-break-before-delimiters nil)