Class Organization Rules¶
A well-organized class is easier to read, understand, and maintain. These rules enforce a consistent structure for the members of a class, including access specifiers and the order of different member types.
Access Specifier Order¶
CLASS_ACCESS_SPECIFIER_ORDER
Enforces a standard order for access specifiers within a class definition: `public` members should come first, followed by `protected`, and then `private`. This convention prioritizes the public API. The Class functions should still be documented according to the `[Documentation rules](./documentation.md)`
Examples:
✅ Correct
❌ Incorrect (Lint Error)
class MyWidget {
public:
// Public API is declared first for clarity.
MyWidget();
void Update();
protected:
// Protected members for derived classes.
void OnUpdate();
private:
// Private implementation details are last.
void Recalculate();
int value_;
};
class MyWidget {
private:
// Private members are declared first, hiding the public API.
void Recalculate();
int value_;
public:
MyWidget();
void Update();
protected:
// Order is inconsistent.
void OnUpdate();
};
Member Organization¶
CLASS_MEMBER_ORGANIZATION
Enforces a logical ordering of class members within each access section: types (like `using` or `enum`), constructors/destructor, public methods, and finally member variables.
Examples:
✅ Correct
❌ Incorrect (Lint Error)
class DataManager {
public:
// 1. Type aliases and enums (When Used)
using DataMap = std::map<std::string, int>;
// 2. Constructors and destructor
DataManager();
~DataManager();
// 3. Public methods
void LoadData();
const DataMap& GetData() const;
private:
// 4. Member variables
DataMap data_;
bool is_loaded_;
};
class DataManager {
public:
// Member variables mixed with methods
DataMap data_;
// Constructor after methods
void LoadData();
DataManager();
// Type alias at the end
using DataMap = std::map<std::string, int>;
};
Summary¶
A consistent class structure makes your code predictable and easy to navigate:
API First: Always put the
publicinterface at the top.Logical Grouping: Order members by type, constructors, methods, and variables.
This organization helps developers quickly understand a class’s purpose and how to use it.