1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <meta http-equiv="Content-Type"
4 content="text/html;charset=iso-8859-1"><title>bbtk Developpers</title></head>
8 <!--#######################################################################-->
9 <h1>bbtk coding style (and other religious/agnostic beliefs)</h1>
10 <hr size="1"><address style=""></address>
13 The following coding style intends to ease the work of developpers
14 themselves but also of users who will study, maintain, fix, and extend
15 the code. Any bread crumbs that you can drop in the way of explanatory
16 names and comments will go a long way towards helping other readers and
18 Keep in mind that to a large extent the structure of code directly
19 expresses its implementation.
22 - C++ (for the kernel) and Python (for the wrappers).
23 - all the names (variables, members, methods, functions) and comments
24 should be based on English. Documentation, guides, web site and other
25 informations should be in English.
26 Make sure you use correct (basic) English and complete, grammatically
27 correct sentences for comments and documentation.
30 - Each line of code should take no more than 79 characters. Break the code
31 across multiple lines as necessary.
32 - Methods and functions should keep a reasonable number of lines when
33 possible (a typical editor displays 50 lines). Avoid code duplication.
34 Always prefer creating a new method or function to duplication.
35 A high indentation level generally suggests the need for a new
37 - All the code should be properly indented. The appropriate indentation
38 level is TWO spaces for each level of indentation. DO NOT USE TABS.
39 Set up your editor to insert spaces. Using tabs may look good in your
40 editor but will wreak havoc in others, or in external tools (e.g. side
42 - The declaration of variables within classes, methods, and functions
43 should be one declaration per line. Provide them with default values
44 and don't rely on compilers for default initialization.
48 In general, names are constructed by using case change to indicate
49 separate words, as in ImageDataSize (standing for "image data size").
50 Underscores are not used. Variable names are choosen carefully with the
51 intention to convey the meaning behind the code. Names are generally
52 spelled out; use of abbreviations is discouraged.
53 [Note: abbreviation are allowable when in common use, and should be in
54 uppercase as in LUT or RGBA.]
55 While this does result in long names, it self-documents the code.
57 Files should have the same name as the class, with a "bbtk" prepended.
58 Header files are named .h, while implementation files are named either
59 .cxx or .txx, depending on whether they are implementations of templated
60 classes. For example, the class bbtk::BlackBox is declared and defined
61 in the files bbtkBlackBox.h and bbtkBlackBox.cxx.
62 - Naming Class Data Members, Methods, and Functions:
63 Class data members (aka Attributes) are named beginning with a lower case 'm'
64 (m as 'member'!) followed by capital letter as in mGroupPixel, in order not
65 be confused with methods names.
66 Global functions and class methods, either static or class members, are
67 named beginning with a capital letter, as in GetImageDataSize().
68 - Naming Local Variables:
69 Local variables begin in lowercase. There is more flexibility in the
70 naming of local variables although they still should convey some
72 - Naming function parameters:
73 Function parameters begin in lowercase. There is more flexibility in the
74 naming of function parameters although they still should convey some
78 - Don't use the inline keyword when defining an inline function
79 within a class definition.
80 (Any method defined within a .h file is ipso facto considered as 'inline'.
81 Dont write useless stuff!)
82 - As stated in the "Naming conventions" section, class data members
83 named beginning with a lower case 'm' followed by capital letter
85 But the parameter names of method should be named with a lowercase
86 letter (in order to distinguish at a glance data members, from parameters
87 and also to avoid potential collisions with data members):
88 void A::SetGroupPixel( int groupPixel )
90 mGroupPixel = groupPixel;
92 - Don't use trailing ';' in inline function definition.
94 void A::SetGroupPixel( int groupPixel ){mGroupPixel = groupPixel;}
96 void A::SetGroupPixel( int groupPixel ){mGroupPixel = groupPixel;};
97 - Do not repeat the 'virtual' keyword when overriding virtual base methods
98 in declaration of subclasses:
101 virtual void foo(...);
103 class B : public bbtk::A
105 void foo(...); // and NOT: virtual void foo(...);
108 (when the keyword 'virtual' is used at the parent level, it's propagated
109 for all the child classes)
111 - In declaration of subclasses, always preprend the class name with 'bbtk::'
112 in order not to confuse Umbrello ( OpenSource UML diagram generator)
117 class B : public bbtk::A // and NOT: class B: public A
121 - The public, protected, private declarations should be at the
122 same indent level as the class. Use :
133 - The Data members should be declared at the end of the class declaration :
148 int mProtectedCounter;
153 - Method and functions devoided of arguments should not use the void
155 SomeType Header::GetPixelData()
157 SomeType Header::GetPixelData(void)
160 - Braces must be used to delimit the scope of an if, for, while, switch, or
161 other control structure. Braces are placed on a line by themselves, and
162 at the same indentation level as the control structure to which they
164 for (i=0; * i<3; i++)
169 for (i=0; * i<3; i++) {
177 else if ( other condition )
188 } else if ( other condition ) {
193 You can choose to use braces on a line with a code block when
194 the block consists of a single line:
195 if ( condition ) { foo=1; }
196 else if ( condition2 ) { foo=3; }
199 for (i=0; i<3; ++i) {x[i]=0.0;}
200 Methods and functions should follow the same usage of braces:
201 void File::ParsePixelData()
207 - Avoid code mixed with comments on a single line. Instead, prepend the
208 logical blocks of code with the concerned comments.
209 - Use parentheses around conditions e.g. with an if statement:
210 if ( someLocalVariable == 2 ) { ... }
211 - Add spaces around parentheses, or braces. Use
212 if ( someLocalVariable == 2 ) { mClassMember += 1; }
214 if (someLocalVariable == 2) {mClassMember += 1;}
215 - Add spaces around each side of the assignement operator, and
216 around binary operators used in boolean expression. Use
217 someLocalVariable = mClassMember * 2;
218 if ( someLocalVariable == 2 || mClassMember == 2 ) ...
220 someLocalVariable=mClassMember*2;
221 if ( someLocalVariable==2||mClassMember==2 ) ...
224 - Don't use underscores. Don't use tabs. Don't use control characters
225 like ^M. Anyhow, cvs is configured to reject such commits.
226 - Comments should be in C++ style ("// ...", two slashes, per line). Don't
227 use C style comments ("/* ... */").
228 - The last line of a file should terminate with "\n".
229 - Returned arguments of methods and functions should not be wrapped with
231 return iter->second;
233 return ( iter->second );
235 * Debugging and Verbose modes:
236 Never use std::cout. Instead use the bbtkMessage, bbtkDebugMessage, bbtkWarning or bbtkError macros and their variants. Example:
237 #include "bbtkMessageManager.h"
240 bbtkDebugMessageInc("MyClass",9,"Local function name: entering.");
242 bbtkDecTab("MyClass",9);
244 will send the message to std::cout when the Debug messages are compiled
245 and the Message Level for the category of messages "MyClass" is greater than 9.
248 The Doxygen open-source system is used to generate on-line documentation.
249 Doxygen requires the embedding of simple comments in the code which is in
250 turn extracted and formatted into documentation. See :
251 http://www.stack.nl/~dimitri/doxygen/
252 for more information about Doxygen.
253 - Documenting a class:
254 Classes should be documented using the class and brief doxygen commands,
255 followed by the detailed class description:
258 * \brief Header acts as container of Dicom elements of an image.
260 * Detailed description of the class is provided here
263 The key here is that the comment starts with /**, each subsequent line has
264 an aligned *, and the comment block terminates with a */.
265 - Documenting class members and inline methods:
266 All the members and the inline methods should be documented within
267 the class declaration ( .h file) as shown in the following example:
270 /// True when parsing was successfull. False otherwise.
271 bool mReadable = false;
273 /// \brief The number of lines of the image as interpreted from
274 /// the various elements encountered at header parsing.
275 int mNumberOfLines = -1;
277 /// Predicate implemented as accessor around \ref mReadable.
278 bool IsReadable() { return mReadable; }
280 - Documenting a Method:
281 Methods should be documented using the following comment block style
282 as shown in the following example:
285 * \brief Within the Dicom Elements (parsed with the public and private
286 * dictionaries), look for the element value representation of
288 * @param group Group number of the searched tag.
289 * @param elem Element number of the searched tag.
290 * @return Corresponding element value representation when it exists,
291 * and the string "bbtk::Unfound" otherwise.
293 std::string Document::GetEntryByNumber(guint16 group, guint16 elem)
298 * External includes and C style:
299 - Only the C++ standard library and the STL includes should be used.
300 When including don't use the .h extension (use #include <iostream>
301 instead of #include <iostream.h>).
302 Note: include the stl header AFTER the bbtk ones (otherwise pragma
303 warnings won't work).
304 - Don't use the C standard library. Don't include stdio.h, ctype.h...
305 Don't use printf(), sprinf(), FILE*...
306 - Don't use the NULL notation (neither as macro, nor as const int NULL=0).
307 A pointer that doesn't refer to an object should simply be defined as
308 DataPointer *myDataPointer = 0;
311 - Assume T is a given type. When declaring or defining with the
312 "pointer to T" notation, the * character must be adjacent to
313 the variable and not the type. That is use
319 - Assume T is a given type. When declaring or defining with the
320 "reference to T" notation, the & character must be adjacent to
321 the variable and not the type. That is use :
326 (Doxygen will not have any longer to correct)
328 - Always define a typedef for a new type and be consistent in usage.
330 typedef Header *HeaderPointer;
331 HeaderPointer myHeaderPointer;
333 - One notorious counter example for non using C style inclusion concerns
334 exact-width integers (since there seem to be no equivalent for C++).
335 When using exact-width integers use the typedef names defined by
336 the Basic ISO C99: 7.18 Integer types i.e.
337 int8_t int16_t int32_t int64_t (signed integers)
339 uint8_t uint16_t uint32_t uint64_t (unsigned integers).
340 Conversion table is then:
341 unsigned char -> uint8_t;
342 unsigned short -> uint16_t;
343 unsigned int -> uint32_t;
344 unsigned long -> uint32_t;
345 unsigned long long -> uint64_t;
346 (signed) char -> int8_t;
350 long long -> int64_t;
351 Hence do not use declarations like "unsigned int".
352 With g++, accessing those typedef is achieved by the following
353 #include < stdint.h >
354 </iostream.h></iostream></pre>
357 <!--#######################################################################-->
358 <hr size="1"><address style=""></address>