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.
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.
if (a == b) { } else if (b == c) { } else { }
Operations
There must be a space around all operations (==
, >=
, &&
, etc.)
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:
if (1 + 1 > 0) { ... } // Only one condition if ((1 + 1 > 0) && 4 == 4) { ... } // More than one condition
Loops
Functions
Naming and Brackets
Functions should use lower camel case in their names. There should be no spaces between the function name and the parameter list. The open curly-bracket must be on the same line as the function name, and must be separated from the parameter list by one space:
int hewwoThereFwend() { return 0; }
Parameters
Pointers
The *
in pointers must be separated on both sides by one space.
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.
Reference Parameters
The &
in reference parameters must be appended to the datatype of the parameter.
void hewwoThereFwend(string& fwendName) { ... }
We will prefer to use this C++ feature when passing values by reference. To learn more about reference parameters, you can refer to the following links:
http://www.fredosaurus.com/notes-cpp/functions/refparams.html
https://stackoverflow.com/questions/2564873/how-do-i-use-reference-parameters-in-c
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:
void switchFwendName(string& fwendName, string newFwendName) { ... }
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.
void hewwoThereFwend(string fwendOne, string fwendTwo, string fwendTwee, string fwendFour, string fwendFive) { ... }
Commenting
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
/** * 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);
0 Comments