Skip to content
AStuehlmeyer edited this page Apr 7, 2014 · 1 revision

This is an excerpt of the Google C++ Style Guide (http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml - retrieved April 2014) used for this library's code.

Naming

General Naming Rules

Function names, variable names, and filenames should be descriptive; eschew abbreviation.

Type Names

Type names (classes, structs, ..) start with a capital letter and have a capital letter for each new word, with no underscores: MyExcitingClass, MyExcitingEnum.

Variable Names

Variable names are all lowercase, with underscores between words. Try to avoid multiple words. Class member variables have a leading 'm_'. For instance: local_variable, m_member_variable.

Constant Names

Use all capital letters with underscores: DAYS_IN_A_WEEK.

Function Names

Regular functions have mixed case and start with a small letter. Use a verb as first word. For setters, use'set' as prefix: setCount(); for getters, use the variable name: count().

Enumerator Names

Name Enumerators like constants: APPLE, DAYS_IN_A_WEEK.

Namespace Names

Namespace names are all lower-case, words are seperated by underscores and based on project names. awesome_project.

Comments

General

For single-line comments, use '// ', for multi-line comments use '/* */' and indent the contents.

// I am a single line comment

/*
	This is a useful comment.
	And we use multiple lines.
*/

File Comments

Add the author and license to the top of every file.

Class Comments

Every class definition should have an accompanying comment that describes what it is for and how it should be used.

Function Comments

Declaration comments start with one sentence, that describes, what the function does. If needed, add a paragraph, that describes how to use the function. Optionally you can can add a comment at the definition that describes, how the function works. Getters and Setters don't need to be commented.

Variable Comments

In general the actual name of the variable should be descriptive enough to give a good idea of what the variable is used for. In certain cases, more comments are required. Each class data member (also called an instance variable or member variable) should have a comment describing what it is used for.

Implementation Comments

In your implementation you should have comments in tricky, non-obvious, interesting, or important parts of your code. Use a new line for the comment if the line gets too long.

TODO Comments

Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.

Formatting

Loops and Branches

Don't use a new line for the opening bracket.

if (x == 1) {
	return z;
}

Vertical Whitespace

Minimal use of vertical whitespace.

Indentation

Use one tab to indent code.

Function Declarations and Definitions

Return type on the same line as function name, parameters on the same line if they fit, opening bracket on the same line as parameters.

Function Calls

On one line if it fits; otherwise, wrap arguments at the parenthesis.

bool retval = doSomething(argument1, argument2, argument3);

bool retval = doSomething(argument1,
			  argument2,
			  argument3,
			  argument4);

Class Format

Sections in public, protected and private order, each indented one time.

Classes

Access Control

Make data members private, and provide access to them through accessor functions as needed. Typically a variable would be called m_foo and the accessor function foo(). You may also want a mutator function setFoo(). Exception: static const data members (typically called FOO) don't need to be private.

Declaration Order

Use the specified order of declarations within a class: public: before private:, methods before data members (variables).

Other

Use of Const

Use const whenever possible.

define-Guard

Use a define-Guard to prevent double-including of definitions.

#ifndef HEADER_NAME
#define HEADER_NAME

/* Code */

#endif //HEADER_NAME

Reference Arguments

All parameters passed by reference must be labeled const.

Casting

Use C++ casts like static_cast<>(). Do not use other cast formats like int y = (int)x; or int y = int(x);.

Lambda Expressions

Try to avoid lambda expressions, or the related std::function or std::bind utilities.

Makros

Avoid using Makros. Only use them, if there is just no other way.