Style guide
| implicit none always | |
| Intent everything | |
| Small modules | |
| Name things plainly |
implicit none always
Start every program unit with implicit none so all variables need explicit declarations.
Misspelled names then fail at compile time instead of silently becoming new variables.
program tidy
implicit none
integer :: total
total = 3
print *, "total =", total
end program tidy
Intent everything
Give every dummy argument an intent: in for inputs and out for results and inout for updated values.
Clear intent documents data flow and lets the compiler catch accidental writes to inputs.
subroutine add_tax(price, rate, total)
implicit none
real, intent(in) :: price
real, intent(in) :: rate
real, intent(out) :: total
total = price + price * rate
end subroutine add_tax
Small modules
Keep modules small and focused on one topic. One data type plus its helpers is a good size.
Small units are easy to test, easy to reuse, and easy to explain to the next reader.
module math_helpers
implicit none
contains
function double_it(x) result(y)
implicit none
real, intent(in) :: x
real :: y
y = 2.0 * x
end function double_it
end module math_helpers
Name things plainly
Choose short plain names: count for a counter and total for a sum and rate for a ratio.
Avoid clever abbreviations. The reader should guess the meaning without opening another file.
program plain_names
implicit none
integer :: count
real :: total
count = 4
total = real(count) * 1.5
print *, "count =", count, "total =", total
end program plain_names
Next: Hero roadmap: the whole course on one page
Article author: Arthur Isaev