CppStyleGuide

Simple style guide for C++ code, could also be applied to other languages such as C or Java.

Table of contents

Naming

Syntax styles:

Upper case: NUMBER
Lower case: home
Upper camel case: HouseOfDaniel
Lower camel case: numberOfHouses

Classes and Structs:

The first letter always in upper case, use upper camel case syntax.

class Human { 
};

Variables:

The first letter always in lower case, use lower camel case syntax.

string name;
float moneyOnBank = 30.6;

Constants:

Source Files:

Classes should be defined in one or two separated files, one for the headers and the other for the methods.
Another class can be implemented on the same file only if they are very related, like nested clases or similar.

Braces & Indentation

Use a tab character equivalent to 4 spaces for indentation.

Use K&R style with opening braces on the same line:

int main() {
	
	bool compiles = true;
	
	if (compiles) {
		beHappy();
	}
	else {
		cry();
	}	
}

void beHappy() {
	cout << "Being happy..." << endl;
}

void beHappy() {
	cout << "Crying..." << endl;
}

Best practices:

Variables:

Let’s take a look at this example:

int johnAge = 10;

Why use “int” in a variable that is not suppose to be bigger than ~150?

Why? Because it uses less memory, 8 bits instead of 32 :)

However, try to avoid maths when using unsigned values:

unsigned int age = 1;  
cout << (age > -1) << endl; // Output: false
// Ugly
double myMoney;
int number, myNumber;
double yourMoney, test;
int anotherNumber;

// Better
double myMoney;
double test;
double yourMoney;
int anotherNumber;
int number;

Classes:

We could use two different styles:

Use default member values when they’re always initialized with the same value, unless the value is different depending on the constructor, then initialize those values individually.

Try to avoid empty constructors, the uninitialized members has undefined values (use default member to avoid this, if you want).

class Human {
	
	string name;
	uint8_t age;
  
public:
	Human(const uint8_t age, const string& name) {
		this->age = age;
		this->name = name;
	}
	
	void setAge(const uint8_t newAge) {
      	if (newAge <= 150) {
        	age = newAge;	  
      	}
	}
  
  	bool isAdult() {
    	return (age >= 18);
  	}
	// ...
};

Main function:

You don’t need to type return 0; at the end.

4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

for loops:

Avoid old for loops when iterating through a range of elements:

	
int numbers[] = {-1,2,3};
vector<string> names = {"Daniel", "John", "Peter"};
	
// Classic for

for (uint8_t i = 0; i < 3; ++i) { // Use "size_t" for large vectors
	cout << numbers[i] << ' ';
}

for (vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
	cout << *it << ' ';
}

// Range-based for loop

for (const int& number: numbers) {
	cout << number << ' ';
}

for (const auto& name: names) { // "auto" is a string in this case
	cout << name << ' ';
}

Extra:

Comments:

// Sort a range of elements in a custom order using QuickSort algorithm
template <typename Type, typename Function>
void quickSort(const vector<Type>& elements, const Function& order) { /*...*/ }
/* BAD! */

// Main function
int main() {
	
	// This is the number of apples
	int a;
	
	string s;
	
	// Print text c:
	cout << "Daniel bought " << a << " apples in " << s << endl;
}

/* GOOD */

int main() {
	
	string name = "Daniel";
	string lastName = "ILLESCAS";
	float height = 170.0; // in meters

	// Print the name in upper case
	for (char letter: name) {
		cout << char(toupper(letter));
	}
	
	cout << toLower(lastName) << endl;
}

// Transform a string to lower case
string toLower(string str) {
	transform(str.begin(), str.end(), str.begin(), ::tolower);
	return str;
}