Each level of indentation is exactly one hard tab, or character 0x09 in the ISO 8859-1 character set..
Curly braces follow on the following line at all times, same level of indentation.
| Example |
while (1)
{
// do something
// do more
}
|
If a code block is only one statement, following a loop or conditional, curly braces are not required, but suggested for complicated, or code subject to much modification.
Code may also follow the loop or conditional on the same line, but only in the event of simple constructs.
| Example |
if (i<0) i=0; |
| Example |
if (i<0)
{
i=abs(i)*4+2;
}
|
Comments are to be placed on the previous line, with a blank line preceding it, and not to be placed to the right of a line of code, unless it is in a block with no curly braces. Placing a comment on a line with a curly brace is permitted.
| Example |
if(i < 0) i = 0; // optimize me else if(i > 100) i = 100; |
| Example |
// optimize me i=0; |
| Example |
int factorial(int n)
{ // Replace this with a non-recursive after the beta
return n>=2 ? factorial(n-1)*n : n;
}
|
A label of any sort (case, goto label, public: et al.) is to
not gain a level of indentation, while the rest of the block does.
| Example |
class Foo
{
public:
Foo();
};
|
| Example |
switch(command)
{
case 1:
doThis();
break;
case DONT_KILL_ME:
exit(1);
break;
default:
break;
};
|
| Example |
int i = 0; gotoIsEvil: i++; if(i < 10) goto gotoIsEvil; |
The colons following a label may not have a preceding space.
if, elseIn an else block, the position of the code (same line, or next) must be
the same as the preceding if block. Same goes for the presence of curly
braces. If the the else code is a complex block, while the if
is simple, curly braces are required for both sets.
The space after the if word is optional.
| Example |
if (i<0)
{ // optimize me
i=0;
}
else
{
// hmmm...
kdDebug(KMYAPP_AREA) << "i >= 0" << endl;
i++;
}
|
| Example |
// optimize me if (i<0) i=0; |
whileThis can be used identically to an if construct.
In a do... while() construct, curly braces are always present, and there is a single
space between the closing curly brace and the while.
| Example |
do
{
something();
} while (thisIsTrue());
|
forfor can also be used like an if, spaces between semicolon
and operator is optional, but suggested in complex code fragments.
casecase, as used in a switch block has optional parentheses
following the test value. It is suggested in complex values, especially non-integers
(including named-integers, such as enums and #defines.
Curly braces for case blocks should only be used when the block declares a variable.
namespaceCode in a namespace is not to be indented an extra level. This is because
properly modular code will typically have an entire source file within a particular
namespace, so the extra indentation is useless.
All functions and variables have different words each capitalized, except the first one.
A function always begins with a lower case letter. A function or variable may not have
underscores, except in the the case where an internal function is declared in a public
context, but may not be called (particularly when modularity needs to be maintained).
classes and namespaces are to be named as functions are, save that their first letter is to be
capitalized as well.
Preprocessor definitions are to have names in all capitals. However, the use of
enums for class and namespace-specific constants is preferred. enum
constants are to be capitalized as classes.
identifiers may not be prefixed (as in the m_lpszHungarian notation), with the exception of prefixing a variable
with 'm' followed by a capitalized name afterwards. An example 'mEnabled'. This
can only be used when it is a member of a class (denoting the letter 'm'). This is an
optional construct. Likewise, function parameter names may optionally take a prefix of _.
| Example |
#ifndef MCP_H
#define MCP_H
class MCP : public PlayList
{
public:
/**
* Constructor. The _parent is what will be shown
* and hidden by the user when he hits Toggle
* Playlist.
*/
MCP(QWidget *_parent, const char *_name);
enum SongGenre
{
Dumb = 0,
Loud,
Annoying
};
private:
SongGenre mGenre;
};
#endif // MCP_H
|
The names of C++ source files are to end in .cpp. The names of C++ headers are to end in .h, or have no extension at all.
Functions which take no parameters may be written with a void parameter list.
Operators are not required to be surrounded by spaces, but they are recommended for clarity.
If someone complains that you indent with [the one true indentation character] instead of spaces, refer them to why-to-tabs.html
If your math.h is damaged, the following example will assist you in rebuilding it:
| Example |
#ifdef FUNDAMENTALISM #define PI 3.0 // 1 Kings 7:23 #else #define PI 3.14159265 #endif |