Changes between Version 2 and Version 3 of CodingStyle
- Timestamp:
- Dec 22, 2007, 5:21:09 PM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CodingStyle
v2 v3 1 1 = BOINC coding style = 2 2 3 == 3 == All languages == 4 4 5 === 5 === Code factoring === 6 6 7 * If code is repeated, factor it out and make it into a function 8 * If a function becomes longer than 100 lines or so, split it up 7 * If code is repeated, factor it out and make it into a function. 8 * If a function becomes longer than 100 lines or so, split it up. 9 9 * If a file is becoming 'landfill', split it up. 10 * C++ .h files often contain both interface and implementation. 10 * C++ .h files often contain both interface and implementation. Clearly divide these. 11 11 12 === 12 === Code documentation === 13 13 14 14 * .C files have a comment at the top saying what's in the file (and perhaps what isn't). … … 16 16 * Structs and classes are preceded by a comment saying what they are. 17 17 18 === 18 === Naming === 19 19 20 * Names should be descriptive without being verbose (local variables names may be short) 21 * Class and type names, and #defined symbols, are all upper case, 20 * Names should be descriptive without being verbose (local variables names may be short). 21 * Class and type names, and #defined symbols, are all upper case, with underscores to separate words. 22 22 * Variable and function names are all lower case, with underscores to separate words. 23 * No mixed case names 23 * No mixed case names. 24 24 25 25 === Indentation === 26 26 27 27 * Each level of indentation is 4 spaces (not a tab). 28 * multi-line function call:28 * Multi-line function call: 29 29 {{{ 30 30 func( … … 33 33 ); 34 34 }}} 35 * switch statements: case labels are at same indent level as switch35 * {{{switch}}} statements: {{{case}}} labels are at same indent level as {{{}}}switch: 36 36 {{{ 37 37 switch (foo) { … … 61 61 } 62 62 }}} 63 * always use curly braces on multi-line if statements63 * Always use curly braces on multi-line `if` statements. 64 64 {{{ 65 65 if (foo) 66 66 return blah; // WRONG 67 67 }}} 68 * 1-line if() statments are OK:68 * 1-line `if()` statements are OK: 69 69 {{{ 70 70 if (foo) return blah; … … 86 86 === Includes === 87 87 88 * A .C file should have the minimum set of #includes to get that particular file to compile (e.g. the includes needed by foo.C should be in foo.C, not foo.h)89 * Includes should go from general (<stdio.h>) to specific (thisfile.h)88 * A .C file should have the minimum set of #includes to get that particular file to compile (e.g. the includes needed by {{{foo.C}}} should be in {{{foo.C}}}, not {{{foo.h}}}). 89 * Includes should be ordered from general (`<stdio.h>`) to specific (`thisfile.h`). 90 90 91 91 === Extern declarations === 92 92 93 foo.h should have 'extern' declarations for all public functions and variables in foo.C There should be no 'extern' statements in .Cfiles.93 {{{foo.h}}} should have '{{{extern}}}' declarations for all public functions and variables in {{{foo.C}}} There should be no '{{{extern}}}' statements in {{{.C}}} files. 94 94 95 95 === Use of static === 96 96 97 * if a function or variable is used in only one file, declare it static.97 * If a function or variable is used in only one file, declare it {{{static}}}. 98 98 99 99 === Things to avoid unless there's a truly compelling reason: === 100 100 101 * inline functions102 * operator or function overloading103 * templates101 * Inline functions. 102 * Operator or function overloading. 103 * Templates. 104 104 105 105 === Things to avoid === 106 106 107 * use typedef (not #define) to define types108 * don't use memset() or memcpy() to initialize or copy classesthat are non-C compatible. Write a default constructor and a copy constructor.107 * Use {{{typedef}}} (not {{{#define}}}) to define types. 108 * Don't use {{{memset()}}} or {{{memcpy()}}} to initialize or copy classes that are non-C compatible. Write a default constructor and a copy constructor. 109 109 110 110 === Error codes === 111 111 112 * ( almost) all functions should return an integer error code.Nonzero means error. See lib/errornumbers.h for a list of error codes.112 * (Almost) all functions should return an integer error code. Nonzero means error. See lib/errornumbers.h for a list of error codes. 113 113 * Calls to functions that return an error code should check the code. Generally they should return on error, e.g.: 114 114 {{{