This document outlines style guidelines for writing consistent and readable C++ code.
- Prefer readable and consistent naming conventions.
- Avoid deeply nested structures for readability.
- Maintain a consistent indentation style across files.
- Use self-documenting variable and function names.
- Use comments to explain the "why," not the "what."
- Document complex logic with clear comments.
- Avoid redundant comments that duplicate what the code states.
- Limit the length of files and functions.
- Group related functions and classes logically.
- Separate interface (header files) from implementation.
- Use
PascalCasefor class names. - Use
camelCasefor variables and functions. - Prefix member variables with
m_or an equivalent convention.
- Include necessary
#includeguards. - Minimize includes in headers to reduce dependencies.
- Use 4 spaces for indentation.
- Place braces
{}on their own line for classes and methods. - Align
*and&with the variable name:int* ptr; // Recommended
- Variables and functions:
camelCase - Classes:
PascalCase - Constants:
UPPER_SNAKE_CASE
- Use
autofor type inference where appropriate:auto sum = a + b; for (auto& element : container) { // Logic }