Changes between Initial Version and Version 1 of CodingConventions


Ignore:
Timestamp:
06/03/13 02:03:45 (12 years ago)
Author:
epyon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CodingConventions

    v1 v1  
     1= Code Conventions =
     2[[PageOutline(2-3)]]
     3
     4----
     5
     6== General Principles ==
     7
     8Code must be written in C++11. All code must compile under -Wall/-W4 compiler flag. Code must compile under the most recent version of GCC and most recent version of MSVC.
     9
     10== Definitions ==
     11
     12 * member functions are functions that are associated with classes.
     13 * data members are variables that are part of classes.
     14
     15== Source Files ==
     16
     17=== General Conventions ===
     18
     19 * never use any white spaces or non-ASCII characters in filenames. Also, the following characters are not allowed: & ^ + - @ $ % * ( ) | \ / [ ] { } < > ? ; : , "
     20 * for source code files, every filename should reflect the name of the class defined or implemented in it.
     21 * source files names should be all lowercase.
     22 * if a code standard rule is broken it has to be explicitly documented.
     23 * the files must not contain source code that is not in usable ( use sandbox for that )
     24 * code must never be copy/pasted (literally doubled). If two functionalities have the common parts, they must reuse single code portion -- do not duplicate what was done. If some functionality is already implemented, it must be used. If the interface or the implementation of the functionality does not satisfy a developer he should talk about the enhancements, and must not create yet another version.
     25 * creating own versions of algorithms already present in STL is only allowed if a serious reason exists.
     26 * header files use .hh extension, source files use .cc
     27
     28=== Header files ===
     29 
     30 * write comments! Header file comments are written for the user of the module. They are adequate if the test cases for the module can be drawn up on the basis of them. Class and function comments (including parameters and return values) always go in the header file. This means all comments that are meant to be handled by the automatic class documentation generator program Doxygen.
     31 * add #ifndef NV_(MODULE_)CLASS_NAME_HH  header guard statements in the beginning of a header to protect it against multiple includes from other files.
     32 * write the implementation of a single class in a single file (exceptions can be made sometimes for header-only classes).
     33 * use the same names for function parameters as you have used in the header file.
     34
     35=== Source files ===
     36
     37 * Write comments! Implementation file comments are written for the maintainer of the module. They are adequate if they meet the following requirements:
     38   * They describe class level implementation and clarify all the non-trivial issues.
     39   * Member function comments cover explanations for all the non-trivial implementation code. Other functions must also contain parameter and return value comments.
     40 * Write the implementation of one class in one file.
     41 * Use the same names for function parameters as you have used in the header file.
     42
     43----
     44
     45== Directory structure ==
     46
     47Each component must be placed in separate directory. Includes and source files are placed in separate directory trees.
     48
     49{{{
     50 \ - common files (readme, install)
     51 \doc – documentation (generated by Doxygen)
     52 \nv – include files
     53 \nv\gl – gl module include files
     54 \nv\...
     55 \src – source files
     56 \src\gl – gl module
     57 \src\...
     58 \ref – reference material
     59 \bin - compiled binaries
     60 \tests - test sources
     61 \tests\gl_test - gl_test project
     62}}}
     63
     64----
     65
     66== Language ==
     67
     68=== General conventions ===
     69
     70 * use namespaces
     71 * don't use global variables
     72 * use constants instead of defines where applicable
     73 * pass function parameters as constant references where possible
     74
     75=== Naming prefixes ===
     76
     77Getter and setter methods should start with "get" and "set", the only exception being Boolean getters which can be prefixed with "is". Conversion functions can start with "as".
     78
     79=== Class names ===
     80
     81 * use an underscore ("_") to separate words
     82 * all letters are lowercase (STL/Boost style)
     83
     84=== Functions ===
     85
     86 * parameter names must be mentioned in both declaration and definition. If some parameter is not referenced inside the function, its name must be commented out in the definition, to avoid compilation warnings.
     87 * use a const attribute where appropriate to indicate that a function does not change the state of an object or a parameter which is read-only.
     88 * each variable declaration should be in a separate line.
     89 * use an underscore ("_") to separate words. Do not start a function name with an underscore.
     90
     91=== Member functions ===
     92 
     93 * follow the rules above
     94 * keep const-correctness! If a member doesn't change it's object, mark it as const
     95
     96=== Member data ===
     97
     98 * member data should be at least protected if not private. Provide accessors for data that needs to be public instead of making it public ( this is the only header inlined code that is acceptable as long as it consists of a single line ).
     99
     100=== Pointer and references ===
     101 
     102 * when declaring or defining pointers or references, place a specifier (* or &) next to typename, not next to the variable.
     103
     104=== Macros and other #defines ===
     105
     106 * avoid macros in release code. Exception: Debug and test code often requires precompiler macros. Debug flags must be analyzed and kept only if they are useful and well commented.
     107 * capitalize all the letters of macros.
     108 * use an underscore ("_") to separate words. Do not start a macro name with an underscore.
     109 * avoid #defines. Use real constants instead.
     110 * exceptions include conditional compilation based on architecture/compiler, and the log/debug macros
     111
     112=== Constants ===
     113
     114 * constant names should be upper-case.
     115 * constants should be defined in source files.
     116 * each number ( except trivial ones like 0 and 1 ) should be a constant.
     117
     118=== Enumerated types ===
     119
     120 * enums and their members must have relevant, meaningful and unambiguous names.
     121 * enumerations names should be upper-case and white spaces should be replaced by underscores.
     122
     123=== Global variables ===
     124
     125... are not.
     126
     127=== Control structures ===
     128 
     129 * every switch statement must have a default: clause, at least for detecting unexpected switch expressions. Use logging code or some other handler to catch unexpected switch expressions. Case branches without a break or return statement should be avoided. If one has to be used, the flow through must be explicitly indicated by a comment.
     130 * always use compound statement braces ({}) in all the control flow statements — if() - else, while(), do/while() and for() — even if there were only a single statement or an empty block. Exceptions for this rule may be made for templated code.
     131
     132'''Example:'''
     133{{{
     134#!cpp
     135 if ( my_variable )
     136 {
     137     do_something();
     138 }
     139}}}
     140
     141 * control flow statement goto must not be used
     142
     143=== Spacing ===
     144
     145 * all C++ statements within a block of code should be indented exactly one tab deeper than the enclosing block.
     146 * tabs are used instead of spaces.
     147 * primary operators should be written with no spaces around them.
     148 * unary operators should be written with no spaces between them and their operands. Exceptions: new and delete.
     149 * all other operators should be written with one space on each side of the operator.
     150 
     151'''Example:'''
     152{{{
     153#!cpp
     154 if ( pointer->function() > MAX_VALUE )
     155 {
     156     counter++;
     157     variable = table[a] + offset;
     158 }
     159}}}
     160
     161=== Placement of braces and parentheses ===
     162 * the placement of the "{" and "}" braces must be at the same level of indentation as the control statement. A "{" or "}" is the only character in a given line. Note, that some editor tools can be configured to indent the code according to this rule.
     163 * prefer a maximum of 120 characters per line. Longer lines are hard to read and some editors cannot even handle them properly.
     164 * parentheses should always have a space on the inside.
     165 * there must be no space between a function or macro name and its parenthesized argument(s).
     166 * there must be a single space between a keyword (for, if, etc.) and its parenthesized argument(s).
     167 * parentheses should always be used where there is potential ambiguity.
     168
     169----
     170
     171== Commenting conventions ==
     172
     173=== General commenting conventions ===
     174 
     175 * Doxygen guidelines can be read on the DoxygenConventions page.
     176 * comments are to be written in English.
     177 * comments must be kept up to date.
     178 * comments must be inserted into the code as it is being developed. They must appear with the relevant code.
     179 * avoid commenting a matter in many places.
     180 * avoid repeating code in comments.
     181 * in general, comments should explain why something has been done rather than telling what has been done. However, non-obvious control and data structures should be explained, as well as the programmer's assumptions of the program's state at a certain point, if it's non-obvious.
     182 * use C++ style comments (// Comment)  for internal documentation and use C style block comments (/* Comment */) for doxygen documentation. Exception - private members can be commented using //<
     183
     184=== Special comments ===
     185
     186All special comments need to have a description of what they relate to. The proper format is //<space>KEYWORD<colon><space><description>. The following are widely recognized:
     187
     188 * '''// TODO:''' there is work to be done following this part. Describe what needs to be done.
     189 * '''// REFACTOR:''' following code needs refactoring. Describe what needs refactoring, and optimally a possible solution.
     190 * '''// HACK:''' following code was hacked up quickly, needs refactoring. Describe why a more elegant solution wasn’t used.
     191 * '''// SPITBALL:''' following code was hacked up quickly, refactor ASAP, no comment provided.
     192 * '''// TRICKY:'''  following code is not obvious, or depends on some non-obvious assumptions, do not touch under death penalty. Possibly try to provide an explanation.
     193 * '''// DONOTREMOVE:''' this code may not be used but should not be removed. Description of why it should not be removed is optional.
     194 * '''// WARNING:''' following code makes some non-obvious assumptions. Document them.
     195
     196----
     197
     198== Identifier Names ==
     199
     200 * all variable and identifiers are all lowercase letter.
     201 * macros are always uppercase.
     202 * global variables don't exist.
     203 * stl_style everywhere. In case of abbrevations all letters are lowercase too (xml_node)
     204 * long identifier names almost always (float_data_vector_iterator instead of f_data_viter)
     205 * when parameter name would conflict with class data member, prepend parameter with "a"
     206 * get/set for accessors, no data members should be public
     207 * is/has is acceptable for boolean observers
     208
     209----
     210
     211== Control Structures ==
     212
     213The control structure indenting is as follows:
     214{{{
     215#!cpp
     216 if ( expression )
     217 {
     218   statements;
     219 }
     220 else
     221 {
     222   statements;
     223 }
     224}}}
     225{{{
     226#!cpp
     227 for ( expression; expression; expression )
     228 {
     229   statements;
     230 }
     231}}}
     232{{{
     233#!cpp
     234 do
     235 {
     236   statements;
     237 }
     238 while ( expression );
     239}}}
     240{{{
     241#!cpp
     242 while ( expression )
     243 {
     244   statements;
     245 }
     246}}}
     247{{{
     248#!cpp
     249 switch ( expression )
     250 {
     251   case constant:
     252     statements;
     253     break;
     254   default:
     255     statements;
     256     break;
     257 }
     258}}}
     259{{{
     260#!cpp
     261 switch ( expression )
     262 {
     263   case constant: single_statement; break;
     264   default: single_statement_or_none; break;
     265 }
     266// if return is used, break is not needed
     267}}}