Programming Reference:Cpp Coding Style: Difference between revisions
Appearance
m Coding Style moved to Cpp Coding Style: Disambiguation |
No edit summary |
||
| Line 1: | Line 1: | ||
= Coding Style = | = C++ Coding Style = | ||
== Line Formatting == | == Line Formatting == | ||
* No tab characters (configure your editor to insert spaces instead) | * No tab characters (please configure your editor to insert spaces instead). | ||
* Indentation in steps of 2 | * Indentation in steps of 2 or 3 space characters. | ||
* Opening braces have their own line, aligned with the previous line; corresponding closing braces are placed on their own lines, at the same character position as the opening brace. | * Opening braces have their own line, aligned with the previous line; corresponding closing braces are placed on their own lines, at the same character position as the opening brace. | ||
* For function definitions, return types appear on their own line, such that the function name is first in its line. | * For function definitions, return types appear on their own line, such that the function name is first in its line. | ||
| Line 9: | Line 9: | ||
== Naming == | == Naming == | ||
* '''ALL_UPPERCASE_NAMES''' are reserved for preprocessor macros. | * '''ALL_UPPERCASE_NAMES''' are reserved for preprocessor macros. | ||
* | * '''CamelCase''' (uppercase letters inside words) is used to indicate word boundaries. | ||
* Class names and namespaces begin with uppercase letters: '''MyNameSpace''', '''TheClass'''. | * Class names and namespaces begin with uppercase letters: '''MyNameSpace''', '''TheClass'''. | ||
* Local variables and function arguments begin with lowercase letters. | * Local variables and function arguments begin with lowercase letters. | ||
* Accessor functions use Set/Get or directly the name of the property. Setter functions should return a non-const reference to the data object itself rather than '''void'''. | * Accessor functions use Set/Get followed by the property's name, or directly the name of the property. Setter functions should return a non-const reference to the data object itself rather than '''void''', this allows for chaining. | ||
=== Name prefixes === | === Name prefixes === | ||
Prefixes should carry information about the scope and usage of a variable but not its type. | Prefixes should carry information about the scope and usage of a variable but not its type. | ||
* '''m''' for class data members | * '''m''' for class data members, | ||
* '''s''' for static class members and static variables | * '''s''' for static class members and static variables, | ||
* '''g''' for | * '''g''' for globals, | ||
* '''c''' for constants | * '''c''' for constants, | ||
* '''p''' for pointers | * '''p''' for pointers, | ||
* '''fp''' for function pointers | * '''fp''' for function pointers, | ||
* '''in''' for function input arguments | * '''in''' for function input arguments, | ||
* '''out''' for function output arguments | * '''out''' for function output arguments, | ||
* '''io''' for function arguments used for input and output | * '''io''' for function arguments used for input and output. | ||
== Pointers, References, Stack and Heap, Memory allocation, Control flow == | == Pointers, References, Stack and Heap, Memory allocation, Control flow == | ||
| Line 37: | Line 37: | ||
void | void | ||
MyClass::MyFunction( const int | MyClass::MyFunction( const int inTheInput, int& outTheResult ) | ||
{ | { | ||
outTheResult = 10; | |||
for( int | for( int theCounter = 0; < inTheInput; ++i ) | ||
{ | { | ||
int k = 4; | int k = 4; | ||
while( --k > 0 ) | while( --k > 0 ) | ||
outTheResult -= k; | |||
} | } | ||
} | } | ||
Revision as of 13:54, 24 January 2007
C++ Coding Style
Line Formatting
- No tab characters (please configure your editor to insert spaces instead).
- Indentation in steps of 2 or 3 space characters.
- Opening braces have their own line, aligned with the previous line; corresponding closing braces are placed on their own lines, at the same character position as the opening brace.
- For function definitions, return types appear on their own line, such that the function name is first in its line.
Naming
- ALL_UPPERCASE_NAMES are reserved for preprocessor macros.
- CamelCase (uppercase letters inside words) is used to indicate word boundaries.
- Class names and namespaces begin with uppercase letters: MyNameSpace, TheClass.
- Local variables and function arguments begin with lowercase letters.
- Accessor functions use Set/Get followed by the property's name, or directly the name of the property. Setter functions should return a non-const reference to the data object itself rather than void, this allows for chaining.
Name prefixes
Prefixes should carry information about the scope and usage of a variable but not its type.
- m for class data members,
- s for static class members and static variables,
- g for globals,
- c for constants,
- p for pointers,
- fp for function pointers,
- in for function input arguments,
- out for function output arguments,
- io for function arguments used for input and output.
Pointers, References, Stack and Heap, Memory allocation, Control flow
- Always use the narrowest possible scope for a name to avoid side effects.
- Always initialize variables (at declaration resp. constructor).
- Allocate from the stack rather than the heap unless there is a good reason to use the new operator. This avoids memory leaks.
- Use references rather than pointers wherever possible.
- Use STL containers rather than allocating arrays with new[]. This eliminates a number of possible errors (initialization, allocation, deallocation).
- Avoid goto, break outside switch-case blocks, and multiple return statements.
Example Function
void
MyClass::MyFunction( const int inTheInput, int& outTheResult )
{
outTheResult = 10;
for( int theCounter = 0; < inTheInput; ++i )
{
int k = 4;
while( --k > 0 )
outTheResult -= k;
}
}