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, 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 But to save you the trouble of reading the Linux kernel style guide, here
27 * Maximum Line Width is 80 Characters
28 The reason is not about people with low-res screens but rather sticking
29 to 80 columns prevents you from easily nesting more than one level of
30 if statements or other code blocks. Use source3/script/count_80_col.pl
31 to check your changes.
33 * Use 8 Space Tabs to Indent
34 No whitespace fillers.
36 * No Trailing Whitespace
37 Use source3/script/strip_trail_ws.pl to clean up your files before
40 * Follow the K&R guidelines. We won't go through all of them here. Do you
41 have a copy of "The C Programming Language" anyways right? You can also use
42 the format_indent.sh script found in source3/script/ if all else fails.
52 Add the follow to your $HOME/.emacs file:
54 (add-hook 'c-mode-hook
57 (c-toggle-auto-state)))
62 (Thanks to SATOH Fumiyasu <fumiyas@osstech.jp> for these hints):
64 For the basic vi editor included with all variants of \*nix, add the
65 following to $HOME/.exrc:
70 For Vim, the following settings in $HOME/.vimrc will also deal with
71 displaying trailing whitespace:
73 if has("syntax") && (&t_Co > 2 || has("gui_running"))
75 function! ActivateInvisibleCharIndicator()
76 syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
77 highlight TrailingSpace ctermbg=Red
79 autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
81 " Show tabs, trailing whitespace, and continued lines visually
82 set list listchars=tab:»·,trail:·,extends:…
84 " highlight overly long lines same as TODOs.
86 autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
89 =========================
90 FAQ & Statement Reference
91 =========================
96 Comments should always use the standard C syntax. C++
97 style comments are not currently allowed.
100 Indention & Whitespace & 80 columns
101 -----------------------------------
103 To avoid confusion, indentations have to be tabs with length 8 (not 8
104 ' ' characters). When wrapping parameters for function calls,
105 align the parameter list with the first parameter on the previous line.
106 Use tabs to get as close as possible and then fill in the final 7
107 characters or less with whitespace. For example,
109 var1 = foo(arg1, arg2,
112 The previous example is intended to illustrate alignment of function
113 parameters across lines and not as encourage for gratuitous line
114 splitting. Never split a line before columns 70 - 79 unless you
115 have a really good reason. Be smart about formatting.
118 If, switch, & Code blocks
119 -------------------------
121 Always follow an 'if' keyword with a space but don't include additional
122 spaces following or preceding the parentheses in the conditional.
131 Yes we have a lot of code that uses the second form and we are trying
132 to clean it up without being overly intrusive.
134 Note that this is a rule about parentheses following keywords and not
135 functions. Don't insert a space between the name and left parentheses when
138 Braces for code blocks used by for, if, switch, while, do..while, etc.
139 should begin on the same line as the statement keyword and end on a line
140 of their own. You should always include braces, even if the block only
141 contains one statement. NOTE: Functions are different and the beginning left
142 brace should be located in the first column on the next line.
144 If the beginning statement has to be broken across lines due to length,
145 the beginning brace should be on a line of its own.
147 The exception to the ending rule is when the closing brace is followed by
148 another language keyword such as else or the closing while in a do..while
157 for (x=1; x<10; x++) {
161 for (really_really_really_really_long_var_name=0;
162 really_really_really_really_long_var_name<10;
163 really_really_really_really_long_var_name++)
165 print("%d\n", really_really_really_really_long_var_name);
169 printf("also good\n");
176 print("I'm in a loop!\n"); }
186 print("I should be in braces.\n");
192 While many people have been academically taught that "goto"s are
193 fundamentally evil, they can greatly enhance readability and reduce memory
194 leaks when used as the single exit point from a function. But in no Samba
195 world what so ever is a goto outside of a function or block of code a good
200 int function foo(int y)
206 z = malloc(sizeof(int)*y);
213 print("Allocated %d elements.\n", y);
224 Checking Pointer Values
225 -----------------------
227 When invoking functions that return pointer values, either of the following
228 are acceptable. Use your best judgement and choose the more readable option.
229 Remember that many other persons will review it:
231 if ((x = malloc(sizeof(short)*10)) == NULL ) {
232 fprintf(stderr, "Unable to alloc memory!\n");
237 x = malloc(sizeof(short)*10);
239 fprintf(stderr, "Unable to alloc memory!\n");
246 Samba has large amounts of historical code which makes use of data types
247 commonly supported by the C99 standard. However, at the time such types
248 as boolean and exact width integers did not exist and Samba developers
249 were forced to provide their own. Now that these types are guaranteed to
250 be available either as part of the compiler C99 support or from
251 lib/replace/, new code should adhere to the following conventions:
253 * Booleans are of type "bool" (not BOOL)
254 * Boolean values are "true" and "false" (not True or False)
255 * Exact width integers are of type [u]int[8|16|32|64]_t
261 Samba tries to avoid "typedef struct { .. } x_t;" so we do always try to use
262 "struct x { .. };". We know there are still such typedefs in the code,
263 but for new code, please don't do that anymore.
265 Make use of helper variables
266 ----------------------------
268 Please try to avoid passing function calls as function parameters
269 in new code. This makes the code much easier to read and
270 it's also easier to use the "step" command within gdb.
276 name = get_some_name();
281 ret = some_function_my_name(name);
287 ret = some_function_my_name(get_some_name());