C++ Style Guide

Version 1 (Tim Place, 2011-03-15 05:57 pm)

1 1 Tim Place
h1. C++ Style Guide
2 1 Tim Place
3 1 Tim Place
4 1 Tim Place
h2. Naming Conventions
5 1 Tim Place
6 1 Tim Place
7 1 Tim Place
h3. Message Naming
8 1 Tim Place
9 1 Tim Place
Be as unambiguous as possible.
10 1 Tim Place
11 1 Tim Place
Example:
12 1 Tim Place
13 1 Tim Place
You want to get the names of all of the attributes of an object.  You could name this "getAttributes".  Unfortunately, this is ambiguous.  Does it mean to get the Attribute objects?  The attribute names?  The attribute values?  It should be instead be named "getAttributeNames", because it is returning the names of the attributes.  Perhaps it should be even more specific, indicating which attribute names it is getting: is it getting all of them?  just some of them?  just those that are current?
14 1 Tim Place
15 1 Tim Place
16 1 Tim Place
17 1 Tim Place
h2. C++ Syntax Conventions
18 1 Tim Place
19 1 Tim Place
In C/C++ your code can end up looking like almost anything given the flexibility that is afforded with whitespace and other "decorative" code.  While these rules are not hard and fast, it will make it easiest if every generally follows the same way of formatting common idioms of the language.
20 1 Tim Place
21 1 Tim Place
h3. if/else
22 1 Tim Place
23 1 Tim Place
The <code>if</code> condition should be surround by one space on each side (one after the if, one before the opening brace).  The should also be a space before the opening brace of the <code>else</code> clause.
24 1 Tim Place
25 1 Tim Place
The opening brace should be on the same line as the <code>if</code> keyword unless the condition is long and it becomes clearer to do otherwise.
26 1 Tim Place
27 1 Tim Place
The closing braces should always be on their own line and with proper indentation.
28 1 Tim Place
29 1 Tim Place
Generally, when using one line statements in an if/else structure it is preferred to eliminate the braces.  Sometimes it might, however, be handy to leave them for debugging or other reasons.
30 1 Tim Place
31 1 Tim Place
Example:
32 1 Tim Place
<pre>
33 1 Tim Place
  if (condition) {
34 1 Tim Place
    // some code
35 1 Tim Place
  }
36 1 Tim Place
  else {
37 1 Tim Place
    // some code
38 1 Tim Place
  }
39 1 Tim Place
</pre>