1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <META http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
5 <TITLE>Gdcm Developpers</TITLE>
10 <!#######################################################################>
11 <H1>Gdcm coding style (and other religious/agnostic beliefs)</H1>
12 <HR size="1"><ADDRESS style="align: right;"></ADDRESS>
16 The following coding style intends to ease the work of developpers
17 themselves but also of users who will study, maintain, fix, and extend
18 the code. Any bread crumbs that you can drop in the way of explanatory
19 names and comments will go a long way towards helping other readers and
21 Keep in mind that to a large extent the structure of code directly
22 expresses its implementation.
25 - C++ (for the kernel) and Python (for the wrappers).
26 - all the names (variables, members, methods, functions) and comments
27 should be based on English. Documentation, guides, web site and other
28 informations should be in English.
29 Make sure you use correct (basic) English and complete, grammatically
30 correct sentences for comments and documentation.
33 - Each line of code should take no more than 79 characters. Break the code
34 across multiple lines as necessary.
35 - Methods and functions should keep a reasonable number of lines when
36 possible (a typical editor displays 50 lines). Avoid code duplication.
37 Always prefer creating a new method or function to duplication.
38 A high indentation level generally suggests the need for a new
40 - All the code should be properly indented. The appropriate indentation
41 level is three spaces for each level of indentation. DO NOT USE TABS.
42 Set up your editor to insert spaces. Using tabs may look good in your
43 editor but will wreak havoc in others, or in external tools (e.g. side
45 - The declaration of variables within classes, methods, and functions
46 should be one declaration per line. Provide them with default values
47 and don't rely on compilers for default initialization.
51 In general, names are constructued by using case change to indicate
52 separate words, as in ImageDataSize (standing for "image data size").
53 Underscores are not used. Variable names are chosen carefully with the
54 intention to convey the meaning behind the code. Names are generally
55 spelled out; use of abbreviations is discouraged.
56 [Note: abbreviation are allowable when in common use, and should be in
57 uppercase as in LUT or RGBA.]
58 While this does result in long names, it self-documents the code.
60 Files should have the same name as the class, with a "gdcm" prepended.
61 Header files are named .h, while implementation files are named either
62 .cxx or .txx, depending on whether they are implementations of templated
63 classes. For example, the class gdcm::Document is declared and defined
64 in the files gdcmDocument.h and gdcmDocument.cxx.
65 - Naming Class Data Members, Methods, and Functions:
66 Class data members are named beginning with a capital letter as in
68 Global functions and class methods, either static or class members, are
69 named beginning with a capital letter, as in GetImageDataSize().
70 - Naming Local Variables:
71 Local variables begin in lowercase. There is more flexibility in the
72 naming of local variables although they still should convey some
76 - Don't use the inline keyword when defining an inline function
77 within a class definition.
78 - As stated in the "Naming conventions" section, class data members
79 are named beginning with a capital letter as in GroupPixel.
80 But the parameter names of method should be named with a lowercase
81 letter (in order to distinguish at a glance data members, from parameters
82 and also to avoid potential collisions with data members):
83 void A::SetGroupPixel( int groupPixel )
85 GroupPixel = groupPixel;
87 - Do not repeat the virtual keyword when overriding virtual base methods
88 in declaration of subclasses:
91 virtual void foo(...);
95 void foo(...); // and not: virtual void foo(...);
97 - The public, protected, private declarations should be at the
98 same indent level as the class. Use
106 - Method and functions devoided of arguments should not use the void
108 SomeType Header::GetPixelData()
110 SomeType Header::GetPixelData(void).
113 - Braces must be used to delimit the scope of an if, for, while, switch, or
114 other control structure. Braces are placed on a line by themselves, and
115 at the same indentation level as the control structure to which they
117 for (i=0; * i<3; i++)
126 else if ( other condition )
134 You can choose to use braces on a line with a code block when
135 the block consists of a single line:
136 if ( condition ) { foo=1; }
137 else if ( condition2 ) { foo=3; }
140 for (i=0; i<3; ++i) {x[i]=0.0;}
141 Methods and functions should follow the same usage of braces:
142 void File::ParsePixelData()
148 - Avoid code mixed with comments on a single line. Instead, prepend the
149 logical blocks of code with the concerned comments.
150 - Use parantheses around conditions e.g. with an if statement:
151 if ( someLocalVariable == 2 ) { ... }
152 - Add spaces around parantheses, or braces. Use
153 if ( someLocalVariable == 2 ) { ClassMenber += 1; }
155 if (someLocalVariable == 2) {ClassMenber += 1;}
156 - Add spaces around each side of the assignement operator, and
157 around binary operators used in boolean expression. Use
158 someLocalVariable = ClassMember * 2;
159 if ( someLocalVariable == 2 || ClassMember == 2 ) ...
161 someLocalVariable=ClassMember*2;
162 if ( someLocalVariable==2||ClassMember==2 ) ...
165 - Don't use underscores. Don't use tabs. Don't use control characters
166 like ^M. Anyhow, cvs is configured to reject such commits.
167 - Comments should be in C++ style ("// ...", two slashes, per line). Don't
168 use C style comments ("/* ... */").
169 - The last line of a file should terminate with "\n".
170 - Returned arguments of methods and functions should not be wrapped with
174 return ( iter->second );
176 * Debugging and Verbose modes:
177 Never use std::cout. Instead use the gdcmDebug class through the
178 global instance dbg. Example:
179 #include "gdcmDebug.h"
182 dbg.Verbose(3, "Local function name: entering.");
185 will send the message to std::cout when dbg.Debug() is called
189 The Doxygen open-source system is used to generate on-line documentation.
190 Doxygen requires the embedding of simple comments in the code which is in
191 turn extracted and formatted into documentation. See
192 http://www.stack.nl/ dimitri/doxygen/
193 for more information about Doxygen.
194 - Documenting a class:
195 Classes should be documented using the class and brief doxygen commands,
196 followed by the detailed class description:
199 * \brief Header acts as container of Dicom elements of an image.
201 * Detailed description of the class is provided here
204 The key here is that the comment starts with /**, each subsequent line has
205 an aligned *, and the comment block terminates with a */.
206 - Documenting class members and inline methods:
207 All the members and the inline methods should be documented within
208 the class declaration as shown in the following example:
211 /// True when parsing was succesfull. False otherwise.
212 bool Readable = false;
214 /// \brief The number of lines of the image as interpreted from
215 /// the various elements encountered at header parsing.
216 int NumberOfLines = -1;
218 /// Predicate implemented as accessor around \ref Readable.
219 bool IsReadable() { return Readable; }
221 - Documenting a Method:
222 Methods should be documented using the following comment block style
223 as shown in the following example:
226 * \brief Within the Dicom Elements (parsed with the public and private
227 * dictionaries), look for the element value representation of
229 * @param group Group number of the searched tag.
230 * @param element Element number of the searched tag.
231 * @return Corresponding element value representation when it exists,
232 * and the string "gdcm::Unfound" otherwise.
234 std::string Document::GetEntryByNumber(guint16 group, guint16 element)
239 * External includes and C style:
240 - Only the C++ standard library and the STL includes should be used.
241 When including don't use the .h extension (use #include <iostream>
242 instead of #include <iostream.h>).
243 Note: include the stl header AFTER the gdcm ones (otherwise pragma
244 warnings won't work).
245 - Don't use the C standard library. Don't include stdio.h, ctype.h...
246 Don't use printf(), sprinf(), FILE*...
247 - Don't use the NULL notation (either as macro, or as const int NULL=0).
248 A pointer that doesn't refer to an object should simply be defined as
249 DataPointer* MyDataPointer = 0;
252 - Assume T is a given type. When declaring or defining with the
253 "pointer to T" notation, the * character must be adjacent to
254 the type and not the variable. That is use
258 - Always define a typedef for a new type and be consistent in usage.
260 typedef Header* HeaderPointer;
261 HeaderPointer MyHeaderPointer;
262 - One notorious counter example for non using C style inclusion concerns
263 exact-width integers (since there seem to be no equivalent for C++).
264 When using exact-width integers use the typedef names defined by
265 the Basic ISO C99: 7.18 Integer types i.e.
266 int8_t int16_t int32_t int64_t (signed integers)
268 uint8_t uint16_t uint32_t uint64_t (unsigned integers).
269 Conversion table is then:
270 unsigned char -> uint8_t;
271 unsigned short -> uint16_t;
272 unsigned int -> uint32_t;
273 unsigned long -> uint32_t;
274 unsigned long long -> uint64_t;
275 (signed) char -> int8_t;
279 long long -> int64_t;
280 Hence do not use declarations like "unsigned int".
281 With g++, accessing those typedef is achieved by the following
282 #include < stdint.h >
286 <!#######################################################################>
287 <HR size="1"><ADDRESS style="align: right;"></ADDRESS>