Style guide
This style guide is taken, modified and enhanced from DUNE.
General formatting
- Use 4 spaces indent (no tabs, not 2 spaces).
- Trailing whitespace: source files may not contain trailing whitespace to reduce the amount of noise in diffs and during merges.
- In contrast to the remainder of the coding style guidelines, these code formatting rules are (partially) enforced automatically with a pre-commit hook. Due to the distributed nature of Git, this hook can only check your commits once they arrive in the central repository, so it is important to make your local Git repository check your commits as well. The dunecontrol script will automatically install such a pre-commit hook for you.
C++
Documentation
-
Please document freely what each part of your code should do. Document assumptions.
-
All comments/documentation in English.
-
We proclaim the Doc-Me dogma, which means whatever you do, please document it at least with
//! \todo Please doc me!
-
We use Doxygen to generate documentation from the source code. Detailed instructions can be found here.
int lineOfCode = 1; // Short comment on line of code 1 that will _not_ show in doxygen int lineOfCode = 2; //!< Short comment on line of code 2 that will show in doxygen //! Short comment on the following line (line of code 3) that will show in doxygen int lineOfCode = 3; /*! * Longer comment on line of code 4 * with several lines of length * that will show in doxygen */ int lineOfCode = 4;
-
Files always contain the following documentation header before the headerguard:
/*! * \file * \ingroup GroupName * \brief A short description of the file. */ #ifndef DUMUX_HEADERGUARD_HH #define DUMUX_HEADERGUARD_HH
where
GroupName
is a doxygen module group, click here for an overview of existing groups. -
Each class should be documented using the following style:
/*! * \ingroup GroupName * \brief A short description of the class. * * Optional detailed description of the class * over several lines. * * \tparam T very short description of the template parameter T */ template<class T> class MyClass {};
-
Each free function and class member function should be documented using the following style:
/*! * \brief A short description of the function. * * Optional detailed description of the function * over several lines. * * \tparam paramType Very short description of paramType * \param paramName Very short description of paramName * \return returnName Very short description of returnName */ template<typename paramType> paramType functionName(const paramType& paramName) { ... return returnName }
-
Also document non-obvious function parameters at call site:
localResidual.eval(/*includeBoundaries=*/true);
-
Possible exceptions thrown by a function.
Naming schemes
- Avoid abbreviation, i.e. write
saturation
instead ofs
.
Variables / Functions
-
CamelCase starting with a lower case letter, each new word starting with a capital letter.
-
No underscores (except private data members, see below).
-
Exception: If and only if a single letter that represents an abbreviation or index is followed by a single letter (abbreviation or index), CamelCase is not used.
-
Examples:
-
pw
butpressureW
, because "pressure" is a word. -
srnw
butsReg
, because "Reg" is not an abbreviation consisting of a single letter. -
pcgw
butdTauDPi
, because "Tau" and "Pi" are words and longer than one letter. -
CaCO3
, because we write chemical formulas in their chemically sensible way.
-
-
Private data members end with an underscore.
Typenames / Classes / Aliases / Namespaces
- Same rules as for variables, except the first letter is capital.
Filenames / Folders
-
Lower case letters (filenames, but not foldernames may contain underscores).
-
Header files get the suffix
.hh
, implementation files the suffix.cc
. -
Every header file contains a unique header guard. The name should mimic the folder structure and contain the filename, i.e. for a file
common/myfile.hh
it should be#ifndef DUMUX_COMMON_MYFILE_HH #define DUMUX_COMMON_MYFILE_HH
Macros
- The use of preprocessor macros is strongly discouraged. If you have to use them for whatever reason, please use capital letters only.
Indent
- The default indent is 4 spaces (no tabs, not 2 spaces).