Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 3 and Version 4 of code/C++_styleguide


Ignore:
Timestamp:
Oct 30, 2007, 4:09:28 PM (17 years ago)
Author:
patrick
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/C++_styleguide

    v3 v4  
    11
    2 = Styleguide for C++ =
     2= Style Guide for Orxonox C++ Code =
    33
    4 == Language Specification ==
     4'Coding style' refers to the way source code is formatted. For C++, this involves things like brace placement, indentation, and the way parentheses are used. The most important thing is for the code to be consistent within a program or library - code with sloppy formatting is not acceptable, since it is hard to read.
    55
     6When writing a new program or library, please follow a consistent style of brace placement and indentation. We recommend the following code style.
     7
     8Remember that any violation to the guide of course is allowed if it enhances readability.
     9
     10== File Organization ==
     11=== Header and Source ===
     12Keep 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{{{
     14file names:
     15my_example_class.cc
     16my_example_class.h
     17}}}
     18
     19=== Namespaces ===
     20Create a directory for every namespace (== create a directory for every modules and all its submodules).
     21
     22=== Inlineing and Templates ===
     23Inline 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 ===
     26Place 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 ===
     31Every 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 ===
     38Names representing types must be in mixed case starting with upper case.
     39{{{
     40class SavingsAccount {
     41};
     42
     43struct SimpleStruct {
     44};
     45}}}
     46
     47=== Variables ===
     48Variable names must be in mixed case starting with lower case.
     49{{{
     50string testString;
     51int numberOfTimes;
     52}}}
     53Memeber variables of classes all end with an underline:
     54{{{
     55class TestClass {
     56
     57private:
     58  int numberOfTimes_;
     59};
     60
     61
     62=== Constants ==
     63Named constants (including enumeration values) must be all uppercase using underscore to separate words. Always use constants instead of numbers.
     64{{{
     65const int MAX_ITERATIONS = 5;
     66
     67for (int i = 0; i < MAX_ITERATIONS; i++) {} // Instead of (... i < 5; ...)
     68}}}
     69
     70=== Functions ===
     71Names representing methods or functions must be verbs and written in mixed case starting with lower case.
     72{{{
     73void calculateTheLowerBoundary() {}
     74int howManyItemsLeft() {}
     75}}}
     76For interface functions to local member variables use short functions:
     77{{{
     78class WorldEntity {
     79public:
     80  inline string Name() {
     81    return name_;
     82  }
     83
     84  inline void setName(const string& name) {
     85    name_ = name;
     86  }
     87
     88private:
     89  string name_;
     90};
     91}}}
     92
     93
     94
     95== Indentation ==
     96=== Spaces ===
     97Use 2 spaces for an indentation level. Never use tabs, as it is common in windows IDEs.
     98
     99== Special Implementations ==
     100=== Use STL Libraries ===
     101Use STL (standard template library) implementations of common abstract data structures like lists, arrays, stacks, queues, never implement your own!
    6102
    7103== General Topics ==