Code Formatting Conventions

From OpenMW Wiki
Revision as of 20:29, 28 June 2020 by Ptmikheev (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In the beginning of OpenMW we did not have any code formatting conventions, which resulted in substantial inconsistencies. This page tries to codify the conventions that are most commonly used in the code base. Please stick to them, even if the code in the neighbourhood of what you are working on is not.

Layout & Indention[edit]

This is an example of the layout & indention style we encourage. <syntaxhighlight lang="cpp">

// somefile.hpp
namespace SomeSpace
{
    class SomeClass
    {
        private:
           int mSomeVar;
 
        public:
           float someFunction(float p1, float p2) const;
           int getSomeVar() const { return mSomeVar; }  // Allowed only for one-liner getters and setters.
           void setSomeVar(int v) { mSomeVar = v; }     // All other functions should be implemented in somefile.cpp.
    };
}
// somefile.cpp
namespace SomeSpace
{
    float SomeClass::someFunction(float p1, float p2) const
    {
        return p1 + p2;
    }
}

</syntaxhighlight>

Note that the indention width is 4 spaces. If you use tabs instead of spaces, our build server will reject your code. Mixing spaces and tabs is not good.

Trailing spaces are unwanted, but avoid fixing it in existing code if there are no other changes in the same block of code.

General code formatting[edit]

<syntaxhighlight> if (x > 0)

   doSomething();                              // CORRECT

if (x > 0) {

   doSometing();                               // CORRECT

}

if (someCondition1() && someCondition2()

   && someCondition3() && someCondition4())    // CORRECT

{

   doSomething();

}

if (x > 0) doSomething(); // WRONG!

if (x > 0) {

   doSomething();                             // WRONG!

}


if (someCondition1() && someCondition2()

   && someCondition3() && someCondition4())   // WRONG!
   doSomething();

</syntaxhighlight>

Includes[edit]

An implementation file (cpp) should always start by including its own header.

Further includes should be grouped. Groups must be separated by an empty line.

Standard C and C++ headers[edit]

Example: <syntaxhighlight lang="cpp">

#include <string>

</syntaxhighlight>

C headers (if any) should be listed before the C++ headers.

External libraries headers[edit]

<syntaxhighlight lang="cpp">

#include <osg/Vec3f>

</syntaxhighlight>

If more than one external library is used, each library's headers should be put into a separate group.

Components headers[edit]

<syntaxhighlight lang="cpp">

#include <components/compiler/context.hpp>

</syntaxhighlight>

Apps headers[edit]

<syntaxhighlight lang="cpp">

#include <mwworld/world.hpp>

</syntaxhighlight>

Local headers[edit]

<syntaxhighlight lang="cpp">

#include "world.hpp"

</syntaxhighlight>

Doxygen Comments[edit]

Class definitions and function declarations should have doxygen comments. We aren't very strict about this though (especially for trivial functions).

A class should be documented like this: <syntaxhighlight lang="cpp">

/// \brief short description
///
/// Longer description.
class SomeClass

</syntaxhighlight>

The longer description can be skipped, if there is nothing more to say.

A function should be documented like this: <syntaxhighlight lang="cpp">

/// Description.
void someFunction();

</syntaxhighlight>

Here is a link to the Doxygen Documentation. Please make plenty of use of the listed commands, especially the following:

  • \a
  • \attention
  • \brief
  • \note
  • \p
  • \param
  • \return
  • \todo