Editing C++ style guide
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 10: | Line 10: | ||
=== Line Length === | === Line Length === | ||
Maximal length of source lines is 79 characters. | |||
=== Indentation === | === Indentation === | ||
* Use only spaces | * Use only spaces. | ||
* Tabs are prohibited. | * Tabs are prohibited. | ||
* Indent with 2 spaces at a time. | |||
==== Control structures (if, while, ...) ==== | ==== Control structures (if, while, ...) ==== | ||
When indenting, indent the statement after control structures (like {{codeline|if}}, {{codeline|while}}, etc.). | When indenting, indent the statement after control | ||
structures (like {{codeline|if}}, {{codeline|while}}, etc.). If there | |||
If there is a compound statement, indent ''both'' the curly braces and the body of the statement (so that the body gets indented by ''two'' indents). | is a compound statement, indent ''both'' the curly braces and the | ||
body of the statement (so that the body gets indented by ''two'' | |||
indents). | |||
Example: | Example: | ||
Line 63: | Line 38: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If you have nested {{codeline|if}} statements, use extra braces for extra clarification | If you have nested {{codeline|if}} statements, use extra braces for extra | ||
clarification. | |||
==== Split long expressions ==== | ==== Split long expressions ==== | ||
Line 112: | Line 54: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Non indenting whitespace === | ||
Consider putting extra braces around a multi-line expression to make it | Consider putting extra braces around a multi-line expression to make it | ||
Line 118: | Line 60: | ||
put extra braces anywhere if it improves clarity. | put extra braces anywhere if it improves clarity. | ||
The negation operator is written with a space between the operator | |||
and its target, e.g., {{codeline|! A}}. | |||
Declarations of pointers have the '*' character cuddled with the | Declarations of pointers have the '*' character cuddled with the name of the variable. | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
Line 126: | Line 69: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
However, references have the '&' character cuddled with the | However, references have the '&' character cuddled with the type of the variable. | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
unsigned int& reference_variable; | unsigned int& reference_variable; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Function headers === | === Function headers === | ||
Format function headers like this: | |||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
Line 149: | Line 87: | ||
The return type of the function and any modifiers are specified on the first | The return type of the function and any modifiers are specified on the first | ||
line. The function name on the second line should start in column 1, and | line. The function name on the second line should start in column 1, and | ||
multi-line argument lists should be aligned on the first | multi-line argument lists should be aligned on the first char after the open | ||
parenthesis. | parenthesis. You should put a space before the left open parenthesis and after | ||
commas, for both function definitions and function calls. | commas, for both function definitions and function calls. | ||
=== Namespace === | === Namespace === | ||
All code should be in the | All code should be in the octave namespace. This is an ongoing project. We | ||
are still moving existing classes into namespaces but all new classes | |||
should go somewhere into the "octave" namespace. There is 1 extra level for namespaces | |||
inside octave to be used with care, we don't want too many namespaces. | |||
Ask before creating a new namespace. | |||
* Indent namespaces as any other block. Emacs and other editors can do this automatically. | |||
* Define namespace on the .cc files; | |||
* Do not use "using X" directives; | |||
* Do not declare anything on the std namespace; | |||
{{Code|namespace style on a .h file|<syntaxhighlight lang="cpp"> | |||
// Note indentation | |||
{{Code| | namespace octave | ||
{ | |||
namespace math | |||
{ | |||
class foo | |||
{ | |||
public: | |||
foo (...); | |||
}; | |||
} | |||
} | |||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{Code|namespace style on a .cc file|<syntaxhighlight lang="cpp"> | |||
// Note indentation and that functions are not defined | |||
{{Code| | // as "octave::math::foo:foo" | ||
// Note indentation and that functions are not defined as "octave::math::foo:foo" | |||
namespace octave | namespace octave | ||
{ | { | ||
Line 203: | Line 133: | ||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
== Naming == | == Naming == | ||
Use lowercase names if possible. Uppercase is acceptable for variable names consisting of 1-2 letters. Do not use mixed case | Use lowercase names if possible. Uppercase is acceptable for variable | ||
names consisting of 1-2 letters. Do not use mixed case names. | |||
== Header Files == | == Header Files == | ||
Line 262: | Line 178: | ||
=== references === | === references === | ||
Use references when passing variables that will be changed | Use references when passing variables that will be changed to subroutines rather than the C-style method of passing pointers. | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 296: | Line 212: | ||
|} | |} | ||
When passing variables that are large, but will not be changed in a subroutine (read-only), | When passing variables that are large, but will not be changed in a subroutine (read-only), consider using 'const' references. This helps avoid overflowing the finite stack capacity of a program while still ensuring that read-only access is enforced. | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 303: | Line 219: | ||
|- | |- | ||
| <syntaxhighlight lang="c++"> | | <syntaxhighlight lang="c++"> | ||
void foo (const | void foo (const int& a_ref) | ||
{ | { | ||
// foo does not change content of ` | // foo does not change content of `a_ref` | ||
} | } | ||
void bar () | void bar () | ||
{ | { | ||
int a = 42; | |||
foo ( | foo (a); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| <syntaxhighlight lang="c++"> | | <syntaxhighlight lang="c++"> | ||
void foo ( | void foo (int& a_ref) | ||
{ | { | ||
// foo does not change content of ` | // foo does not change content of `a_ref` | ||
} | } | ||
void bar () | void bar () | ||
{ | { | ||
int a = 42; | |||
foo ( | foo (a); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |} | ||
=== std::string === | === std::string === |