Project

General

Profile

C++ coding rules » History » Version 1

ModularMix, 10/18/2011 04:06 PM

1 1 ModularMix
h1. C++ coding rules
2 1 ModularMix
3 1 ModularMix
h2. Usefulness
4 1 ModularMix
5 1 ModularMix
The following C++ code rules aim to keep the same plugin structure whatever the developer.
6 1 ModularMix
7 1 ModularMix
h2. General points
8 1 ModularMix
9 1 ModularMix
* Indentation
10 1 ModularMix
** 2 space characters (no tab),
11 1 ModularMix
** Left brace at the beginning of the line (no Java-style using the left brace at the end of the line).
12 1 ModularMix
13 1 ModularMix
<pre>
14 1 ModularMix
 if (true)
15 1 ModularMix
 {
16 1 ModularMix
   // Good Indentation
17 1 ModularMix
 }
18 1 ModularMix
 else
19 1 ModularMix
 {
20 1 ModularMix
 // Bad indentation
21 1 ModularMix
 }
22 1 ModularMix
</pre>
23 1 ModularMix
24 1 ModularMix
* No space between the latest character on a line and the final semicolon.
25 1 ModularMix
26 1 ModularMix
<pre>
27 1 ModularMix
 int maPremiereVariable = 1; // Good
28 1 ModularMix
 int maSecondeVariable = 1 ; // Bad
29 1 ModularMix
</pre>
30 1 ModularMix
31 1 ModularMix
* The character '*' for a pointer and '&' for a reference must be stuck to the related type or variable.
32 1 ModularMix
33 1 ModularMix
<pre>
34 1 ModularMix
 MyClass* pointeur1 = new MyClass();  // Good
35 1 ModularMix
 MyClass * pointeur2 = new MyClass(); // Bad
36 1 ModularMix
 MyClass *pointeur1 = new MyClass();  // Bad
37 1 ModularMix
</pre>
38 1 ModularMix
 
39 1 ModularMix
* Each new parameter of a function must be preceded by a space. Furthermore, there mustn't be any space just after the left or right parenthesis.
40 1 ModularMix
41 1 ModularMix
<pre>
42 1 ModularMix
 int maVariable = maFonction(param1, param2, param3);       // Good
43 1 ModularMix
 int maVariable2 = maFonction(param1,param2,param3);        // Bad
44 1 ModularMix
 int maVariable2 = maFonction(param1 , param2  , param3 );  // Bad
45 1 ModularMix
</pre>
46 1 ModularMix
47 1 ModularMix
* Don't hesitate to follow a very strict indentation, even if when the source code can be composed of a single line. This method will make the debugging easier.
48 1 ModularMix
49 1 ModularMix
<pre>
50 1 ModularMix
 if(curMaterialPair)SAFE_DELETE(curMaterialPair); // Bad
51 1 ModularMix
  
52 1 ModularMix
 if (curMaterialPair)
53 1 ModularMix
   SAFE_DELETE(curMaterialPair);                  // Good
54 1 ModularMix
 
55 1 ModularMix
 if (curMaterialPair)                             // Also good
56 1 ModularMix
 {
57 1 ModularMix
   SAFE_DELETE(curMaterialPair);
58 1 ModularMix
 }
59 1 ModularMix
</pre>
60 1 ModularMix
61 1 ModularMix
62 1 ModularMix
h2. Class definition
63 1 ModularMix
64 1 ModularMix
For each definition of a class, the following structure must be followed :
65 1 ModularMix
<pre>
66 1 ModularMix
 class MyClass
67 1 ModularMix
 {
68 1 ModularMix
 // Member variables, typedefs and enums are defined here
69 1 ModularMix
 public:
70 1 ModularMix
 protected:
71 1 ModularMix
   int maVariableMembre; // member variable sample.
72 1 ModularMix
 private:
73 1 ModularMix
 
74 1 ModularMix
 // Member functions and methods are defined here
75 1 ModularMix
 public:
76 1 ModularMix
   /*!
77 1 ModularMix
   Constructor sample
78 1 ModularMix
   */
79 1 ModularMix
   MyClass(int newVal);
80 1 ModularMix
 protected:
81 1 ModularMix
 private:
82 1 ModularMix
 };
83 1 ModularMix
</pre>
84 1 ModularMix
85 1 ModularMix
Using this way, it's easy to identity the different elements from a class, and their respective access level (public, protected or private).