webpage icon indicating copy to clipboard operation
webpage copied to clipboard

Fortran Best Practices -- suggestions

Open Beliavsky opened this issue 4 years ago • 0 comments

In (https://github.com/fortran-lang/fortran-lang.org/issues/313) I made suggestions about grammar and spelling and later made a pull request, which was accepted. I also made suggestions about content, which I will repeat here, since that issue was closed:

For naming conventions, I suggest that short, especially one-letter variable names for integers and reals follow Fortran's implicit typing rules, although one should use implicit none. A variable i should be an integer and x should be a real, for example.

A naming convention I would suggest is to use consistent names for integer variables in a do loop. For example, to loop over days of data,

do iday=1,ndays
   ...
end do

Also, it is common for an integer variable starting with n to denote a loop bound or array dimension and for a variable starting with i to be a loop variable. So one would not write

do nday=1,idays
   ...
end do

At https://fortran-lang.org/learn/best_practices/modules_programs use, only is done for imports, which I agree with, but it should be explained that this reduces name clashes and helps the reader understand where imported entities are coming from.

The code examples use argument intent, but is there a place where that is explicitly suggested and justified?

Why use Roman numerals instead of the usual Arabic digits in https://fortran-lang.org/learn/best_practices/type_casting ?

A naming convention I suggest is that local variables that shadow optional variables have similar names, possibly appending "_". For example,

subroutine sub(a,b)
real, intent(in) :: a
real, intent(in), optional :: b
real :: b_
if (present(b)) then
   b_ = b
else
   b_ = default_value
end if
! remaining code uses b_, not b
end subroutine sub

It may not hurt to state the obvious -- a "best practice" is to use free source form. Free source form code should use the .f90 suffix, not .f95 or .f03 etc., because .f90 stands for free source form, not for compliance to the Fortran 90 standard. I believe @certik supports .f for free source form, but since 99% of .f Fortran code uses fixed source form, I think that causes confusion.

Beliavsky avatar Sep 06 '21 00:09 Beliavsky