Changes between Version 4 and Version 5 of code/C++_styleguide
- Timestamp:
- Oct 30, 2007, 4:27:19 PM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
code/C++_styleguide
v4 v5 8 8 Remember that any violation to the guide of course is allowed if it enhances readability. 9 9 10 == File Organization==10 == Files == 11 11 === Header and Source === 12 12 Keep your classes/files short, don't exceed 2000 LOC (if it get's longer you may separate functions into different modules). Put every class in a separate file and name the file like the class name (don't use !CamelCase names separate them with underlines). Create a separate header (ends with .h) and source file (ends with .cc). Example for class {{{MyExampleClass}}}. … … 26 26 Place machine-dependent code in a special file so that it may be easily located when porting code from one machine to another. 27 27 28 === 80 Columns at Maximum === 29 File content must be kept within 80 columns. 30 ''Comment: 80 columns is a common dimension for editors, terminal emulators, printers and debuggers, and files that are shared between several people should keep within these constraints. It improves readability when unintentional line breaks are avoided when passing a file between programmers.'' 31 28 32 == Comments == 29 33 … … 35 39 36 40 == Naming Conventions == 41 37 42 === Classes and Types === 38 43 Names representing types must be in mixed case starting with upper case. … … 46 51 47 52 === Variables === 48 Variable names must be in mixed case starting with lower case. 53 Variable names must be in mixed case starting with lower case. Each variable declaration on a new line. Avoid abbreviations in names. 49 54 {{{ 50 55 string testString; … … 58 63 int numberOfTimes_; 59 64 }; 60 65 }}} 66 Use useful names: 67 {{{ 68 // wrong 69 ProgressBar *prbar; 70 string prtxt, errstr; 71 72 // correct 73 ProgressBar* downloadProgressBar; 74 string progressText; 75 string errorString; 76 }}} 77 ''Comment: Apart from its name and its type, the scope of a variable is its most important feature. Indicating class scope by using underscore makes it easy to distinguish class variables from local scratch variables. This is important because class variables are considered to have higher significance than method variables, and should be treated with special care by the programmer.'' 61 78 62 79 === Constants == 63 Named constants (including enumeration values) must be all uppercase using underscore to separate words. Always use constants instead of numbers. 80 Named constants (including enumeration values) must be all uppercase using underscore to separate words. Always use constants instead of numbers. 64 81 {{{ 65 82 const int MAX_ITERATIONS = 5; … … 69 86 70 87 === Functions === 71 Names representing methods or functions must be verbs and written in mixed case starting with lower case. 88 Names representing methods or functions must be verbs and written in mixed case starting with lower case. Avoid abbreviations in names. 72 89 {{{ 73 90 void calculateTheLowerBoundary() {} 74 91 int howManyItemsLeft() {} 75 92 }}} 76 For interface functions to local member variables use short functions :93 For interface functions to local member variables use short functions (setter and getter functions): 77 94 {{{ 78 95 class WorldEntity { … … 91 108 }}} 92 109 110 The name of the object is implicit, and should be avoided in a method name. 111 {{{ 112 class Line { 113 public: 114 float getLength(); // Not getLineLength() since it's clear that it is a line. 115 }; 116 }}} 93 117 118 === Enumerations === 119 Enumeration constants can be prefixed by a common type name. 120 {{{ 121 enum Color { 122 COLOR_RED, 123 COLOR_GREEN, 124 COLOR_BLUE 125 }; 126 }}} 127 128 === Namespaces === 129 Names representing namespaces should be all lowercase. 130 {{{ 131 namespace ioutil { 132 133 } 134 }}} 94 135 95 136 == Indentation == 96 137 === Spaces === 97 138 Use 2 spaces for an indentation level. Never use tabs, as it is common in windows IDEs. 139 140 141 == Whitespaces == 142 Use one space after each keyword 143 144 145 146 147 148 98 149 99 150 == Special Implementations ==