Changes between Version 8 and Version 9 of doc/tec/developerrules/palmstandard


Ignore:
Timestamp:
Nov 6, 2018 4:41:59 PM (6 years ago)
Author:
kanani
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • doc/tec/developerrules/palmstandard

    v8 v9  
    11= PALM coding rules =
    2 Why to follow some standards? - Because everyone has her/his own programming style, sort of a dialect, making it difficult for other developers to understand, extend, debug, or optimize the code. So what do we do? We learn and apply the coding standard to make PALM more easily readable for all current and future developers. Let's all work on that together. We are aware that the PALM core doesn't completely comply with the following rules yet, but we are working on that.
    3 \\
     2'''Why to follow some standards?'''\\\\
     3Because everyone has her/his own programming style, sort of a dialect, making it difficult for other developers to understand, extend, debug, or optimize the code. So what do we do? We learn and apply the coding standard to make PALM more easily readable for all current and future developers. Let's all work on that together. We are aware that the PALM core doesn't completely comply with the following rules yet, but we are working on that.
     4\\\\
    45Formulated rules and specifications are based on the specifications given for the ocean dynamics model [add-new-link-to NEMO].
    56Other work that influenced the development of this standard are [add-link Community Climate System Model], [add-link Software Developer's Guide], [add-link Report on Column Physics Standards], and [add-link European Standards For Writing and Documenting Exchangeable FORTRAN 90 Code].
    67
    78\\
    8 == (1) General language & form requirements ==
     9= (1) General hints & requirements =
     10== (1.1) Language ==
    911* FORTRAN-2003 standard (all FORTRAN compilers can handle this, FORTRAN-2008 standard not yet supported by all compilers)
    1012* Use English language only
    1113* Use ASCII characters only
    1214* Use SI units as physical units
    13 * Line length limit: 80 characters (soft) | 120 characters (hard) --> Optimize readability (FORTRAN-2003 standard allows '''max. 132''' characters)
    1415
    1516|| '''AVOID''' || '''USE INSTEAD''' ||
    1617|| COMMON blocks || ... ||
    1718
     19== (1.2) Implementing new features to PALM ==
     20* module structure (template)
     21* available interfaces in the PALM core
     22
    1823\\
    19 == (2) Indenting, spaces & line breaks ==
     24= (2) Documenting & commenting =
     25
     26\\
     27= (3) Naming conventions =
     28
     29\\
     30= (4) Formatting & sorting =
     31Line length limit: '''80''' characters (soft) | '''120''' characters (hard). The absolute limit for compilers is '''132''' characters!
     32
     33== (4.1) Indenting, spaces & line breaks ==
    2034'''__General module/subroutine structure'''
    2135(see Fig. 1)
     
    2539'''Fig. 1''' Indention example with highlighted whitespaces. Click to enlarge.
    2640}}}
    27 
    2841* '''0 whitespace''' in front of pre-processor directives
    2942* '''1 whitespace''' before MODULE, CONTAINS, SUBROUTINE (= first indention level)
    30 * '''+3 whitespaces''' for all following indention levels \\(only exception: ONLY list in USE statements '''+4 whitespaces''')\\
    31 
     43* '''+3 whitespaces''' for all following indention levels \\(only exception: ONLY list in USE statements '''+4 whitespaces''')\\\\
    3244* '''1 whitespace''' between individual strings, and between strings and symbols/numbers
    3345* '''1 whitespace''' after ''','''\\(only exception: '''0 whitespace''' between array dimensions)
    3446* '''1 whitespace''' before '''::''' \\(at minimum, see Sect. [#align Alignment])
    35 * '''2 whitespace''' after ''':''' and '''::'''\\
    36 
    37 * '''1 blank line''' between enclosed blocks of instructions, assignments, clauses, statements, etc.
     47* '''2 whitespace''' after ''':''' and '''::'''\\\\
     48* '''1 blank line''' between enclosed/unrelated blocks of instructions, assignments, clauses, statements, etc.
     49* '''2 blank lines''' in front of each SUBROUTINE within a MODULE
    3850* '''&''' character for line continuation at position 80 (minimum), max at position 120
    3951
     
    5163'''Fig. 2''' Indention & whitespaces in loops. Click to enlarge.
    5264}}}
    53 
    54 * In general: '''1 whitespace''' everywhere and '''3 whitespace''' for indention levels
     65* In general: '''1 whitespace''' everywhere and '''3 whitespace''' for each indention level
    5566* But: '''2 whitespace'''\\
    5667  * between '''DO''' and '''loop index'''
     
    6071
    6172\\\\\\\\\\
    62 == (3) Alignment ==
     73== (4.2) Alignment ==
    6374(see Fig. 3)
    6475{{{
     
    7182* At least block-wise alignment for same type/group of declaration statement
    7283* Alignment of message_string values
    73 * Alignment of expressions between brackets (e.g. in '''IF (  )  THEN''' or in argument list in subroutine calls)
     84* Alignment of expressions between brackets (e.g. in '''IF (  )  THEN''' or in argument list of subroutine calls)
     85* Alignment of same level of mathematical expressions or array indices '''(missing in png)'''
    7486
    7587\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    76 == Alphabetical sorting ==
     88== (4.3) Alphabetical sorting ==
    7789except for CALLs to modules/subroutines
    7890
    7991== Lower/upper case ==
    8092
    81 == Commenting ==
     93== Commenting & documentation ==
     94Documentation consists of putting information both inside and outside the source code.
     95Comments should give a good idea of what the code does and where to look for any special activity.
     96
     97'''Doxygen format'''
     98* Declaration !< Description
     99* see file header
     100* @author etc.
     101* MODULE/SUBROUTINE description !>
     102
     103'''Header section'''
     104* todos, notes, author,....
     105
     106'''Inside the code'''
     107* Examples.png..
     108* Avoid superfluous comments, such as\\ {{{a = a + 1 ! Increment a by one}}}
     109* Example for multiple-line declaration comment
     110!
     111!-- Text starts at same position as indented code to be described
     112
     113'''Outside the code'''
     114
     115= (X) Coding =
     116== (X.1) Variable & parameter declarations ==
     117* clear structure in declaration part (USE, IMPLICIT NONE, declarations, SAVE, PRIVATE, PUBLIC list_of_public_variables)
     118* for long lists form groups
     119* one declaration line per variable
    82120
    83121== Naming conventions ==
     122Use clear, unambiguous naming in '''lower-case letters''', with  individual words separated by underscore.
     123* MODULE/SUBROUTINE: name is constructed (if applicable) as verb followed by object, e.g.\\
     124  {{{land_surface_model}}} or {{{read_restart_data}}}
     125* MODULE/SUBROUTINE files: <filename>.f90 --> MODULE <filename> ... END MODULE <filename> (simplification of Make process)
     126* Functions: names provide information about the value it is expected to return, e.g.\\
     127  {{{solar_zenith_angle( )}}}
     128* Variables & constants: names are readable, memorable and descriptive
     129* LOGICAL (boolean) variables: give names that imply\\
     130  {{{TRUE}}} or {{{FALSE}}}
     131
     132'''PROHIBITED:'''
     133* Names that may clash with the operating system or language intrinsics, e.g.\\
     134{{{read( )}}} or {{{access( )}}}
     135* One-letter variable names (only allowed for basic flow variables like velocities (u, v, w), humidity (q) or turbulent kinetic energy (e), as well as loop or other counters (e.g. i, j, k))
    84136
    85137== Allowed operators ==
     
    88140
    89141== Preprocessor directives ==
     142PALM works with the C Pre-Processor (CPP), available on any UNIX platform, and covered my most FORTRAN compilers.
     143Only few pre-processor directives are used in PALM, and activated by the {{{%cpp_options}}} variable in the .palm.config.<configuration_identifier> file.
    90144
    91 == Declaration statements ==
    92 * for long lists form groups
     145Table or link to other page
     146|| '''flag''' || '''description''' ||
     147|| __parallel || .... ||
     148
     149Use this syntax (starting at first character of a line):
     150{{{#if defined(__parallel)
     151      some code
     152#endif}}}
     153together with the standard logical operators '''!''' (instead of .NOT.), '''||''' (instead of .OR.), '''&&''' (instead of .AND.), e.g.
     154{{{#if ! defined(__parallel) && (__netcdf)}}}
     155 
     156
     157
    93158
    94159== File header ==
     
    98163* Finally, involved authors are included, followed by a description of the purpose and functions of the module. If necessary, TODOs, notes and known bugs can be added. The "!>" indicate doxygen-readable comment lines, the "@" marks doxygen variables.
    99164
    100 == Check compilation ==
    101 * no warning/error message should remain
     165== (X) Code structure ==
     166* one module per file (only exception: modules.f90)
     167* clarify program entities, i.e. use {{{SUBROUTINE <name> ... END SUBROUTINE <name>}}}, same holds for INTERFACE, MODULE, PROGRAM
     168
     169-------------------------
     170== Error messages ==
     171* Use message routine (explain parameters here...)
     172* I/O error conditions via IOSTAT (is this fail-safe for different compilers?)
     173
     174
     175== (X) Final steps ==
     176'''Good practice'''
     177* no warning/error message should remain during compile (also try debug options)
     178
     179'''Clean up'''
     180* PRINT/WRITE statements for debugging
     181* Check that all parameters are used