| 184 | === Constants === |
| 185 | The use of magic numbers in the code should be avoided. Numbers other than 0 and 1 should be considered declared as named constants instead. |
| 186 | |
| 187 | |
| 188 | == Statements == |
| 189 | |
| 190 | |
| 191 | === Access Modifiers for Classes === |
| 192 | The parts of a class must be sorted public, protected and private. All sections must be identified explicitly. Not applicable sections should be left out. |
| 193 | |
| 194 | |
| 195 | === Type Casts === |
| 196 | Type conversions must always be done explicitly. Never rely on implicit type conversion. |
| 197 | {{{ |
| 198 | floatValue = static_cast<float>(intValue); // NOT: floatValue = intValue; |
| 199 | // or: |
| 200 | floatValue = float(intValue); |
| 201 | }}} |
| 202 | By this, the programmer indicates that he is aware of the different types involved and that the mix is intentional. |
| 203 | |
| 204 | |
| 205 | === Encapsulation === |
| 206 | Class variables should never be declared public. The concept of C++ information hiding and encapsulation is violated by public variables. Use private variables and access functions instead. One exception to this rule is when the class is essentially a data structure, with no behavior (equivalent to a C struct). In this case it is appropriate to make the class' instance variables public. |
| 207 | |
| 208 | |
| 209 | === Loops === |
| 210 | Only loop control statements must be included in the for() construction. |
| 211 | |
| 212 | |
| 213 | == Layout == |
| 214 | === Indentation === |
| 215 | Basic indentation should be 2. |
| 216 | {{{ |
| 217 | for (i = 0; i < numberOfElements; i++) |
| 218 | a[i] = 0; |
| 219 | }}} |
| 220 | |
| 221 | |
| 222 | === Block Layout === |
| 223 | Block layout should be as illustrated in this example: |
| 224 | {{{ |
| 225 | while (!done) { |
| 226 | doSomething(); |
| 227 | done = moreToDo(); |
| 228 | } |
| 229 | }}} |
| 230 | |
| 231 | === Class Declaration === |
| 232 | The class declarations should have the following form: |
| 233 | {{{ |
| 234 | class SomeClass : public BaseClass |
| 235 | { |
| 236 | public: |
| 237 | void functionBla(); |
| 238 | |
| 239 | protected: |
| 240 | ... |
| 241 | |
| 242 | private: |
| 243 | ... |
| 244 | }; |
| 245 | }}} |
| 246 | |
| 247 | |
| 248 | === Function Definitions === |
| 249 | Method definitions should have the following form: |
| 250 | {{{ |
| 251 | void functionBla() |
| 252 | { |
| 253 | ... |
| 254 | } |
| 255 | }}} |
| 256 | |
| 257 | === If-Statement === |
| 258 | The if-else class of statements should have the following form: |
| 259 | {{{ |
| 260 | if (condition) { |
| 261 | ... |
| 262 | } |
| 263 | else { |
| 264 | ... |
| 265 | } |
| 266 | }}} |
| 267 | |