-
Notifications
You must be signed in to change notification settings - Fork 1
Code style
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.
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)
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 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.
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):
- Public methods
- Public fields
- Protected methods
- Protected fields
- Private methods
- Private fields
In every of those groups members should be placed in the next order:
- Constexpr
- Plain
- Static
Every group in the list above must be separated with one blank line.
class Player {
public:
float Health() const;
void SetHealth(float health);
static Player* Instance();
private:
void thinkSomePrivateThoughts();
private:
float m_Health;
static Player* s_Instance;
}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 must be named with g_.