diff options
Diffstat (limited to 'doc/Coding_style')
-rw-r--r-- | doc/Coding_style | 247 |
1 files changed, 208 insertions, 39 deletions
diff --git a/doc/Coding_style b/doc/Coding_style index d0eba0d..47bd173 100644 --- a/doc/Coding_style +++ b/doc/Coding_style | |||
@@ -1,45 +1,214 @@ | |||
1 | The coding style is almost the same as i blackbox. | 1 | Use 4 space indent |
2 | Instead of 2 spaces there is tab. | 2 | Spaces between "," |
3 | It might look strange now in some places, that is because | 3 | ex: 1, 2, a, 4 |
4 | the code hasnt been "translated" to tab 100% yet. | 4 | |
5 | Use a tab size of 2 and you will be fine. | 5 | if/else-statements: |
6 | 6 | An else clause is joined to any preceding close curly brace | |
7 | note if-statement: | 7 | that is part of its if. |
8 | 8 | ||
9 | if ( stuff ) | 9 | if (....) { |
10 | function(stuff, more stuff, | 10 | .... |
11 | more, even more); | 11 | } else { |
12 | else | 12 | .... |
13 | morefunction( stuff, more stuff | 13 | } |
14 | stuff, | 14 | if the line needs to be splited up, right after an if-statement |
15 | stuff, | ||
16 | stuff); | ||
17 | |||
18 | if the functionline needs to be split up, like above, right after a if-statement | ||
19 | use { and }, so its clear when the if-statement ends. | 15 | use { and }, so its clear when the if-statement ends. |
20 | It should look like this | 16 | ex: |
21 | 17 | if (...) { | |
22 | if ( stuff ) { | 18 | function(....., |
23 | function(stuff, more stuff, | 19 | ......, .... ); |
24 | more, even more); | 20 | } |
25 | } else { | 21 | |
26 | morefunction( stuff, more stuff | 22 | This is ok: |
27 | stuff, | 23 | if (...) |
28 | stuff, | 24 | shortline(...); |
29 | stuff); | 25 | |
30 | } | 26 | |
31 | 27 | while-statement: | |
32 | The includeguards: | 28 | |
33 | _FILENAME_HH_ | 29 | while (...) { |
30 | .... | ||
31 | } | ||
32 | |||
33 | for-statement: | ||
34 | |||
35 | for (init; condition; update) { | ||
36 | .... | ||
37 | } | ||
38 | |||
39 | for (longinit; | ||
40 | longcondition; | ||
41 | longupdate ) { | ||
42 | .... | ||
43 | } | ||
44 | alt form: | ||
45 | |||
46 | init; | ||
47 | for (; condition; update) { | ||
48 | .... | ||
49 | } | ||
50 | |||
51 | do-statement: | ||
52 | |||
53 | do { | ||
54 | .... | ||
55 | } while (...); | ||
56 | |||
57 | switch-statement: | ||
58 | should always have a default value. | ||
59 | Enum values is an exception, they should not have a default: , when you add | ||
60 | new values to an enum you might forget to add them to switch statement. | ||
61 | |||
62 | switch (...) { | ||
63 | case ...: | ||
64 | ...; | ||
65 | break; | ||
66 | case ...: { | ||
67 | ...; | ||
68 | } break; | ||
69 | case ...: | ||
70 | ...; | ||
71 | default: | ||
72 | ....; | ||
73 | break; | ||
74 | } | ||
75 | |||
76 | Include guards: | ||
77 | For files with namespace: | ||
78 | #ifndef NAMESPACE_FILENAME_HH | ||
79 | #define NAMESPACE_FILENAME_HH | ||
80 | .... | ||
81 | |||
82 | #endif //NAMESPACE_FILENAME_HH | ||
83 | <eof> | ||
84 | |||
85 | Without namespace: | ||
86 | #ifndef FILENAME_HH | ||
87 | #define FILENAME_HH | ||
88 | .... | ||
89 | |||
90 | #endif //FILENAME_HH | ||
91 | |||
92 | <eof> | ||
93 | |||
94 | preprocessors: | ||
95 | The # of all preprocessor commands must always be in column 1, and never use | ||
96 | indentation for preprocessor directives | ||
97 | |||
98 | They should always have a // PREPROCESSOR to mark where they end | ||
99 | #ifdef DEBUG | ||
100 | ... | ||
101 | ... | ||
102 | #endif //DEBUG | ||
34 | 103 | ||
104 | Don't use preprocessors for constants or macro-functions, they can be | ||
105 | cryptic and sometime make it hard to debug. | ||
35 | 106 | ||
107 | |||
108 | functions: | ||
109 | The name starts with a lowercase and then a uppercase for name separation: | ||
110 | void functionWithAName(...) { | ||
111 | ...; | ||
112 | } | ||
113 | |||
114 | Use Javadoc style for function description (see www.doxygen.org) | ||
36 | Function comments: | 115 | Function comments: |
37 | //------------ function name -------- | 116 | /** |
38 | // This do that and that | 117 | This do that and that |
39 | // Returns this on success else | 118 | @return this on success else this on failure. |
40 | // this on failure. | 119 | TODO: if there is something to do. |
41 | // TODO: if there is something to do. | 120 | */ |
42 | //----------------------------------- | 121 | void functionDoes(...) { |
43 | type classname::function(...) { | 122 | |
123 | } | ||
124 | |||
125 | Class: | ||
126 | Order: public, protected and then private | ||
127 | Class names always starts with a big letter. | ||
128 | Class data members are prefixed by m_ , static data members are prefixed with s_ . | ||
129 | Class member function will be organized according to creator, | ||
130 | manipulator and accessors categories. | ||
131 | |||
132 | class Classname:public AnotherClass { | ||
133 | public: | ||
134 | //1. public enums, structs | ||
135 | |||
136 | //2. constructors and destructor | ||
137 | |||
138 | //3. manipulators | ||
139 | |||
140 | //4. accessors | ||
141 | |||
142 | protected: | ||
143 | //1. enums, structs | ||
144 | |||
145 | //2. functions | ||
146 | |||
147 | //3. variables | ||
148 | |||
149 | private: | ||
150 | //1. enums, structs | ||
151 | |||
152 | //2. functions | ||
153 | |||
154 | //3. variables | ||
155 | }; | ||
156 | |||
157 | |||
158 | struct follows the class style. | ||
159 | |||
160 | namespace: | ||
161 | namespace TheName { | ||
162 | ...; | ||
163 | ...; | ||
164 | }; //end namespace TheName | ||
165 | |||
166 | Don't use "using namespace thenamespace;" in header-file | ||
167 | We don't want to force the other files, that include the file, a namespace. | ||
168 | |||
169 | |||
170 | try/catch-statement: | ||
44 | 171 | ||
172 | try { | ||
173 | ....; | ||
174 | } catch (...) { | ||
175 | ....; | ||
45 | } | 176 | } |
177 | |||
178 | Variables: | ||
179 | Avoid variables that contain mixtures of the numbers 0 & l and the letters O | ||
180 | and 1, because they are hard to tell apart. | ||
181 | Having a type name and a variable differing in only in case (such as: | ||
182 | String string;) is permitted, but discouraged. | ||
183 | |||
184 | Use lowercase for variables and use _ to separate names, ex: | ||
185 | int number_of_screens; | ||
186 | int num_colors; | ||
187 | |||
188 | |||
189 | All constants must be in Upper case letters. | ||
190 | |||
191 | enums must be in uppercase letters and not in file scope: | ||
192 | enum {WHITE, RED, BLUE}; | ||
193 | |||
194 | Other: | ||
195 | |||
196 | if (strcmp(...) == 0) //good | ||
197 | if (!strcmp()) //bad | ||
198 | |||
199 | Don't create free-functions, encapsulate them in a namespace or a class | ||
200 | and name filenames so it's clear what they hold so it's easier to find | ||
201 | functions, classes and other stuff. | ||
202 | |||
203 | |||
204 | ChangeLog format: | ||
205 | *year/month/day: | ||
206 | * whats changed (who changed it) | ||
207 | which file | ||
208 | |||
209 | ex: | ||
210 | |||
211 | *02/01/01: | ||
212 | * Fixed bug workspace change (TheDude) | ||
213 | Workspace.cc | ||
214 | |||