Skip to content

Code style conventions

Zalasus edited this page May 14, 2018 · 2 revisions

Code style conventions

Try to be consistent with the style used in the project when making commits. As this project 'grew naturally' initially, you may find some code that does not follow all of these rules. Please don't take this as a warrant to disregard them yourself.

Basic things to consider:

  • Use lower camelCase for identifiers
  • As long as a line fits on your screen, it's fine. Breaking every line so it stays below 80 collumns can make code a pain to read
  • Use fully qualified names (no using namespace)
  • Use the stdint typedefs (uint32_t instead of unsigned int). You may use the global scope typedefs here (no need for std::uint32_t).
  • Getters, setters and other dumb methods may be defined inline in the header
  • Remember RAII and the Rule of three

Indentation

Indent using 4 spaces. Here is an example of how to indent things:

// .h file:
namespace SomeNamespace
{
    class SomeClass
    {
    public:
    
        uint32_t someFunction(float f);
       
    private:
       
        double mSomeValue;
       
    };
}

// .cpp file:
namespace SomeNamespace
{
    uint32_t SomeClass::someFunction(float f)
    {
        return (f + 1)/mSomeValue;
    }
}

Includes

Group your includes into three groups that appear in order:

  • Parent header (If applicable. In a source defining a class Logger (i.e. Logger.cpp), this would be the header declaring Logger, i.e. Logger.h)
  • Library headers (Prefer putting standard headers first, then other libraries)
  • Local headers

Use <...> for all includes outside project scope, "..." for local headers.

Comments and documentation

Write comments for everything that is non-straightforward.

Write Doxygen comments for all classes and all non-trivial member functions.

Clone this wiki locally