Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/coding_standards.txt

1Coding Standard rev. 0 (First Draft)
2
31. Indentation
4 having seen most indentation styles going from 2 to 8 spaces, I would suggest a indentation of 4 spaces.
5
62. Comments
7I see here two main differents cases:
8function description comments and one-line code quite comments
9
10For functions documentation, I suggest to use this syntax
11/**
12 *
13 */
14Note the use of /** that will make future html auto-documentation easier (i.e: Doxygen at least recognize this marker)
15
16for punctual, short code comment, let's use:
17//
183) #define at top of document
194) Global vars right below #include / #define (notation: gLobal)
20Note that global vars and static vars should be avoided as much as possible in favor of local variables use, get/set functions (properties).
21
225) No curly brackets for single lines
23
246) else
25{
26 ....
27}
28
29instead of:
30
31else {
32 ....
33}
34
357) if
36{
37 ....
38}
39instead of:
40
41if {
42 ....
43}
44
458) fall through code (using indention) or bail out early (using returns)?
46Using early bail out for preconditions early in the function code,
47use common sense to avoid as an example more than 4 imbricated if() constructions.
48In the later case, consider decomposing your function in more manageable primitives.
49
509) Spaces/readability i.e. not: if (fd<0)
51but: if (fd < 0)
52
5310. types, variables, functions, naming
54non const variables should follow the (currently mostly used) CamelCase convention:
55int myVariableIsFine;
56instead of :
57int my_variable_is_ok;
58
59Functions should follow the same conventions except for standard c lib related functions.
60Types should share the same convention but start with a Captial letter instead of lower case.
61
6211. Please make sure you extensively initialize variables:
63avoid as much as possible:
64int myVar
65...
66myVar = 10;
67
68but use instead:
69int myVar = 10;
70
7112. const values:
72const int MY_CONST_VARIABLE=42; is also ok for me, depending on the context of use.
73or
74const int MyConstVariable = 42; (with a Capital first letter)
75
7613. macro definitions should follow this convention:
77#define MY_HANDY_MACROS_PSEUDO_FUNC() ...
78
7914. Macros use should be limited to really special cases where they bring real value (like special optimization cases)
80Most of the time inlining a function is much better than the use of macros
81
8215. Don't optimize your code blindly, always favor readability when in doubt.
83Very often, optimization is not necessary where you think it is, think about the bubble sort algorithm, where people would code it in assembly, where a heap or quick sort algorithm would be much more efficient (n log(n) instead of quadratic complexity), as an example when values count to be sorted get high.
84
8516. To avoid duplicate symbols, set your variable as static and implement a setter/getter
86
8717. LIBSA SHOULDN'T CALL FUNCTION (OR INCLUDE HEADERS) OF LIBSAIO, AS WELL AS LIBSAIO SHOULDN'T CALL FUNCTIONS (OR INCLUDE HEADERS) OF BOOT2
88
8918. Always declare your functions (static , implicit-extern or explicit-extern)

Archive Download this file

Revision: 2839