Changes between Version 7 and Version 8 of CodingRules
- Timestamp:
- 11/26/13 15:34:36 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CodingRules
v7 v8 28 28 0. For proper documentation, please have a look at other classes. Note that FACT++ and Mars style is slightly different. Mars is using THtml with some Mars specific setup, FACT++ is using doxygen. 29 29 0. Never define constants as preprocessor directives. Generally avoid preprocessor directives. 30 o. omit else in constructions like *if (...) return x; [else] ....* 31 0. Source files have the extension cc, header files the extension h, C source files just c. 32 0. Operators +=, -=, etc. are preferable over constructions like x=x+, x=x-, ... 33 0. If you omit intentionally a break in a switch statement (*fallthru*) add a comment 34 0. Sometimes including system headers is not identical for all operating systems, comments might help 35 0. Try to avoid using dynamic allocation whenever possible: "int i=5; /* do something with i*/; /* i runs out of scope */" is preferred over "int *i = new int; *i=5; /* do something with i */; delete i;" Although this example sounds ridiculous, the same is true for every class instance you use. 30 36 31 37 == Example == 32 38 {{{#!C++ 39 #ifndef MARS_Complex 40 #define MARS_Complex 41 42 #include <math.h> // needed for sqrt on Ubuntu 10.14 43 44 class Complex 45 { 46 private: 47 double fRe; 48 double fIm; 49 50 public: 51 Complex(double re, double im) 52 : fRe(re), fIm(im) 53 { 54 } 55 56 double Modulus() const 57 { 58 return sqrt(fRE * fRe + fIm * fIm); 59 } 60 61 bool Foo(int a, int b) 62 { 63 for (int i=0; i<b-a; i++) 64 { 65 if (i<b) 66 bar(i); 67 else 68 { 69 bar(i); 70 bar(b); 71 } 72 } 73 74 const int c = a + b; 75 switch (c) 76 { 77 case 0: 78 // Code goes here 79 return false; 80 case 1: 81 case 2: 82 // Code goes here 83 return false; 84 default: 85 // Code goes here 86 return true; 87 } 88 89 return true; 90 } 91 }; 92 93 #endif 33 94 }}} 34 95