Skip to content

Code style

A edited this page Dec 6, 2022 · 3 revisions

General code style philosophy

Generally if there is no solid reasons to do otherwise we should prefer readability and clearness of the code over some smartass moves to look more intelligent. Do not optimize prematurely.

Symbol prefixes

There are a set of prefixes suggested:

  • m_ for non-public fields of user defined types.
  • s_ for static variables.
  • g_ for non-static global variables (although they should be avoided at any cost)

Class members

All public members of a class (including methods) must be named in PascalCase. Nonpublic methods of a class should be named in camelCase.

For an example check Example Class.

Getters and setters

Getters must not be prefixed with Get, so a getter for a m_Health should be Health() but not GetHealth(). Setters on other hand must be prefixed with Set, so the setter for the same field must be SetHealth().

For an example check Example Class.

Class structure

Inside a class members should be placed in the order provided below (Note: every point of this list means that a separate access modifier must be used):

  1. Public methods
  2. Public fields
  3. Protected methods
  4. Protected fields
  5. Private methods
  6. Private fields

In every of those groups members should be placed in the next order:

  1. Constexpr
  2. Plain
  3. Static

Every group in the list above must be separated with one blank line.

Example Class

class Player {
public:
  float Health() const;
  void SetHealth(float health);

  static Player* Instance();

private:
  void thinkSomePrivateThoughts();

private:
  float m_Health;

  static Player* s_Instance;
}

Functions

Utility functions that are outside of a class should be named according to C conventions with snake_case (e.g. make_padded_hex()), especially if that function does not show up in a header file.

And it is hard to explain, but sometimes we does not obey this rule. For example CreateApplication(). It feels wrong to name it in snake_case, and I can't necessarily explain why, it's just there is some resistance of my soul when I look on the create_application(). I'll pay $5 anyone, who will be first to come up with a sound description and reasoning why make_padded_hex(), but CreateApplication().

Global Variables

Global variables must be named with g_.

Clone this wiki locally