Style guide should be followed for all C/C++ code developed for WARG.
Spacing
We use 4 spaces instead of tabs here.
Variables
Note: For class
and struct
member conventions, refer to the Data Structures
section.
All variables must follow snake case.
Code Block | ||
---|---|---|
| ||
int you_are_reading_this = 0;
int you_are_reading_this{0};
bool nice_thanks_for_reading = false;
bool nice_thanks_for_reading{false};
double this_is_super_fun = 0.0f; |
As a rule of thumb, all variables should be initialized upon declaration to prevent undefined behaviour.
Only exception to this is static variables in classes, which should have an underscore before the first character:Generally, we want to follow the guide set by BAARS C or MISRA C, although generally our style follows that of the google c++ guide. https://google.github.io/styleguide/cppguide.html. A .clang-format will be uploaded soon.
Table of Contents |
---|
Spacing
We use 4 spaces instead of tabs here.
Hard line limit of 80 characters.
Variables
Note: For class
and struct
member conventions, refer to the Data Structures
section.
Generally, variables should follow snake case.
Code Block | ||
---|---|---|
| ||
// These are static variables for a class staticint you_are_reading_this = 0; int _you_are_reading_this{0}; static bool _nice_thanks_for_reading = false; bool nice_thanks_for_reading{false}; static double _this_is_super_fun = 0.0f; |
File-Scope Global Variables
Should always be static and named like static class member variables:
...
As a rule of thumb, all variables should be initialized upon declaration to prevent undefined behaviour.
Static variables
Static variables should be appended with an underscore
Code Block | ||
---|---|---|
| ||
static int _i_am_a_static_integer = 0oops_{69}; std::string openhd_status_ = "broken like always"; |
Constants
Constants (const
and constexpr
) should be named using camel screaming case, with the first character being a k
: never lead with an underscore
Code Block |
---|
const int kYouAreReadingThisYOU_ARE_READING_THIS{0} constexpr bool kNiceThanksForReadingK_NICE_THANKS_FOR_READING{false}; |
Pointers
The* should be attached to the variable:
...
Code Block | ||
---|---|---|
| ||
class ExampleClass { public: ... protected: ... private: ... }; |
Member Variables
Non-static member variables should use snake case.
Constants should use camel case with a k
as the first character.
...
...
}; |
Member Variables: follow variable naming
Code Block | ||
---|---|---|
| ||
class ExampleClass { public: private: // Member variables should always be private. If you need access to them, provide getters and setters. int hello_there; const double kGeneralKenobiGENERAL_KENOBI; static int _its_over_anakin_; }; // Remember static variables must be redeclared outside of the class declaration int ExampleClass::_its_over_anakin_ {0}; |
Constructors
We prefer to use initializer lists when initializing member variables, however there are cases where we should stick to initializing member variables within the constructor.
...
Code Block | ||
---|---|---|
| ||
struct StructName { ... }; |
If you are making a typedef, append a _t.
Code Block | ||
---|---|---|
| ||
TypeDef struct NewType_t{...} |
Initializing Structs
When statically declaring a struct variable, always initialize upon declaration:
...