Personal Code Style Guide

This is my personal C++ code style guide.

General

Example Code

#include <iostream>

namespace foo::bar {
  template<typename T>
  class baz {
    public:
      baz();
      ~baz();

      u32 private_member() const;
      void private_member(u32 new_value) noexcept;

      T public_member;
    private:
      u32 _private_member;
  };

  int main(i32 argc, char** argv) {
    if (argc > 1) {
      return 1;
    }

    baz<f32> my_baz;
    
    std::cout << "Hello, World!" << std::endl;

    return 0;
  }
}
      

Naming Conventions

Formatting

Indentation uses spaces. The size of one tab is 2 spaces.

Braces are placed on the same line as the statement, like

if (expression) {
}

The preffered line limit is between 80 and 100 characters.

Pointers and references shall be declared like T& x

Portable C++ code

Use of C++ STL standard library

The use of C++ STL types is encouraged and preferred. The C++ STL should be preferred over C standard library functions.

Comments

You should write self-documenting code if possible. But comments are allowed.

C++ Style comments (// comment ... ... ... ...) are preffered.

Files

Statements

Semantics

Use plenty of assertions.

Always validate function input.

nullptr

nullptr should be used instead of the C-style NULL macro in all cases.