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.
Code Block |
---|
|
int b = (1 + 2 - 3) * 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
There must be a space around all operations (==
, >=
, &&
, etc.)
Code Block |
---|
|
if (1 < 2 || (1 <= 3 && 2 >= 1) || 1 == 1 || 1 > 0) { ... } |
Additionally, if a condition list has more than one condition, any condition containing mathematical operations must be surrounded in brackets:
Code Block |
---|
|
if (1 + 1 > 0) { ... } // Only one condition
if ((1 + 1 > 0) && 4 == 4) { ... } // More than one condition |
Loops
Functions
Naming and Brackets
...
Code Block |
---|
int hewwoThereFwend() {
return 0;
} |
Parameters
Pointers
The *
in pointers must be separated on both sides by one space.
Code Block |
---|
|
void hewwoThereFwend(string * fwendName) { ... } |
NOTE: Moving forward, we will prefer to use reference parameters over pointers when passing by reference. It is a more C++ style of programming.
...
We will prefer to use this C++ feature when passing values by reference from now on. To learn more about reference parameters, you can refer to the following links:
More than one Parameter
Parameters in a list must be separated by commas (obviously lol), but a space must separate a comma and a parameter:
Code Block |
---|
|
void hewwoThereFwendswitchFwendName(string& fwendName, string fwendTwoNamenewFwendName) { ... } |
If the parameter list is very long, creating a vertical list of parameters is acceptable too:. Just ensure each parameter starts in the same column so as to ensure readability.
Code Block |
---|
|
void hewwoThereFwend(string fwendOne,
string fwendOnefwendTwo,
string fwendOnefwendTwee,
string fwendOne, ) |
Mathematics
Mathematical Operations
...
fwendFour,
string fwendFive) { ... } |
All function declarations must have a comment above describing its purpose, parameters, and returned value. Pay notice to the spacing on the *
and the /**
that opens the comment
Code Block |
---|
|
int b = (1 + 2 - 3) * 4/5.0 + 6 % 7;/**
* This function switches the friend's name with a new name
*
* @param fwendName -> reference pointing to the friend's name
* @param newFwendName -> value of the friend's new name
*
* @return none
*/
void switchFwendName(string& fwendName, string newFwendName); |
Data Structures
Structs
Classes
Enums