|
1 | 1 | # Fortran stdlib Style Guide
|
2 | 2 |
|
3 |
| -This document will describe the code style to use. |
| 3 | +> [A]utomate and codify as much as you possibly can while remembering that the human touch is still necessary to praise |
| 4 | +> and be kind to your contributors. |
| 5 | +> Let robots handle your project’s pedantry and humans handle your project’s empathy. |
| 6 | +
|
| 7 | +-- @mikemcquaid, [Homebrew] project leader<sup>[1]</sup> |
| 8 | + |
| 9 | +[1]: https://mikemcquaid.com/2018/06/05/robot-pedantry-human-empathy/ |
| 10 | +[Homebrew]: https://brew.sh |
| 11 | + |
| 12 | +## File naming conventions |
| 13 | + |
| 14 | +- Source files should contain at most one `program` or `module`/`submodule`. |
| 15 | +- The filename should match the program or module name and have the file extension `.f90` or `.F90` if preprocessing is required. |
| 16 | +- 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 `_implementation`. E.g., `string_class.f90` and `string_class_implementation.f90` |
| 17 | +- Tests should be added in the `tests` subdirectory and have the same name as the module they are testing with the `test_` prefix added. E.g., `string_class.f90` and `tests/test_string_class.f90` |
| 18 | + |
| 19 | +## Indentation & whitespace |
| 20 | + |
| 21 | +By setting and following a convention for indentation and whitespace, code reviews and git-diffs can |
| 22 | +focus on the semantics of the proposed changes rather than style and formatting. |
| 23 | + |
| 24 | +* __TABs vs spaces__: __Spaces__: TABs should never be used for indentation |
| 25 | +* __Trailing whitespace__: All trailing whitespace must be removed |
| 26 | +* __End of File (EOF)__: The file must end in a single new-line character |
| 27 | +* __Line length__: __132__: The Fortran 90 (and later) standard mandates lines be no longer than 132 characters |
| 28 | +* __Indentation__: __Two (2) spaces__ for all control constructs, line continuations, etc. |
| 29 | +* __Line ending character__: __Native__: Git will convert line endings to `\n` (Unix style) on commit, and will checkout files with native line endings |
| 30 | + |
| 31 | +## Variable naming |
| 32 | + |
| 33 | +Variable names should be descriptive, and not artificially short. |
| 34 | +By default, where it makes sense to do so, variable names shall be made up of one or more full words separated by an underscore. |
| 35 | +For cases where a conventional & appropriate shortening of a word is used then the underscore may be omitted. |
| 36 | + |
| 37 | +Examples: |
| 38 | + |
| 39 | +__GOOD__: |
| 40 | + |
| 41 | +``` fortran |
| 42 | +logical :: has_failed |
| 43 | +real function linspace(...) |
| 44 | +``` |
| 45 | + |
| 46 | +__BAD__: |
| 47 | + |
| 48 | +``` fortran |
| 49 | +logical :: has_failed |
| 50 | +real function lin_space(...) |
| 51 | +``` |
| 52 | + |
| 53 | +## Keyword case |
| 54 | + |
| 55 | +Fortran keywords should __*not*__ be UPPERCASE. |
| 56 | +Modern editors and IDEs can highlight Fortran syntax and UPPERCASE keywords. |
| 57 | +UPPERCASE keywords give Fortran source code the appearance of being antiquated. |
| 58 | + |
| 59 | +## End <scope> block closing statements |
| 60 | + |
| 61 | +Fortran allows certain block constructs or scopes to include the name of the program unit in the end statement. |
| 62 | +The convention adopted herein is to __NOT__ include procedure names, `module` names and `program` names in the `end` statement. |
| 63 | +There should only ever be one `program` and `module` statement per file and therefore including the name provides no useful information. |
| 64 | +An exception to this rule is for procedures (`function`s and `subroutine`s) that are longer than a page of text. |
| 65 | +Long procedures often are a sign of poor abstraction, however, sometimes they are necessary. |
| 66 | +In such cases, the procedure name may be included in the `end` procedure statement. |
0 commit comments