Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Coding style

Metabolix edited this page Feb 10, 2012 · 4 revisions

Coding style

Classes are named like ThisExample and functions and variables like thisExample. Tabs are used for indentation. Tab are 8 characters wide. Do not leave white-space at the end of lines. Each file must end with a newline character. Newlines are in UNIX format (\n). Non-ASCII characters use UTF-8 encoding.

Doxygen is used for generating documentation, see the existing source files for examples. Every class and class member and all globals should be documented, unless it's clear that they're stubs that can't really be documented yet.

The rest of the coding style is mostly based on Linux kernel coding style, with some minor exceptions:

  • The braces in function definitions are positioned as in any other blocks.
  • Each if, for, while etc. should have braces, even if it's followed by only one statement.

Also notice:

  • Long lines that need splitting should basically be rewritten to a few shorter ones.
  • Do not fear using temporary variables (or constants)!

Here is a small code sample. Unfortunately this wiki screws the tabs. :(

// A constructor without parameters:
ExampleClass::ExampleClass() {
	// A for loop; notice all spaces and braces!
	for (int i = 0; i < 1; ++i) {
		std::cout << "Do something!" << std::endl;
	}

	// An if statement; notice the else!
	if (true) {
		std::cout << "Do something!" << std::endl;
	} else if (true) {
		std::cout << "Dead code here..." << std::endl;
	} else if (true) {
		std::cout << "More dead code here..." << std::endl;
	}

	// A function call:
	someFunction(someValue, anotherValue);

	// A function call with many long arguments:
	someFunction(
		theseParametersAreSoLong + thatItMakesNoSense,
		toPutThemOnOneLine,
		so + put + each + variable + to + its + own + line
	);
}

// A constructor with simple parameters used for initialization:
ExampleClass::ExampleClass(int x_, int y_):
	x(x_),
	y(y_) {
	std::cout << "Do something!" << std::endl;
}

// A constructor with long parameter names:
ExampleClass::ExampleClass(
	SomeLongTypeName x_,
	AnotherQuiteLongTypeName y_,
	SomeTerriblyLongTypeNameThatJustDoesNotFitOnOneLine z_
):
	x(x_),
	y(y_),
	z(z_) {
	std::cout << "Do something!" << std::endl;
}

// A function with few parameters:
void ExampleClass::someFunction(int x, int y) {
	std::cout << "Do something!" << std::endl;
}

// A function with many parameters and a const qualifier:
const std::ostream& ExampleClass::someFunction(
	SomeLongTypeName x,
	AnotherQuiteLongTypeName y,
	SomeTerriblyLongTypeNameThatJustDoesNotFitOnOneLine z
) const {
	return std::cout << "Do something!" << std::endl;
}

Clone this wiki locally