Code Formatting Conventions: Difference between revisions

From OpenMW Wiki
Jump to navigation Jump to search
(Created page with "==Layout & Indention== This is an example of the layout & indention style we encourage. <div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="mono...")
 
No edit summary
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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.
__TOC__
==Layout &amp; Indention==
==Layout &amp; Indention==


This is an example of the layout &amp; indention style we encourage.
This is an example of the layout &amp; 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.
    };
}


<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
// somefile.cpp
namespace SomeSpace
{
    float SomeClass::someFunction(float p1, float p2) const
    {
        return p1 + p2;
    }
}
</syntaxhighlight>


<span class="kw2">namespace</span> SomeSpace
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.
<span class="br0">{</span>
    <span class="kw2">class</span> SomeClass
    <span class="br0">{</span>
        <span class="kw2">private</span><span class="sy4"><nowiki>:</nowiki></span>
 
            <span class="kw4">int</span> mSomeVar<span class="sy4"><nowiki>;</nowiki></span>
 
        <span class="kw2">public</span><span class="sy4"><nowiki>:</nowiki></span>
 
            <span class="kw4">float</span> someFunction <span class="br0">(</span><span class="kw4">float</span> p1, <span class="kw4">float</span> p2<span class="br0">)</span> <span class="kw4">const</span><span class="sy4"><nowiki>;</nowiki></span>
    <span class="br0">}</span><span class="sy4"><nowiki>;</nowiki></span>
<span class="br0">}</span>


</font></div></div><div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
Trailing spaces are unwanted, but avoid fixing it in existing code if there are no other changes in the same block of code.


<span class="kw2">namespace</span> SomeSpace
==General code formatting==
<span class="br0">{</span>
<syntaxhighlight>
    <span class="kw4">float</span> SomeClass<span class="sy4"><nowiki>::</nowiki></span><span class="me2">someFunction</span> <span class="br0">(</span><span class="kw4">float</span> p1, <span class="kw4">float</span> p2<span class="br0">)</span> <span class="kw4">const</span>
if (x > 0)
    <span class="br0">{</span>
    doSomething();                             // CORRECT
        <span class="kw1">return</span> p1<span class="sy2"> </span>p2<span class="sy4"><nowiki>;</nowiki></span>
    <span class="br0">}</span>
<span class="br0">}</span>


</font></div></div>
if (x > 0)
{
    doSometing();                              // CORRECT
}


Note that the indention width is 4 spaces. We are not too strict about these rules, but if you use tabs instead of spaces, you have a decent chance to piss us off. Mixing spaces and tabs is '''not''' good.
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==
==Includes==
Line 39: Line 71:
Further includes should be grouped. Groups must be separated by an empty line.
Further includes should be grouped. Groups must be separated by an empty line.


===Standard C and C headers===
===Standard C and C++ headers===


Example:
Example:
<syntaxhighlight lang="cpp">
#include <string>
</syntaxhighlight>


<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
C headers (if any) should be listed before the C++ headers.
 
<span class="co2"><nowiki>#include &lt;string&gt;</nowiki></span>
 
</font></div></div>
 
C headers (if any) should be listed before the C headers.


===External libraries headers===
===External libraries headers===
 
<syntaxhighlight lang="cpp">
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
  #include <osg/Vec3f>
 
</syntaxhighlight>
  <span class="co2"><nowiki>#include &lt;OgreVector3.h&gt;</nowiki></span>
 
</font></div></div>


If more than one external library is used, each library's headers should be put into a separate group.
If more than one external library is used, each library's headers should be put into a separate group.
===OpenMW-related library headers===
OpenEngine and mangle.
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
<span class="co2"><nowiki>#include &lt;openengine/ogre/renderer.hpp&gt;</nowiki></span>
</font></div></div>
If both OpenEngine and mangle are present, mangle includes should be put into a separate group listed before the OpenEngine includes.
Note: If you are including from OpenEngine you would instead write:
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
<span class="co2"><nowiki>#include "renderer.hpp"</nowiki></span>
</font></div></div>
or
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
<span class="co2"><nowiki>#include "ogre/renderer.hpp"</nowiki></span>
</font></div></div>
or even
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
<span class="co2"><nowiki>#include "../renderer.hpp"</nowiki></span>
</font></div></div>
depending on the directory, where the including file is located. This rule applies to the following groups too.


===Components headers===
===Components headers===
 
<syntaxhighlight lang="cpp">
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
  #include <components/compiler/context.hpp>
 
</syntaxhighlight>
  <span class="co2"><nowiki>#include &lt;ocomponents/compiler/context.hpp&gt;</nowiki></span>
 
</font></div></div>


===Apps headers===
===Apps headers===
 
<syntaxhighlight lang="cpp">
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
  #include <mwworld/world.hpp>
 
</syntaxhighlight>
  <span class="co2"><nowiki>#include &lt;mwworld/world.hpp&gt;</nowiki></span>
 
</font></div></div>


===Local headers===
===Local headers===
 
<syntaxhighlight lang="cpp">
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
  #include "world.hpp"
 
</syntaxhighlight>
  <span class="co2"><nowiki>#include "world.hpp"</nowiki></span>
 
</font></div></div>


==Doxygen Comments==
==Doxygen Comments==
Line 128: Line 107:


A class should be documented like this:
A class should be documented like this:
 
<syntaxhighlight lang="cpp">
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
  /// \brief short description
 
  ///
  <span class="co1">/// \brief short description</span>
  /// Longer description.
  <span class="co1">///</span>
  class SomeClass
  <span class="co1">/// Longer description.</span>
</syntaxhighlight>
  <span class="kw2">class</span> SomeClass
 
</font></div></div>


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


A function should be documented like this:
A function should be documented like this:
<syntaxhighlight lang="cpp">
/// Description.
void someFunction();
</syntaxhighlight>


<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
Here is a link to the [http://www.stack.nl/~dimitri/doxygen/commands.html Doxygen Documentation]. Please make plenty of use of the listed commands, especially the following:
 
<span class="co1">/// Description.</span>
<span class="kw4">void</span> someFunction<span class="br0">(</span><span class="br0">)</span><span class="sy4"><nowiki>;</nowiki></span>
 
</font></div></div>
 
or this:
 
<div dir="ltr" style="text-align: left"><div class="source-cpp"><font face="monospace">
 
<span class="kw4">void</span> someFunction<span class="br0">(</span><span class="br0">)</span><span class="sy4"><nowiki>;</nowiki></span>
<span class="co1">///&lt; Description.</span>
 
</font></div></div>
 
<br /> Here is a link to the [http://www.stack.nl/~dimitri/doxygen/commands.html Doxygen Documentation]. Please make plenty of use of the listed commands, especially the following:


* \a
* \a
* \arg
* \attention
* \attention
* \brief
* \brief
* \note
* \note
* \p
* \p
* \param
* \return
* \return
* \todo
* \todo

Latest revision as of 19:29, 28 June 2020

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

This is an example of the layout & indention style we encourage.

 // 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;
     }
 }

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

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();

Includes

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

Example:

 #include <string>

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

External libraries headers

 #include <osg/Vec3f>

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

Components headers

 #include <components/compiler/context.hpp>

Apps headers

 #include <mwworld/world.hpp>

Local headers

 #include "world.hpp"

Doxygen Comments

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:

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

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

A function should be documented like this:

 /// Description.
 void someFunction();

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