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