1 Coding conventions in the Samba tree
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 Coding style guidelines are about reducing the number of unnecessary
11 reformatting patches and making things easier for developers to work
13 You don't have to like them or even agree with them, but once put in place
14 we all have to abide by them (or vote to change them). However, coding
15 style should never outweigh coding itself and so the guidelines
16 described here are hopefully easy enough to follow as they are very
17 common and supported by tools and editors.
19 The basic style for C code, also mentioned in prog_guide4.txt, is the Linux kernel
20 coding style (See Documentation/CodingStyle in the kernel source tree). This
21 closely matches what most Samba developers use already anyways, with a few
22 exceptions as mentioned below.
24 The coding style for Python code is documented in PEP8, http://www.python.org/pep/pep8.
25 If you have ever worked on another free software python project, you are probably
26 already familiar with it.
28 But to save you the trouble of reading the Linux kernel style guide, here
31 * Maximum Line Width is 80 Characters
32 The reason is not about people with low-res screens but rather sticking
33 to 80 columns prevents you from easily nesting more than one level of
34 if statements or other code blocks. Use source3/script/count_80_col.pl
35 to check your changes.
37 * Use 8 Space Tabs to Indent
38 No whitespace fillers.
40 * No Trailing Whitespace
41 Use source3/script/strip_trail_ws.pl to clean up your files before
44 * Follow the K&R guidelines. We won't go through all of them here. Do you
45 have a copy of "The C Programming Language" anyways right? You can also use
46 the format_indent.sh script found in source3/script/ if all else fails.
56 Add the follow to your $HOME/.emacs file:
58 (add-hook 'c-mode-hook
61 (c-toggle-auto-state)))
66 (Thanks to SATOH Fumiyasu <fumiyas@osstech.jp> for these hints):
68 For the basic vi editor included with all variants of \*nix, add the
69 following to $HOME/.exrc:
74 For Vim, the following settings in $HOME/.vimrc will also deal with
75 displaying trailing whitespace:
77 if has("syntax") && (&t_Co > 2 || has("gui_running"))
79 function! ActivateInvisibleCharIndicator()
80 syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
81 highlight TrailingSpace ctermbg=Red
83 autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
85 " Show tabs, trailing whitespace, and continued lines visually
86 set list listchars=tab:»·,trail:·,extends:…
88 " highlight overly long lines same as TODOs.
90 autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
93 =========================
94 FAQ & Statement Reference
95 =========================
100 Comments should always use the standard C syntax. C++
101 style comments are not currently allowed.
103 The lines before a comment should be empty. If the comment directly
104 belongs to the following code, there should be no empty line
105 after the comment, except if the comment contains a summary
106 of multiple following code blocks.
114 * This is a multi line comment,
115 * which explains the logical steps we have to do:
117 * 1. We need to set i=5, because...
118 * 2. We need to call complex_fn1
121 /* This is a one line comment about i = 5. */
125 * This is a multi line comment,
126 * explaining the call to complex_fn1()
133 * @brief This is a doxygen comment.
135 * This is a more detailed explanation of
136 * this simple function.
138 * @param[in] param1 The parameter value of the function.
140 * @param[out] result1 The result value of the function.
142 * @return 0 on success and -1 on error.
144 int example(int param1, int *result1);
151 * This is a multi line comment,
152 * which explains the logical steps we have to do:
154 * 1. We need to set i=5, because...
155 * 2. We need to call complex_fn1
157 /* This is a one line comment about i = 5. */
160 * This is a multi line comment,
161 * explaining the call to complex_fn1()
167 /*This is a one line comment.*/
169 /* This is a multi line comment,
170 with some more words...*/
173 * This is a multi line comment,
174 * with some more words...*/
176 Indention & Whitespace & 80 columns
177 -----------------------------------
179 To avoid confusion, indentations have to be tabs with length 8 (not 8
180 ' ' characters). When wrapping parameters for function calls,
181 align the parameter list with the first parameter on the previous line.
182 Use tabs to get as close as possible and then fill in the final 7
183 characters or less with whitespace. For example,
185 var1 = foo(arg1, arg2,
188 The previous example is intended to illustrate alignment of function
189 parameters across lines and not as encourage for gratuitous line
190 splitting. Never split a line before columns 70 - 79 unless you
191 have a really good reason. Be smart about formatting.
194 If, switch, & Code blocks
195 -------------------------
197 Always follow an 'if' keyword with a space but don't include additional
198 spaces following or preceding the parentheses in the conditional.
207 Yes we have a lot of code that uses the second form and we are trying
208 to clean it up without being overly intrusive.
210 Note that this is a rule about parentheses following keywords and not
211 functions. Don't insert a space between the name and left parentheses when
214 Braces for code blocks used by for, if, switch, while, do..while, etc.
215 should begin on the same line as the statement keyword and end on a line
216 of their own. You should always include braces, even if the block only
217 contains one statement. NOTE: Functions are different and the beginning left
218 brace should be located in the first column on the next line.
220 If the beginning statement has to be broken across lines due to length,
221 the beginning brace should be on a line of its own.
223 The exception to the ending rule is when the closing brace is followed by
224 another language keyword such as else or the closing while in a do..while
233 for (x=1; x<10; x++) {
237 for (really_really_really_really_long_var_name=0;
238 really_really_really_really_long_var_name<10;
239 really_really_really_really_long_var_name++)
241 print("%d\n", really_really_really_really_long_var_name);
245 printf("also good\n");
252 print("I'm in a loop!\n"); }
262 print("I should be in braces.\n");
268 While many people have been academically taught that "goto"s are
269 fundamentally evil, they can greatly enhance readability and reduce memory
270 leaks when used as the single exit point from a function. But in no Samba
271 world what so ever is a goto outside of a function or block of code a good
276 int function foo(int y)
282 z = malloc(sizeof(int)*y);
289 print("Allocated %d elements.\n", y);
300 Checking Pointer Values
301 -----------------------
303 When invoking functions that return pointer values, either of the following
304 are acceptable. Use your best judgement and choose the more readable option.
305 Remember that many other persons will review it:
307 if ((x = malloc(sizeof(short)*10)) == NULL ) {
308 fprintf(stderr, "Unable to alloc memory!\n");
313 x = malloc(sizeof(short)*10);
315 fprintf(stderr, "Unable to alloc memory!\n");
322 Samba has large amounts of historical code which makes use of data types
323 commonly supported by the C99 standard. However, at the time such types
324 as boolean and exact width integers did not exist and Samba developers
325 were forced to provide their own. Now that these types are guaranteed to
326 be available either as part of the compiler C99 support or from
327 lib/replace/, new code should adhere to the following conventions:
329 * Booleans are of type "bool" (not BOOL)
330 * Boolean values are "true" and "false" (not True or False)
331 * Exact width integers are of type [u]int[8|16|32|64]_t
337 Samba tries to avoid "typedef struct { .. } x_t;" so we do always try to use
338 "struct x { .. };". We know there are still such typedefs in the code,
339 but for new code, please don't do that anymore.
341 Make use of helper variables
342 ----------------------------
344 Please try to avoid passing function calls as function parameters
345 in new code. This makes the code much easier to read and
346 it's also easier to use the "step" command within gdb.
352 name = get_some_name();
357 ret = some_function_my_name(name);
363 ret = some_function_my_name(get_some_name());