Skip to content
Snippets Groups Projects
codingStyleGuide.org 17.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henry's avatar
    Henry committed
    #                            -*- mode: org; -*-
    #
    
    #+TITLE:                 OpenFOAM C++ Style Guide
    
    Henry's avatar
    Henry committed
    #+AUTHOR:                  OpenFOAM Foundation
    
    #+LINK:                    http://OpenFOAM.com
    
    Henry's avatar
    Henry committed
    #+OPTIONS: author:nil ^:{}
    #+STARTUP: hidestars
    #+STARTUP: odd
    
    
    Henry's avatar
    Henry committed
    *** General
        + 80 character lines max
        + The normal indentation is 4 spaces per logical level.
        + Use spaces for indentation, not tab characters.
        + Avoid trailing whitespace.
    
    Henry's avatar
    Henry committed
        + The body of control statements (eg, =if=, =else=, =while=, /etc./). is
          always delineated with braces.  A possible exception can be
    
    Henry's avatar
    Henry committed
          made in conjunction with =break= or =continue= as part of a control
          structure.
    
    Henry's avatar
    Henry committed
        + The body of =case= statements is usually delineated with braces.
        + Stream output
    
    Henry's avatar
    Henry committed
          + =<<= is always four characters after the start of the stream,
            so that the =<<= symbols align, i.e.
    
    Henry's avatar
    Henry committed
            Info<< ...
            os  << ...
    
    Henry's avatar
    Henry committed
            so
    
    Henry's avatar
    Henry committed
                << "Warning message"
    
    Henry's avatar
    Henry committed
            *not*
    
    Henry's avatar
    Henry committed
            << "Warning message"
    
    Henry's avatar
    Henry committed
    
    
    Henry's avatar
    Henry committed
        + Remove unnecessary class section headers, i.e. remove
    
    Henry's avatar
    Henry committed
    // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
    
        // Check
    
        // Edit
    
        // Write
    
    Henry's avatar
    Henry committed
          if they contain nothing, even if planned for 'future use'
    
    
    Henry's avatar
    Henry committed
        + Class titles should be centred
    
    Henry's avatar
    Henry committed
    /*---------------------------------------------------------------------------*\
                            Class exampleClass Declaration
    \*---------------------------------------------------------------------------*/
    
    Henry's avatar
    Henry committed
          *not*
    
    Henry's avatar
    Henry committed
    /*---------------------------------------------------------------------------*\
                    Class exampleClass Declaration
    \*---------------------------------------------------------------------------*/
    
    Henry's avatar
    Henry committed
    
    
    Henry's avatar
    Henry committed
    *** The /.H/ Header Files
        + Header file spacing
    
    Henry's avatar
    Henry committed
          + Leave two empty lines between sections
    
    Henry's avatar
    Henry committed
            (as per functions in the /.C/ file /etc./)
        + Use =//- Comment= comments in header file to add descriptions to class
    
    Henry's avatar
    Henry committed
          data and functions do be included in the Doxygen documentation:
    
    Henry's avatar
    Henry committed
          + Text on the line starting with =//-= becomes the Doxygen brief
    
    Henry's avatar
    Henry committed
            description;
    
    Henry's avatar
    Henry committed
          + Text on subsequent lines becomes the Doxygen detailed description /e.g./
    
    Henry's avatar
    Henry committed
            //- A function which returns a thing
            //  This is a detailed description of the function
            //  which processes stuff and returns other stuff
            //  depending on things.
            thing function(stuff1, stuff2);
    
    Henry's avatar
    Henry committed
          + List entries start with =-= or =-#= for numbered lists but cannot start
    
    Henry's avatar
    Henry committed
            on the line immediately below the brief description so
    
    Henry's avatar
    Henry committed
            //- Compare triFaces
            //  Returns:
            //  -  0: different
            //  - +1: identical
            //  - -1: same face, but different orientation
    
            static int compare(const triFace&, const triFace&);
    
    Henry's avatar
    Henry committed
            or
    
    Henry's avatar
    Henry committed
            //- Compare triFaces returning 0, +1 or -1
            //
            //  -  0: different
            //  - +1: identical
            //  - -1: same face, but different orientation
    
            static int compare(const triFace&, const triFace&);
    
    Henry's avatar
    Henry committed
            *not*
    
    Henry's avatar
    Henry committed
            //- Compare triFaces returning 0, +1 or -1
            //  -  0: different
            //  - +1: identical
            //  - -1: same face, but different orientation
    
            static int compare(const triFace&, const triFace&);
    
    Henry's avatar
    Henry committed
          + List can be nested for example
    
    Henry's avatar
    Henry committed
            //- Search for \em name
            //  in the following hierarchy:
    
    Henry's avatar
    Henry committed
            //    - ~/.OpenFOAM/\<VERSION\>/
            //      <em>for version-specific files</em>
            //    - ~/.OpenFOAM/
            //      <em>for version-independent files</em>
    
            //    - $WM_PROJECT_DIR/site/\<VERSION\>
    
    Henry's avatar
    Henry committed
            //      <em>for version-specific files</em>
    
            //    - $WM_PROJECT_DIR/site/
    
    Henry's avatar
    Henry committed
            //      <em>for version-independent files</em>
    
    Henry's avatar
    Henry committed
            //    - $WM_PROJECT_DIR/etc/
            //
            //  \return the full path name or fileName() if the name cannot be found
            //  Optionally abort if the file cannot be found
            fileName findEtcFile(const fileName&, bool mandatory=false);
    
    Henry's avatar
    Henry committed
          + For more details see the Doxygen documentation.
        + Destructor
          + When adding a comment to the destructor use =//-= and code as a normal
            function:
    
    Henry's avatar
    Henry committed
            //- Destructor
            ~className();
    
    Henry's avatar
    Henry committed
        + Inline functions
    
    Henry's avatar
    Henry committed
          + Use inline functions where appropriate in a separate /classNameI.H/
            file.  Avoid cluttering the header file with function bodies.
    
    
    *** The /.C/ Source Files
    
    Henry's avatar
    Henry committed
        + Do not open/close namespaces in a /.C/ file
          + Fully scope the function name, i.e.
    
    Henry's avatar
    Henry committed
            Foam::returnType Foam::className::functionName()
    
    Henry's avatar
    Henry committed
            *not*
    
    Henry's avatar
    Henry committed
            namespace Foam
            {
                ...
                returnType className::functionName()
                ...
            }
    
    Henry's avatar
    Henry committed
            *Exception*
    
    Henry's avatar
    Henry committed
            When there are multiple levels of namespace, they may be used in the
    
    Henry's avatar
    Henry committed
            /.C/ file to avoid excessive clutter, i.e.
    
    Henry's avatar
    Henry committed
            namespace Foam
            {
            namespace compressible
            {
            namespace RASModels
            {
                ...
            } // End namespace RASModels
            } // End namespace compressible
            } // End namespace Foam
    
    Henry's avatar
    Henry committed
    
        + Use two empty lines between functions
    
    *** Coding Practice
    
    Henry's avatar
    Henry committed
        + Passing data as arguments or return values:
          + Pass bool, label, scalar and other primitive types as copy,
            anything larger by reference.
        + =const=
    
    Henry's avatar
    Henry committed
          + Use everywhere it is applicable.
    
    Henry's avatar
    Henry committed
        + Variable initialisation using
    
    Henry's avatar
    Henry committed
          const className& variableName = otherClass.data();
    
    Henry's avatar
    Henry committed
          *not*
    
    Henry's avatar
    Henry committed
          const className& variableName(otherClass.data());
    
    Henry's avatar
    Henry committed
        + Virtual functions
    
    Henry's avatar
    Henry committed
          + If a class is virtual, make all derived classes virtual.
    
    *** Conditional Statements
    
    Henry's avatar
    Henry committed
        if (condition)
        {
            code;
        }
    
    Henry's avatar
    Henry committed
        OR
    
    Henry's avatar
    Henry committed
        if
        (
           long condition
        )
        {
            code;
        }
    
    Henry's avatar
    Henry committed
        *not* (no space between =if= and =(= used)
    
    Henry's avatar
    Henry committed
        if(condition)
        {
            code;
        }
    
    Henry's avatar
    Henry committed
    
    *** =for= and =while= Loops
    
    Henry's avatar
    Henry committed
        {
            code;
        }
    
    Henry's avatar
    Henry committed
        OR
    
    Henry's avatar
    Henry committed
        for
        (
            i = 0;
            i < maxI;
    
    Henry's avatar
    Henry committed
        *not* this (no space between =for= and =(= used)
    
        for(i = 0; i < maxI; ++i)
        {
            code;
        }
        #+end_src
        Range-base for should have a space surrounding the ':'
        #+begin_src C++
        for (auto i : range)
    
    Henry's avatar
    Henry committed
        {
            code;
        }
    
        Note that when indexing through iterators, it is often more efficient
        to use the pre-increment form. Eg, =++iter= instead of =iter++=
    
    *** =forAll=, =forAllIters=, =forAllConstIters=, /etc./ loops
    
    Henry's avatar
    Henry committed
        Like =for= loops, but
    
    Henry's avatar
    Henry committed
        forAll(
    
    Henry's avatar
    Henry committed
        *not*
    
    Henry's avatar
    Henry committed
        forAll (
    
        In many cases, the new =forAllIters= and =forAllConstIters= macros
        provide a good means of cycling through iterators (when a range-base
        for doesn't apply). These use the C++11 decltype and work without
        explicitly specifying the container class:
        #+begin_src C++
        forAllIters(myEdgeHash, iter)
        #+end_src
        Using the older =forAllIter= and =forAllConstIter= macros will
        still be seen.  However, since they are macros, they will fail if
        the iterated object contains any commas /e.g./ following will FAIL!:
    
        forAllIter(HashTable<labelPair, edge, Hash<edge>>, myEdgeHash, iter)
    
    Henry's avatar
    Henry committed
        These convenience macros are also generally avoided in other
        container classes and OpenFOAM primitive classes.
    
        In certain cases, the range-based for can also be used.
    
    Henry's avatar
    Henry committed
    
    *** Splitting Over Multiple Lines
    ***** Splitting return type and function name
    
    Henry's avatar
    Henry committed
          + Split initially after the function return type and left align
          + Do not put =const= onto its own line - use a split to keep it with
    
    Henry's avatar
    Henry committed
            the function name and arguments.
    
    Henry's avatar
    Henry committed
            const Foam::longReturnTypeName&
            Foam::longClassName::longFunctionName const
    
    Henry's avatar
    Henry committed
            *not*
    
    Henry's avatar
    Henry committed
            const Foam::longReturnTypeName&
                Foam::longClassName::longFunctionName const
    
    Henry's avatar
    Henry committed
            *nor*
    
    Henry's avatar
    Henry committed
            const Foam::longReturnTypeName& Foam::longClassName::longFunctionName
            const
    
    Henry's avatar
    Henry committed
            *nor*
    
    Henry's avatar
    Henry committed
            const Foam::longReturnTypeName& Foam::longClassName::
            longFunctionName const
    
    Henry's avatar
    Henry committed
          + If it needs to be split again, split at the function name (leaving
    
    Henry's avatar
    Henry committed
            behind the preceding scoping =::=s), and again, left align, i.e.
    
    Henry's avatar
    Henry committed
            const Foam::longReturnTypeName&
            Foam::veryveryveryverylongClassName::
            veryveryveryverylongFunctionName const
    
    Henry's avatar
    Henry committed
    
    ***** Splitting long lines at an "="
         Indent after split
    
    Henry's avatar
    Henry committed
         variableName =
             longClassName.longFunctionName(longArgument);
    
    Henry's avatar
    Henry committed
         OR (where necessary)
    
    Henry's avatar
    Henry committed
         variableName =
             longClassName.longFunctionName
             (
                 longArgument1,
                 longArgument2
             );
    
    Henry's avatar
    Henry committed
         *not*
    
    Henry's avatar
    Henry committed
         variableName =
         longClassName.longFunctionName(longArgument);
    
    Henry's avatar
    Henry committed
         *nor*
    
    Henry's avatar
    Henry committed
         variableName = longClassName.longFunctionName
         (
             longArgument1,
             longArgument2
         );
    
    Henry's avatar
    Henry committed
    
    *** Maths and Logic
    
    Henry's avatar
    Henry committed
        + Operator spacing
    
    Henry's avatar
    Henry committed
          a + b, a - b
          a*b, a/b
          a & b, a ^ b
          a = b, a != b
          a < b, a > b, a >= b, a <= b
          a || b, a && b
    
        + Splitting formulae over several lines:
    
    Henry's avatar
    Henry committed
          Split and indent as per "splitting long lines at an ="
          with the operator on the lower line.  Align operator so that first
          variable, function or bracket on the next line is 4 spaces indented i.e.
    
    Henry's avatar
    Henry committed
          variableName =
              a*(a + b)
             *exp(c/d)
             *(k + t);
    
    Henry's avatar
    Henry committed
          This is sometimes more legible when surrounded by extra parentheses:
    
    Henry's avatar
    Henry committed
          variableName =
          (
              a*(a + b)
             *exp(c/d)
             *(k + t)
          );
    
        + Splitting logical tests over several lines:
    
    Henry's avatar
    Henry committed
          outdent the operator so that the next variable to test is aligned with
          the four space indentation, i.e.
    
    Henry's avatar
    Henry committed
          if
          (
              a == true
           && b == c
          )
    
    * Documentation
    
    Henry's avatar
    Henry committed
    *** General
        + For readability in the comment blocks, certain tags are used that are
          translated by pre-filtering the file before sending it to Doxygen.
        + The tags start in column 1, the contents follow on the next lines and
          indented by 4 spaces. The filter removes the leading 4 spaces from the
          following lines until the next tag that starts in column 1.
        + The 'Class' and 'Description' tags are the most important ones.
        + The first paragraph following the 'Description' will be used for the
          brief description, the remaining paragraphs become the detailed
    
    Henry's avatar
    Henry committed
          description.  For example,
    
    Henry's avatar
    Henry committed
          Class
              Foam::myClass
    
          Description
              A class for specifying the documentation style.
    
              The class is implemented as a set of recommendations that may
              sometimes be useful.
    
    Henry's avatar
    Henry committed
    
        + The class name must be qualified by its namespace, otherwise Doxygen
          will think you are documenting some other class.
        + If you don't have anything to say about the class (at the moment), use
          the namespace-qualified class name for the description. This aids with
          finding these under-documented classes later.
    
    Henry's avatar
    Henry committed
          Class
              Foam::myUnderDocumentedClass
    
          Description
              Foam::myUnderDocumentedClass
    
    Henry's avatar
    Henry committed
        + Use 'Class' and 'Namespace' tags in the header files.
          The Description block then applies to documenting the class.
        + Use 'InClass' and 'InNamespace' in the source files.
          The Description block then applies to documenting the file itself.
    
    Henry's avatar
    Henry committed
          InClass
              Foam::myClass
    
          Description
              Implements the read and writing of files.
    
    Henry's avatar
    Henry committed
    
    *** Doxygen Special Commands
        Doxygen has a large number of special commands with a =\= prefix.
    
        Since the filtering removes the leading spaces within the blocks, the
    
        Doxygen commands can be inserted within the block without problems.
    
    Henry's avatar
    Henry committed
        InClass
            Foam::myClass
    
        Description
            Implements the read and writing of files.
    
            An example input file:
            \verbatim
                patchName
                {
    
    Henry's avatar
    Henry committed
                    refValue    100;
                    value       uniform 1;
                }
            \endverbatim
    
            Within the implementation, a loop over all patches is done:
            \code
    
    Henry's avatar
    Henry committed
                {
                    ...  // some operation
                }
            \endcode
    
    Henry's avatar
    Henry committed
    
    *** HTML Special Commands
        Since Doxygen also handles HTML tags to a certain extent, the angle
        brackets need quoting in the documentation blocks. Non-HTML tags cause
    
    Henry's avatar
    Henry committed
        Doxygen to complain, but seem to work anyhow.  /e.g./,
    
    Henry's avatar
    Henry committed
        + The template with type =<HR>= is a bad example.
        + The template with type =\<HR\>= is a better example.
        + The template with type =<Type>= causes Doxygen to complain about an
          unknown html type, but it seems to work okay anyhow.
    
    *** Documenting Namespaces
    
        + If namespaces are explicitly declared with the =Namespace()= macro,
    
    Henry's avatar
    Henry committed
          they should be documented there.
        + If the namespaces is used to hold sub-models, the namespace can be
          documented in the same file as the class with the model selector.
    
    Henry's avatar
    Henry committed
          /e.g./,
    
    Henry's avatar
    Henry committed
          documented namespace 'Foam::functionEntries' within the
          class 'Foam::functionEntry'
    
    Henry's avatar
    Henry committed
        + If nothing else helps, find some sensible header.
    
    Henry's avatar
    Henry committed
          /e.g./,
    
    Henry's avatar
    Henry committed
          namespace 'Foam' is documented in the foamVersion.H file
    
    Henry's avatar
    Henry committed
    
    *** Documenting Applications
        Any number of classes might be defined by a particular application, but
        these classes will not, however, be available to other parts of
    
    Henry's avatar
    Henry committed
        OpenFOAM. At the moment, the sole purpose for running Doxygen on the
    
    Henry's avatar
    Henry committed
        applications is to extract program usage information for the '-doc'
        option.
    
        The documentation for a particular application is normally contained
        within the first comment block in a /.C/ source file. The solution is this
        to invoke a special filter for the "/applications/{solver,utilities}/"
        directories that only allows the initial comment block for the /.C/ files
        through.
    
        The layout of the application documentation has not yet been finalized,
        but foamToVTK shows an initial attempt.
    
    *** Orthography
    
    Henry's avatar
    Henry committed
        Given the origins of OpenFOAM, the British spellings (/e.g./, neighbour and
        not neighbor) are generally favoured.
    
    Henry's avatar
    Henry committed
    
        Both '-ize' and the '-ise' variant are found in the code comments. If
        used as a variable or class method name, it is probably better to use
        '-ize', which is considered the main form by the Oxford University
    
    Henry's avatar
    Henry committed
        Press. /e.g./,
    
    Henry's avatar
    Henry committed
        myClass.initialize()
    
        #+end_src
    *** References
        References provided in the =Description= section of the class header files
        should be formatted in the [[http://www.apastyle.org][APA (American
        Psychological Association)]] style /e.g./ from =kEpsilon.H=
        #+begin_example
    Description
        Standard k-epsilon turbulence model for incompressible and compressible
        flows including rapid distortion theory (RDT) based compression term.
    
        Reference:
        \verbatim
            Standard model:
                Launder, B. E., & Spalding, D. B. (1972).
                Lectures in mathematical models of turbulence.
    
                Launder, B. E., & Spalding, D. B. (1974).
                The numerical computation of turbulent flows.
                Computer methods in applied mechanics and engineering,
                3(2), 269-289.
    
            For the RDT-based compression term:
                El Tahry, S. H. (1983).
                k-epsilon equation for compressible reciprocating engine flows.
                Journal of Energy, 7(4), 345-353.
        \endverbatim
        #+end_example
        The APA style is a commonly used standard and references are available in
        this format from many sources including
        [[http://www.citationmachine.net/apa/cite-a-book][Citation Machine]] and
        [[http://scholar.google.com][Google Scholar]].