From fixed form to free form
| What fixed form was | |
| Convert mechanically | |
| Replace COMMON with modules | |
| Re-test everything |
What fixed form was
Very old Fortran used fixed columns inherited from punch cards: labels at the start, code in a narrow middle area, and sequence numbers at the end.
The tiny block below is a historic fixed form excerpt only. Do not compile it and do not write new code in this style.
! historic fixed form excerpt, do not use
! code started in column 7 in old files
! X = 1 with six leading blanks
Convert mechanically
Modern free form removes column rules. Statements start anywhere, lines run to 132 characters, and the exclamation mark starts a comment.
Rewrite each old line as plain free form with a program unit and implicit none, then let the compiler check the result.
program converted
implicit none
integer :: x
x = 1
x = x + 2
print *, "x =", x
end program converted
Replace COMMON with modules
Old code shared global data through COMMON blocks, which hide dependencies and break type checking across files.
Replace each shared block with one small module that owns the data and offers procedures to read and change it.
module shared_state
implicit none
integer :: counter = 0
contains
subroutine bump()
implicit none
counter = counter + 1
end subroutine bump
end module shared_state
Re-test everything
Migration changes layout and data flow, so every converted unit needs a fresh run with checks on.
Compile with checks, run the old and new versions on the same input, and compare numeric results before deleting the old file.
program check_migration
implicit none
integer :: got
got = 3
if (got .ne. 3) then
print *, "migration mismatch"
error stop 1
end if
print *, "migration ok"
end program check_migration
Next: Numeric models
Article author: Arthur Isaev