# Checklist to avoid errors

February 2020

Back to index.html.

# Checklist to avoid errors

## Typical errors

A typical error (bug) occurs because of the following reasons.

• Misspelling (or forgetting to declare) variables, procedures, and modules.
• Incorrect types.
• Nonsense assignment.
• Misuse or misspell of punctuations (", ', ;, .).
• Misplaced statements in the program.
• Misuse of procedures and statements.
• Uninitialized variables (i.e., forgetting to put an initial value to the variable)
• Ignoring the error status of statements or procedures.
• Overflow.
• Logical errors.

Forst of all, please read the error message that the compiler shows. Here is some advice to avoid the bugs.

## Basics

• Always use implicit none.
• Make sure that a program unit has the order of statements: 1) the unit name, 2) the use statement, 3) implicit none, 4) variables and interface declarations, 5) body of code, and the end statement.
• Assign an initial value to a variable before using it.
• Do not assign a value to a variable in the declaration statements in a procedure. In functions and subroutines, the assignment makes the variable “saved”, which may create a big problem that you can not figure out quickly.
• You should declare any meaningful constants with parameter. Do not put a magic number directly into the program.
• Some compiler options can detect the above issues on compilation or runtime.

## Coding style and practice

• Write a clear code; put some comments useful for the other people, including the future yourself.
• Use indentation, white spaces, new lines to make the code readable.
• Do not use FORTRAN 77-specific syntax. Use the Modern Fortran methods if available.
• Start from a small code and make sure it works. Defining a procedure keeps the code compact.

## Numerical expressions

• Use proper precision.
• Use real in numerical functions. Wrong: sin(1); Correct: sin(1.0).
• Use double precision in serous numerical computations. Single precision tends to accumulate errors easily.
• Be careful about overflow in integer numbers.
• Keep in mind that a real variable holds a floating-point number, which is just an approximation.
• Be aware of an integer result from integer operations e.g., 2/3 is 1.
• The result of mixing different types is converted to a more precise type (real than integer).

## Arrays

• Be careful to assign an allocated array to another (a=b). It may resize the array unexpectedly. Use a(:) to avoid the automatic resize.
• Care about the lower bound when an array is passed to a procedure. The lower bound is set to 1 by default.

## Program unit

• Put a reasonable name to a variable.
• Use functions and subroutines for structured programming.
• Use a module for easy reuse of the code.
• When using external procedures without a module, always put the interface to the procedures.
• When using internal procedures, check if the procedures have a side-effect i.e., changing the “global” variables or not.

Back to index.html.