Style guide should be followed for all C++ code developed for WARG.
Can I propose a few changes (following what Gordon Fountain (Deactivated) said).
Variables
All variables should be initialized upon declaration. Most variables follow snake_case.
int you_are_reading_this = 0; int you_are_reading_things{0}; \\ what about like this bool nice_thanks_for_reading = false; bool nice_thank_you_for_reading{false}; \\ and like this double this_is_super_fun = 0.0f;
Static variables in classes are prelined by an underscore before the first character.
int _this_is_a_static_variable = 0; bool _do_you_understand = false; double _ok_keep_reading = 0.0f;
Variables
All variables must follow lower Camel Case.
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:
// These are static variables for a class int _youAreReadingThis; bool _niceThanksForReading; double _thisIsSuperFun;
Pointers
The *
should be separated on both sides by a space:
int * integerPointer = nullptr;
As a rule of thumb, all pointers must be initialized on declaration. 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:
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).
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.
int b = (1 + 2 - 3) * randomVariable/5.0 + randomVariableTwo % 7;
Selection
Conditions
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
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 ==
.
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.
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.
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:
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
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);
Add Comment