- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13
Code Style
        alesbe edited this page Jun 5, 2022 
        ·
        2 revisions
      
    - Use 4-space tabs instead of spaces.
// β Use 4 spaces or more
    int fourSpaces;
      int sixSpaces;
// β
 Use 4 spaces tabs
	int fourSpacesTab;- Use descriptive names using camelCase, don't need to be too verbose, just make sure that the name is understandable. You can use short names for loops.
// β Use wrong names
int n_com;
bool Sorted;
string sort-name;
int value2;
// β
 Correct names
int numOfCompatisons;
bool isSorted;
string sortName;
int tempValue;- 
You can use the same style as variable names. Also, if it's a function in a collection (like the algorithm functions), try to follow a pattern. The functions should be documented (see comments section). 
- 
If you're using multiple functions for the same purpose (for example, a sort function that you decided to divide it into different functions like quickSort), try to keep the functions together in a block. In that case you don't need to document each one if you want, instead document the whole "function block" or the main function.algo::quickSort()inSortController.cppis a good example.
// β Use wrong names or undocumented functions.
bool even(bool k) {
	if(k % 2 == 0) {
		return true;
	} else {
		return false;
	}
}
// β
 Correct names and well documented.
/**
 * @brief Checks if a number is even
 *
 * @param num Number to be checked
 * @return true Is even
 * @return false Is odd
 */
bool isEven(bool num) {
	if(num % 2 == 0) {
		return true;
	} else {
		return false;
	}
}- Use //for single line comments, use/* */for a block comment
- Add a space before the comment
- Start the comments with uppercase
- Comment in the line above, not the same line
- For function documentation, use @brieffor a short description@paramfor arguments, and@returnfor the return. If the function is returning a bool you can explain whattrueorfalsemeans individually.
// β Don't follow guidelines
int n1, n2; //nums
int result;
//this is for
//adding the two numbers
result = n1 + n2;
//result
std::cout << "Result: " << result;
// β
 Follow guidelines
// Operation vars
int n1, n2;
int result;
/*
This will add the two numbers to the result. I'm trying to be really verbose
for this example, but you don't need to comment everything! A good balance
is be the best!
*/
result = n1 + n2;
// Print result
std::cout << "Result: " << result;- Put the includes in the header file
- Start the name using PascalCase (same as camelCase but with the first letter uppercase)
- Declare the private variables and methods with an underscore
- Follow the same function order in the .hfile and in the.cppfile, that way will be easier to find
- Try to organize the functions in blocks in the header file, and document them in .cppfile. You can seeSortController.hfor an example
// β
 Follow guidelines
class Dog {
	private:
		// Attributes
		int _name;
		int _age;
		int _color;
		// Timelapse actions
		int _growOneYear();
		int _growTenYears();
	public:
		// Constructor
		Dog(name, age, color);
		// Getters
		int getAge();
		int getName();
		int getColor();
		// Actions
		void bark();
		void jump();
}