Editing C++ style guide
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 25: | Line 25: | ||
The braces should not be indented, i.e., they align at the same indentation level as the keyword such as {{codeline|class}}. | The braces should not be indented, i.e., they align at the same indentation level as the keyword such as {{codeline|class}}. | ||
The body of the block is | The body of the block is indented. | ||
Example: | Example: | ||
Line 64: | Line 62: | ||
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 76: | ||
</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 120: | Line 84: | ||
=== Pointer and Reference appearance === | === Pointer and Reference appearance === | ||
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 90: | ||
</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"> | ||
Line 139: | Line 103: | ||
=== Function headers === | === Function headers === | ||
Format function headers like this: | |||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
Line 149: | Line 113: | ||
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. | ||
=== Class declarations === | === Class declarations === | ||
Line 161: | Line 123: | ||
Within a class, the different access blocks should appear in the order 1) {{codeline|public}}, 2) {{codeline|protected}}, 3) {{codeline|private}}. | Within a class, the different access blocks should appear in the order 1) {{codeline|public}}, 2) {{codeline|protected}}, 3) {{codeline|private}}. | ||
Within an access block, member functions | Within an access block, member functions should be specified before member variables. If there are both member functions and member variables use | ||
//-------- | //-------- | ||
Line 169: | Line 131: | ||
=== 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 171: | ||
} | } | ||
</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. | |||
=== Member Variables === | === Member Variables === | ||
Line 296: | Line 261: | ||
|} | |} | ||
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 327: | Line 292: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |} | ||
=== std::string === | === std::string === |