Style guide should be followed for all C++ code developed for WARG.
Variables
Static Variables
Pointers
Mathematics
Mathematical Operations
There should always be spaces around all mathematical operators. There should always be spaces between equal signs. All variables must follow lower Camel Case.
Code Block |
---|
|
int youAreReadingThis = 0;
bool niceThanksForReading = false;
double thisIsSuperFun = 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:
Code Block |
---|
|
int// bThese =are (1static +variables 2for -a 3)class
* 4/5.0 + 6 % 7; |
Selection
Structure
There must be a space around the if
and else
keywords, and there must be a space separating the condition list and the opening curlly-bracket. Curly-brackets must be on the same line as the condition list.
Code Block |
---|
if (a == b) {
} else if (b == c) {
} else {
} |
Operations
...
int _youAreReadingThis;
bool _niceThanksForReading;
double _thisIsSuperFun; |
Pointers
The *
should be separated on both sides by a space:
Code Block |
---|
|
int * integerPointer = nullptr; |
As a rule of thumb, all pointers must be initialized on decalration. If you are not assigning it a value, then set it to nullptr
like the example above.
Declaring Variables on the Same Line
Declaring and initializing multiple variables of the same type can be done on the same line. However, the number of variables on the line should not exceed 5:
Code Block |
---|
int a = 0, b = 1, c = 2, d = 3;
int a = 0, b = 1, c = 2, d = 3, e = 4, f = 6; // Not good, six variables on same line |
Structs and Classes
When declaring structs, classes, or any object for that matter, we will prefer to use C++'s implementation of value initialization so all parameters are default initialized (are given their default value).
Code Block |
---|
|
struct A {
...
};
int main() {
A hello {}; // The curly brackets trigger value initialiation
} |
Mathematics
Mathematical Operations
There should always be spaces around all mathematical operators (except division). There should always be spaces between equal signs.
Code Block |
---|
|
int b = (1 + 2 - 3) * randomVariable/5.0 + randomVariableTwo % 7; |
Selection
Conditions
Operations
There must be a space around all operations (==
, >=
, &&
, etc.)
...
Code Block |
---|
|
if (1 + 1 > 0) { ... } // Only one condition
if ((1 + 1 > 0) && 4 == 4) { ... } // More than one condition |
Order of Terms
When comparing the value of a variable, it is best practice to put the value before the variable identifier. This prevents runtime errors from occuring in case you use =
instead of ==
.
Code Block |
---|
if (1 == a) { ... } |
If Statements
Structure
There must be a space around the if
and else
keywords, and there must be a space separating the condition list and the opening curlly-bracket. Curly-brackets must be on the same line as the condition list.
Code Block |
---|
if (a == b) {
} else if (b == c) {
} else {
} |
Always use curly brackets, even if the code within the if statement is one line.
Ternary Expressions
Pretty standard.
Code Block |
---|
bool b = true;
int a = b ? 1 : 0; |
Switch Statements
Switch statements are pretty standard too, just need to get the indentation right. Also, ensure you put break
s in every condition unless you cannot put it:
Code Block |
---|
|
int a = 1;
switch(a) {
case 1: // Indent the cases
...
break;
// Keep a space between the end of one case and the condition of the next
case 2:
case 3:
...
break;
default: // Always include a default case
break;
} |
Loops
Functions
Naming and Brackets
...