C++ style guide: Difference between revisions

Jump to navigation Jump to search
2,022 bytes added ,  6 September 2021
Add separator recommendation between member functions and variables.
(Add separator recommendation between member functions and variables.)
(20 intermediate revisions by 2 users not shown)
Line 7: Line 7:


== Formatting ==
== Formatting ==
=== Line Length ===
Maximal length of source lines is 79 characters.


=== Indentation ===
=== Indentation ===


Use only spaces, and indent 2 spaces at a time.
* Use only spaces.
* Tabs are prohibited.
* Indent with 2 spaces at a time.


We use spaces for indentation. Absolutely do not use tabs in your code.
==== Control structures (if, while, ...) ====
You should probably set your editor to emit spaces when you hit the tab key.


When indenting, indent the statement after control
When indenting, indent the statement after control
Line 19: Line 24:
is a compound statement, indent ''both'' the curly braces and the
is a compound statement, indent ''both'' the curly braces and the
body of the statement (so that the body gets indented by ''two''
body of the statement (so that the body gets indented by ''two''
indents). This format is known as "GNU style" and is an option for
indents).
some code formatting tools.  Example indenting:
 
Example:


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 34: Line 40:
If you have nested {{codeline|if}} statements, use extra braces for extra
If you have nested {{codeline|if}} statements, use extra braces for extra
clarification.
clarification.
==== Split long expressions ====


Split long expressions in such a way that a continuation line starts
Split long expressions in such a way that a continuation line starts
Line 46: Line 54:
</syntaxhighlight>
</syntaxhighlight>


=== Non indenting whitespace ===
==== 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 52: 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
=== Pointer and Reference appearance ===
and its target, e.g., {{codeline|! A}}.


Declarations of pointers have the '*' character cuddled with the name of the variable.
Declarations of pointers have the '*' character cuddled with the name of the variable.
Line 67: Line 74:
</syntaxhighlight>
</syntaxhighlight>


=== Line Length ===
=== Miscellaneous ===


Keep the length of source lines to 79 characters or less, for maximum
The negation operator is written with a space between the operator
readability in the widest range of environments.  This is inherited from
and its target, e.g., {{codeline|! A}}.
the [https://www.gnu.org/prep/standards/standards.html#Formatting GNU Coding Standards].


=== Function headers ===
=== Function headers ===
Line 88: Line 94:
parenthesis.  You should put a space before the left open parenthesis and after
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 ===
The access specifier ({{codeline|public}}, {{codeline|protected}}, {{codeline|private}}) should always be stated rather than relying on the C++ language defaults for a particular object (for example, "{{codeline|class}}" = "{{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 should be specified before member variables.  If there are both member functions and member variables use
    //--------
between the sections to visually separate the two categories.


=== Namespace ===
=== Namespace ===
Line 137: Line 155:
names consisting of 1-2 letters.  Do not use mixed case names.
names consisting of 1-2 letters.  Do not use mixed case names.


=== Member Variables ===
Member variables should use the prefix "m_" whenever possible.
=== Class Variables ===
Class variables should use the prefix "s_" (for "static") whenever possible.
=== Filenames ===
As with m-files, the file name of a C++ source file containing a class should match the name of the class defined within the file.  For example, "password.h" defines the class "password" rather than "passwd.h" which is a common abbreviation for "password".


== Header Files ==
== Header Files ==
Line 176: Line 205:
=== references ===
=== references ===


Use references when passing variables that will be changed to subroutines rather
Use references when passing variables that will be changed by a subroutine rather than the C-style method of passing pointers.
than the C-style method of passing pointers.


When passing variables that are large, but will not be changed in a subroutine (read-only)
{| class="wikitable"
, consider using 'const' references.  This helps avoid overflowing the finite stack capacity
! style="color:green;" | good
of a program while still ensuring that read-only access is enforced.
! style="color:darkred;" | bad
|-
| <syntaxhighlight lang="c++">
void foo (int& a_ref)
{
  // foo changes content of `a_ref`
  a_ref = a_ref + 1;
}
 
void bar ()
{
  int a = 42;
  foo (a);
}
</syntaxhighlight>
| <syntaxhighlight lang="c++">
void foo (int *a_ptr)
{
  // foo changes content of `a_ptr`
  *a_ptr = *aptr + 1;
}
 
void bar ()
{
  int a = 42;
  foo (&a);
}
</syntaxhighlight>
|}
 
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"
! style="color:green;" | good
! style="color:darkred;" | bad
|-
| <syntaxhighlight lang="c++">
void foo (const std::string& str_ref)
{
  // foo does not change content of `str_ref`
}
 
void bar ()
{
  std::string str ("This is a large variable, however as a reference it will take up just 8 bytes on the stack when passed to the subroutine foo()");
  foo (str);
}
</syntaxhighlight>
| <syntaxhighlight lang="c++">
void foo (std::string str_copy)
{
  // foo does not change content of `str_copy`
}
 
void bar ()
{
  std::string str ("This is a large variable that will be copied on to the stack and passed as a temporary variable to the subroutine foo()");
  foo (str);
}
</syntaxhighlight>
|}


=== std::string ===
=== std::string ===
1,072

edits

Navigation menu