Style and modern practice

Fortran has sixty years of style in its source code, and most of it is still in use. This lesson takes one small program, writes it the way FORTRAN 77 wrote it and the way a modern project writes it, and then states the rules that separate the two. Nothing in the second version is newer than what a compiler does with the first: the difference is what a reader can see.

The same program, two styles

The task is to sum three numbers. The first version is fixed form: uppercase, one-letter names, a labelled loop, no implicit none and no type on the sum.

The file old.f90:

C A fixed-form program in the style of FORTRAN 77. PROGRAM OLD INTEGER A(3), I, S A(1) = 1 A(2) = 2 A(3) = 3 S = 0 DO 10 I = 1, 3 S = S + A(I) 10 CONTINUE PRINT *, "sum", S END

gfortran -Wall -Wextra -std=legacy -ffixed-form old.f90 -o old
./old

sum 6

The second version is the same algorithm, written for a reader who is going to change it. The names say what the values are, the loop counts to the end of the array instead of to a literal three, and the array is filled in one statement.

The file new.f90:

program sum_array implicit none integer, parameter :: n = 3 integer :: values(n), i, total values = [1, 2, 3] total = 0 do i = 1, n total = total + values(i) end do print *, "sum", total end program sum_array

gfortran -Wall -Wextra -std=f2018 new.f90 -o new
./new

sum 6

The output is identical, byte for byte. Style does not change behaviour; it changes how much of the program a reader has to hold in their head at once. The first version needs three facts from outside the code: that S is an integer because of its letter, that label 10 closes a loop that started eight lines earlier, and that the array has exactly three elements because the loop says so.

Names and layout

Rules worth enforcing

Five rules are not style at all: they decide whether the compiler can check the program. A project that enforces only these has already avoided most of the bugs Fortran is blamed for.

  1. implicit none in every unit. Without it a typo creates a variable, as the lesson on implicit typing shows.
  2. intent(in), intent(out) or intent(inout) on every dummy argument. The compiler then rejects a write to an intent(in) argument, which is a class of bug a C function cannot catch.
  3. private by default in every module, with an explicit public list. A module is an interface, and the interface should be a decision.
  4. use module, only: names instead of use module. A name that arrives by accident is a name that breaks when the module changes.
  5. A parameter instead of a literal that appears twice. integer, parameter :: n = 100 turns three literal hundreds into one name and a compiler that checks the arrays against it.

Features to prefer

The same advice in the other direction: what to write instead of what the 1977 version wrote.

The format checkers

Two tools do for Fortran what gofmt does for Go: fprettify reformats source to a fixed layout, and fortitude reports the rules above as diagnostics. Neither is installed in the container this course was built in, so this page shows no output of theirs; both install from PyPI into a virtual environment, and both are worth a line in a pre-commit hook.

Without them, the rules above are a checklist for a reviewer. The compiler enforces part of the list by itself: -Wall -Wextra reports an unused name and a name used before it is set, intent(in) makes an assignment to an argument an error, and private hides a name that no public list mentions. A missing intent and a missing implicit none are the two the compiler does not report, and they are the two a reviewer looks for first.

Exercises

  1. Rewrite the fixed-form program in free form without changing the algorithm. How many lines does it take?
  2. Add implicit none to the fixed-form program. Which declaration does it demand that was implicit before?
  3. Replace the loop in the modern program with sum(values). What does the program look like now?
  4. Rename every variable in the modern program to a one-letter name and read it aloud. Which line becomes unclear first?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why is implicit none a style rule and a correctness rule at once?

    Answer

    It stops a typo from creating a second variable, and it makes the file state its types

    The line is a declaration of intent for the reader and a check for the compiler.

  2. What does intent(in) on a dummy argument let the compiler check?

    Answer

    That the procedure does not assign to the argument

    A write to an intent(in) argument is a compile error.

  3. Which declaration of a module interface is the modern choice?

    Answer

    private by default, with an explicit public list

    The interface becomes a decision rather than everything that happens to be declared.

  4. What replaces a COMMON block in modern Fortran?

    Answer

    A module with the shared variables

    A module shares typed, checked variables and can restrict who sees them.

  5. What does the modern loop write instead of a labelled DO and CONTINUE?

    Answer

    do ... end do

    The block form has no label and nests unambiguously.

  6. Why prefer an allocatable array to a pointer for owned data?

    Answer

    It is deallocated, copied and checked for you

    Ownership semantics come free with allocatable; a pointer gives none of them.

  7. What does this program print? program p implicit none integer, parameter :: n = 3 integer :: a(n) a = [1, 2, 3] print *, sum(a) end program p

    Answer

    6

    sum is an intrinsic reduction over the whole array.

  8. Why is real*8 a bad declaration?

    Answer

    It is not standard Fortran; a named kind is portable

    The starred form is a vendor extension; real(real64) is the standard way.

  9. What does a comment line earn its place by saying?

    Answer

    Why the code does what it does

    A comment that repeats the code goes stale; a comment that gives the reason does not.

  10. What is the point of use module, only: name?

    Answer

    The program states which names it depends on

    An explicit list keeps accidental names out and makes a dependency visible.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The fixed-form program was built with -std=legacy -ffixed-form.

Article author: Arthur Isaev