Skip to content

Code Style

alesbe edited this page Jun 5, 2022 · 2 revisions

Indentation

  • Use 4-space tabs instead of spaces.
// ❌ Use 4 spaces or more
    int fourSpaces;
      int sixSpaces;

// βœ… Use 4 spaces tabs
	int fourSpacesTab;

Variables

  • 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;

Functions

  • 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() in SortController.cpp is 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;
	}
}

Comments

  • 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 @brief for a short description @param for arguments, and @return for the return. If the function is returning a bool you can explain what true or false means 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;

Classes

  • 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 .h file and in the .cpp file, that way will be easier to find
  • Try to organize the functions in blocks in the header file, and document them in .cpp file. You can see SortController.h for 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();
}
Clone this wiki locally