Do not, under any circumstances, exceed 80 characters on a single line of code. This makes it easy to print the code, and identify the beginning and end of any syntactic construct.
Rationale: I've stolen this from a site about web design, but I think the logic stands for code as well, so here it is.
"Newspapers have long realized the value of short line length. That's why most articles are in narrow columns rather than going across the entire page. Likewise, magazine articles are split into columns rather than going across the entire page. Unfortunately, most designers have not yet learned the basics of good typography. Therefore most webpages have line lengths that are way to long for comfortable reading. Generally, you should have no more than 15 words on a single line, or roughly 65 characters."
In addition to this, keeping everything under 80 characters makes the code easy to read from any terminal, and certainly makes it ... printable.
Indent at 4 characters. Use spaces, not TABs.
Example:
if (someVariable > 10)
someVariable = 10;
Rationale: 4 spaces provide a reasonable level of
indentation. The use of TABs would make the code
indent differently under different editors
depending on the settings used for the TAB size.
Do not put spaces between function names and the parenthesis that starts the list of arguments. It makes it less obvious that the list of parameters belongs to that function call. Also, leave one space between parameters, after the comma, but don't leave any space before the first parameter, after the last one, or before a comma. If the function has no parameters, don't leave a space between parenthesis.
Good Examples:
function();
function(1, 2);
Bad Examples:
function( );
function (1, 2);
function( 1, 2 );
function( 1 , 2 );
Rationale: GNU Coding Standards.
If you need braces, put the open bracket on a line by itself.
Example:
for (int i = 0; i < 100; i++)
{
cout << i << endl;
function(i);
}
Rationale: GNU Coding Standards, slightly
modified.
It is important to put the open-brace that starts the body of a C++ function in column zero, and avoid putting any other open-brace, open-parenthesis or open-bracket in column zero. Several tools look for open-braces in column zero to find the beginnings of C functions. These tools will not work on code not formatted that way.
It is also important for function definitions to start the name of the function in column zero. This helps people to search for function definitions, and may also help certain tools recognize them. Thus, the proper format is this:
Example:
static char*
Text::concat(char* s1, char* s2) <-- Name starts in column zero.
{ <-- Open brace in column zero.
...
}
It is not necessary to do this for function prototypes in header files.
Rationale: GNU Coding Standard, adapted for C++.Leave one space between assignment, logic and arithmetic operators:
Examples:
if (i == 0)
break;
a += 3;
a = b + c;
Rationale: GNU Coding Standards.
Don't leave spaces after the unary operators:
Examples:
if (!flag)
break;
cin/cout/cerr-like indentation should look like this:
Example:
cout << "Problem:\t" << problem.name()
<< "Solution:\t" << problem.solution()
<< endl;
Alternatively, you can split this in several
separate instructions, one per line.
Avoid placing more than one statement on a line.
Good Example:
a++;
b += 3;
Bad Example:
a++; b += 3;
if (condition_number_one &&
condition_number_two &&
condition_number_three)
...
Rationale: This is against the GNU Coding
Standards :-).