| 6 | When writing a new program or library, please follow a consistent style of brace placement and indentation. We recommend the following code style. |
| 7 | |
| 8 | Remember that any violation to the guide of course is allowed if it enhances readability. |
| 9 | |
| 10 | == File Organization == |
| 11 | === Header and Source === |
| 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}}}. |
| 13 | {{{ |
| 14 | file names: |
| 15 | my_example_class.cc |
| 16 | my_example_class.h |
| 17 | }}} |
| 18 | |
| 19 | === Namespaces === |
| 20 | Create a directory for every namespace (== create a directory for every modules and all its submodules). |
| 21 | |
| 22 | === Inlineing and Templates === |
| 23 | Inline functions and template structures should be declared in a .h header file as described above but implemented in a *-inl.h file (since templates are normally not implemented in a source .cc file). |
| 24 | |
| 25 | === Machine-Dependent Code === |
| 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 | |
| 28 | == Comments == |
| 29 | |
| 30 | === Introductory Comment === |
| 31 | Every file that contains source code must be documented with an introductory comment that provides information on the file name and its contents: |
| 32 | {{{ |
| 33 | // |
| 34 | }}} |
| 35 | |
| 36 | == Naming Conventions == |
| 37 | === Classes and Types === |
| 38 | Names representing types must be in mixed case starting with upper case. |
| 39 | {{{ |
| 40 | class SavingsAccount { |
| 41 | }; |
| 42 | |
| 43 | struct SimpleStruct { |
| 44 | }; |
| 45 | }}} |
| 46 | |
| 47 | === Variables === |
| 48 | Variable names must be in mixed case starting with lower case. |
| 49 | {{{ |
| 50 | string testString; |
| 51 | int numberOfTimes; |
| 52 | }}} |
| 53 | Memeber variables of classes all end with an underline: |
| 54 | {{{ |
| 55 | class TestClass { |
| 56 | |
| 57 | private: |
| 58 | int numberOfTimes_; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | === Constants == |
| 63 | Named constants (including enumeration values) must be all uppercase using underscore to separate words. Always use constants instead of numbers. |
| 64 | {{{ |
| 65 | const int MAX_ITERATIONS = 5; |
| 66 | |
| 67 | for (int i = 0; i < MAX_ITERATIONS; i++) {} // Instead of (... i < 5; ...) |
| 68 | }}} |
| 69 | |
| 70 | === Functions === |
| 71 | Names representing methods or functions must be verbs and written in mixed case starting with lower case. |
| 72 | {{{ |
| 73 | void calculateTheLowerBoundary() {} |
| 74 | int howManyItemsLeft() {} |
| 75 | }}} |
| 76 | For interface functions to local member variables use short functions: |
| 77 | {{{ |
| 78 | class WorldEntity { |
| 79 | public: |
| 80 | inline string Name() { |
| 81 | return name_; |
| 82 | } |
| 83 | |
| 84 | inline void setName(const string& name) { |
| 85 | name_ = name; |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | string name_; |
| 90 | }; |
| 91 | }}} |
| 92 | |
| 93 | |
| 94 | |
| 95 | == Indentation == |
| 96 | === Spaces === |
| 97 | Use 2 spaces for an indentation level. Never use tabs, as it is common in windows IDEs. |
| 98 | |
| 99 | == Special Implementations == |
| 100 | === Use STL Libraries === |
| 101 | Use STL (standard template library) implementations of common abstract data structures like lists, arrays, stacks, queues, never implement your own! |