Note
This is a living document. You are welcome to propose changes to this workflow by opening an issue.
Fortran stdlib Style Guide#
Adopting a consistent style can improve code legibility through the choice of good naming conventions.
In addition, style checks will be run during CI to flag any severe non-conformance.
This allows code review discussions to focus on semantics and substance rather than pedantry.
Consistent whitespace usage, and not polluting line endings with trailing white space makes git diffs considerably more legible.
This style guide is a living document and proposed changes may be adopted after discussing them and coming to a consensus.
Use (modern) standard Fortran#
Do not use obsolescent or deleted language features E.g.,
common,pause,entry, arithmeticifand computedgotoDo not use vendor extensions in the form of non-standard syntax and vendor supplied intrinsic procedures E.g.,
real*8oretime()
File naming conventions#
Source files should contain at most one
program,module, orsubmoduleThe filename should match the program or module name and have the file extension
.f90or.F90if preprocessing is requiredAll included files must use the
.incextension. These files should be located in theinclude/directory.If the interface and implementation is split using submodules the implementation submodule file should have the same name as the interface (parent) module but end in
_implementationE.g.,string_class.f90andstring_class_implementation.f90Tests should be added in the
testsubdirectory and have the same name as the module they are testing with thetest_prefix added E.g.,string_class.f90andtest/test_string_class.f90
Indentation & whitespace#
By setting and following a convention for indentation and whitespace, code reviews and git-diffs can focus on the semantics of the proposed changes rather than style and formatting.
The body of every Fortran construct should be indented by four (4) spaces
Line length should be limited to 80 characters and must not exceed 132
Please do not use Tab characters for indentation
Please remove trailing white space before committing code
Variable and procedure naming#
Variable and procedure names, as well as Fortran keywords, should be written in lowercase
Variable and procedure names should be made up of one or more full words separated by an underscore, for example
has_failedis preferred overhasfailedWhere conventional and appropriate shortening of a word is used then the underscore may be omitted, for example
linspaceis preferred overlin_space
Attributes#
Always specify
intentfor dummy arguments.Don’t use
dimensionattribute to declare arrays because it is more verbose. Use this:real, allocatable :: a(:), b(:,:)
instead of:
real, dimension(:), allocatable :: a
real, dimension(:,:), allocatable :: b
When defining many arrays of the same dimension,
dimensioncan be used as an exception if it makes the code less verbose.If the
optionalattribute is used to declare a dummy argument, it should follow theintentattribute.For module procedures, it is recommended to declare attributes before the module keyword for better retro compatibility (Projects using CMake versions lower than CMake 3.25.0 are concerned see Spurious modules). Prefer the following pattern:
<attribute> <attribute> module <function/subroutine> <name>
instead of:
module <attribute> <attribute> <function/subroutine> <name>
End block closing statements #
Fortran allows certain block constructs or scopes to include the name of the program unit in the end statement.
The convention adopted herein is to include procedure names, module names and program names in the end statement,
unless the closing statement can reasonably be expected to be on the same screen or page, within about 25 lines.
Document public API code for Sphinx#
Documentation strings should be provided for all public and protected entities and their arguments or parameters. The documentation preview is now built with Sphinx using MyST Markdown for the hand-written pages and sphinx-fortran-domain for incremental API auto-documentation from source comments. The current convention is:
Keep proposal/specification documents in
doc/specs/as Markdown pages.Write public API source comments using the existing Fortran doc-comment style (
!>/!!).Prefer Sphinx/MyST-compatible Markdown and reStructuredText constructs in new documentation content.
To write the “spec” (specification) for a new proposal, please place it in the
doc/specs/.