|
| 1 | +# C++ Code Standards |
| 2 | + |
| 3 | +This document outlines the coding standards for the project. Adhering to these standards ensures consistency, readability, and maintainability of the codebase. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Variables |
| 8 | + |
| 9 | +- **Constants**: Must be written in all capital letters. |
| 10 | + ```cpp |
| 11 | + const int MAX_VALUE = 100; |
| 12 | + ``` |
| 13 | + |
| 14 | +- **Function Parameters**: Must start with `p_`. |
| 15 | + ```cpp |
| 16 | + void SetValue(int p_value); |
| 17 | + ``` |
| 18 | + |
| 19 | +- **Member Variables**: Must start with `m_`. |
| 20 | + ```cpp |
| 21 | + int m_count; |
| 22 | + ``` |
| 23 | + |
| 24 | +- **Static Variables**: Must start with `s_`. |
| 25 | + ```cpp |
| 26 | + static int s_instanceCount; |
| 27 | + ``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Functions |
| 32 | + |
| 33 | +- **Private Functions**: Must use camelCasing. |
| 34 | + ```cpp |
| 35 | + void calculateSum(); |
| 36 | + ``` |
| 37 | + |
| 38 | +- **Public Functions**: Must use PascalCasing. |
| 39 | + ```cpp |
| 40 | + void CalculateSum(); |
| 41 | + ``` |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Class Structure |
| 46 | + |
| 47 | +Classes must follow a structured order for member visibility: |
| 48 | + |
| 49 | +1. **Public Members** |
| 50 | + - Public functions and variables must be declared at the top of the class. |
| 51 | + |
| 52 | +2. **Protected Members** |
| 53 | + - Protected functions and variables must be declared in the middle of the class. |
| 54 | + |
| 55 | +3. **Private Members** |
| 56 | + - Private functions and variables must be declared at the bottom of the class. |
| 57 | + |
| 58 | +### Example |
| 59 | + |
| 60 | +```cpp |
| 61 | +class ExampleClass |
| 62 | +{ |
| 63 | +public: |
| 64 | + ExampleClass(); |
| 65 | + void PublicFunction(); |
| 66 | + |
| 67 | +protected: |
| 68 | + void ProtectedFunction(); |
| 69 | + |
| 70 | +private: |
| 71 | + void privateFunction(); |
| 72 | + int m_privateVariable; |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +# RTTI (Run-Time Type Information) |
| 77 | +* RTTI should be avoided by default. |
| 78 | +Prefer compile-time polymorphism (e.g., templates or CRTP) or explicit virtual interfaces. |
| 79 | + |
| 80 | +* Only use dynamic_cast or typeid when absolutely necessary, such as interacting with external APIs or systems that require type introspection. |
| 81 | + |
| 82 | +* Prefer virtual dispatch and explicit interface design to eliminate the need for RTTI. |
| 83 | + |
| 84 | +# Object Ownership and Lifetime |
| 85 | +* Classes must self-contain their data. |
| 86 | + |
| 87 | + * Avoid external dependencies that outlive or manage internal class state. |
| 88 | + |
| 89 | + * A class should not retain raw pointers or references to external objects unless ownership/lifetime is guaranteed. |
| 90 | + |
| 91 | +* Use smart pointers (std::unique_ptr, std::shared_ptr) to manage heap-allocated memory safely. |
| 92 | + |
| 93 | +* Destructors must clean up all resources. |
| 94 | + |
| 95 | + * Avoid memory leaks or dangling resources. |
| 96 | + |
| 97 | + * Follow RAII principles: resource acquisition is initialization. [RAII Principles Documentation](https://en.cppreference.com/w/cpp/language/raii) |
| 98 | + |
| 99 | +* No resurrection: |
| 100 | + |
| 101 | + * Objects should not retain shared state or pointers that assume an instance still exists after it’s deleted. |
| 102 | + |
| 103 | +## Anti-Pattern Example (Do NOT do this): |
| 104 | +```cpp |
| 105 | +// Dangerous: storing a reference that can outlive the object |
| 106 | +class A { |
| 107 | +public: |
| 108 | + void SetExternalRef(B& p_ref) { m_ref = &p_ref; } |
| 109 | +private: |
| 110 | + B* m_ref = nullptr; |
| 111 | +}; |
| 112 | +``` |
| 113 | +
|
| 114 | +
|
| 115 | +## Safe Pattern Example (Do this): |
| 116 | +```cpp |
| 117 | +class B; |
| 118 | +
|
| 119 | +class A { |
| 120 | +public: |
| 121 | + explicit A(std::shared_ptr<B> p_b) : m_b(std::move(p_b)) {} |
| 122 | +private: |
| 123 | + std::shared_ptr<B> m_b; |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +# Final Notes |
| 128 | +* Always prefer clarity and maintainability over cleverness. If a piece of code is hard to understand, it should be refactored. |
| 129 | +* Use comments to explain complex logic, but strive for self-documenting code where possible. |
| 130 | +* Follow the principle of least surprise: code should behave in a way that is intuitive to other developers. |
| 131 | + |
| 132 | +By following these coding standards, we can ensure that our codebase remains clean, maintainable, and easy to understand for all contributors. If you have any questions or suggestions regarding these standards, please feel free to discuss them inside issues. |
0 commit comments