diff options
Diffstat (limited to 'doc/Coding_style')
-rw-r--r-- | doc/Coding_style | 247 |
1 files changed, 213 insertions, 34 deletions
diff --git a/doc/Coding_style b/doc/Coding_style index d0eba0d..0c10170 100644 --- a/doc/Coding_style +++ b/doc/Coding_style | |||
@@ -1,45 +1,224 @@ | |||
1 | The coding style is almost the same as i blackbox. | 1 | Use hard tab for indentation. (size 4) |
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, 3, 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: |
17 | if (...) { | ||
18 | function(....., | ||
19 | ......, .... ); | ||
20 | } | ||
21 | 21 | ||
22 | if ( stuff ) { | 22 | This is ok: |
23 | function(stuff, more stuff, | 23 | if (...) |
24 | more, even more); | 24 | shortline(...); |
25 | } else { | 25 | |
26 | morefunction( stuff, more stuff | 26 | |
27 | stuff, | 27 | while-statement: |
28 | stuff, | 28 | |
29 | stuff); | 29 | while (...) { |
30 | } | 30 | .... |
31 | 31 | } | |
32 | The includeguards: | 32 | |
33 | _FILENAME_HH_ | 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: | ||
34 | 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 | goto-statement: | ||
77 | DONT USE IT! | ||
78 | |||
79 | |||
80 | Include guards: | ||
81 | For files with namespace: | ||
82 | #ifndef NAMESPACE_FILENAME_HH | ||
83 | #define NAMESPACE_FILENAME_HH | ||
84 | .... | ||
85 | |||
86 | #endif //NAMESPACE_FILENAME_HH | ||
87 | <eof> | ||
88 | |||
89 | Without namespace: | ||
90 | #ifndef FILENAME_HH | ||
91 | #define FILENAME_HH | ||
92 | .... | ||
93 | |||
94 | #endif //FILENAME_HH | ||
95 | |||
96 | <eof> | ||
97 | |||
98 | preprocessors: | ||
99 | The # of all preprocessor commands must always be in column 1, and never use | ||
100 | indentation for preprocessor directives | ||
101 | |||
102 | They should always have a // PREPROCESSOR to mark where they end | ||
103 | #ifdef DEBUG | ||
104 | ... | ||
105 | ... | ||
106 | #endif //DEBUG | ||
107 | |||
108 | Don't use preprocessors for constants or macro-functions, they can be | ||
109 | cryptic and sometime make it hard to debug. | ||
110 | |||
111 | |||
112 | functions: | ||
113 | The name starts with a lowercase and then a uppercase for name separation: | ||
114 | void functionWithAName(...) { | ||
115 | ...; | ||
116 | } | ||
35 | 117 | ||
36 | Function comments: | 118 | Function comments: |
37 | //------------ function name -------- | ||
38 | // This do that and that | 119 | // This do that and that |
39 | // Returns this on success else | 120 | // Returns this on success else |
40 | // this on failure. | 121 | // this on failure. |
41 | // TODO: if there is something to do. | 122 | // TODO: if there is something to do. |
42 | //----------------------------------- | 123 | void functionDoes(...) { |
43 | type classname::function(...) { | ||
44 | 124 | ||
45 | } | 125 | } |
126 | Comments: | ||
127 | Use // on few line comments. | ||
128 | Use | ||
129 | /* | ||
130 | ... | ||
131 | ... | ||
132 | */ | ||
133 | when there are a lot to comment | ||
134 | |||
135 | Class: | ||
136 | Order: public, protected and then private | ||
137 | Class names always starts with a big letter. | ||
138 | Class data members are prefixed by m_ , static data members are prefixed with s_ . | ||
139 | Class member function will be organized according to creator, | ||
140 | manipulator and accessors categories. | ||
141 | |||
142 | class Classname:public AnotherClass { | ||
143 | public: | ||
144 | //1. public enums, structs | ||
145 | |||
146 | //2. constructors and destructor | ||
147 | |||
148 | //3. manipulators | ||
149 | |||
150 | //4. accessors | ||
151 | |||
152 | protected: | ||
153 | //1. enums, structs | ||
154 | |||
155 | //2. functions | ||
156 | |||
157 | //3. variables | ||
158 | |||
159 | private: | ||
160 | //1. enums, structs | ||
161 | |||
162 | //2. functions | ||
163 | |||
164 | //3. variables | ||
165 | }; | ||
166 | |||
167 | |||
168 | struct follows the class style. | ||
169 | |||
170 | namespace: | ||
171 | namespace TheName { | ||
172 | ...; | ||
173 | ...; | ||
174 | }; //end namespace TheName | ||
175 | |||
176 | Don't use "using namespace thenamespace;" in header-file | ||
177 | We don't want to force the other files, that include the file, a namespace. | ||
178 | |||
179 | |||
180 | try/catch-statement: | ||
181 | |||
182 | try { | ||
183 | ....; | ||
184 | } catch (...) { | ||
185 | ....; | ||
186 | } | ||
187 | |||
188 | Variables: | ||
189 | Avoid variables that contain mixtures of the numbers 0 & l and the letters O | ||
190 | and 1, because they are hard to tell apart. | ||
191 | Having a type name and a variable differing in only in case (such as: | ||
192 | String string;) is permitted, but discouraged. | ||
193 | |||
194 | Use lowercase for variables and use _ to separate names, ex: | ||
195 | int number_of_screens; | ||
196 | int num_colors; | ||
197 | |||
198 | |||
199 | All constants must be in Upper case letters. | ||
200 | |||
201 | enums must be in uppercase letters and not in file scope: | ||
202 | enum {WHITE, RED, BLUE}; | ||
203 | |||
204 | Other: | ||
205 | |||
206 | if (strcmp(...) == 0) //good | ||
207 | if (!strcmp()) //bad | ||
208 | |||
209 | Don't create free-functions, encapsulate them in a namespace or a class | ||
210 | and name filenames so it's clear what they hold so it's easier to find | ||
211 | functions, classes and other stuff. | ||
212 | |||
213 | |||
214 | ChangeLog format: | ||
215 | *year/month/day: | ||
216 | * whats changed (who changed it) | ||
217 | which file | ||
218 | |||
219 | ex: | ||
220 | |||
221 | *02/01/01: | ||
222 | * Fixed bug workspace change (TheDude) | ||
223 | Workspace.cc | ||
224 | |||