smbd: Remove NT4 compatability handling in posix -> NT ACL conversion
[Samba/gebeck_regimport.git] / README.Coding
blob956a733a4ce77e7d4b7dd4ceec0dd6449bc3a323
1 Coding conventions in the Samba tree
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 .. contents::
6 ===========
7 Quick Start
8 ===========
10 Coding style guidelines are about reducing the number of unnecessary
11 reformatting patches and making things easier for developers to work
12 together.
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,
25 http://www.python.org/pep/pep8 (with spaces). 
26 If you have ever worked on another free software Python project, you are
27 probably already familiar with it.
29 We try to stay compatible with Python 2.4, so please don't rely on any
30 features that were introduced later, such as the "with" statement.
32 But to save you the trouble of reading the Linux kernel style guide, here
33 are the highlights.
35 * Maximum Line Width is 80 Characters
36   The reason is not about people with low-res screens but rather sticking
37   to 80 columns prevents you from easily nesting more than one level of
38   if statements or other code blocks.  Use source3/script/count_80_col.pl
39   to check your changes.
41 * Use 8 Space Tabs to Indent
42   No whitespace fillers.
44 * No Trailing Whitespace
45   Use source3/script/strip_trail_ws.pl to clean up your files before
46   committing.
48 * Follow the K&R guidelines.  We won't go through all of them here. Do you
49   have a copy of "The C Programming Language" anyways right? You can also use
50   the format_indent.sh script found in source3/script/ if all else fails.
54 ============
55 Editor Hints
56 ============
58 Emacs
59 -----
60 Add the follow to your $HOME/.emacs file:
62   (add-hook 'c-mode-hook
63         (lambda ()
64                 (c-set-style "linux")
65                 (c-toggle-auto-state)))
70 (Thanks to SATOH Fumiyasu <fumiyas@osstech.jp> for these hints):
72 For the basic vi editor included with all variants of \*nix, add the
73 following to $HOME/.exrc:
75   set tabstop=8
76   set shiftwidth=8
78 For Vim, the following settings in $HOME/.vimrc will also deal with
79 displaying trailing whitespace:
81   if has("syntax") && (&t_Co > 2 || has("gui_running"))
82         syntax on
83         function! ActivateInvisibleCharIndicator()
84                 syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
85                 highlight TrailingSpace ctermbg=Red
86         endf
87         autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
88   endif
89   " Show tabs, trailing whitespace, and continued lines visually
90   set list listchars=tab:»·,trail:·,extends:…
92   " highlight overly long lines same as TODOs.
93   set textwidth=80
94   autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
97 =========================
98 FAQ & Statement Reference
99 =========================
101 Comments
102 --------
104 Comments should always use the standard C syntax.  C++
105 style comments are not currently allowed.
107 The lines before a comment should be empty. If the comment directly
108 belongs to the following code, there should be no empty line
109 after the comment, except if the comment contains a summary
110 of multiple following code blocks.
112 This is good:
114         ...
115         int i;
117         /*
118          * This is a multi line comment,
119          * which explains the logical steps we have to do:
120          *
121          * 1. We need to set i=5, because...
122          * 2. We need to call complex_fn1
123          */
125         /* This is a one line comment about i = 5. */
126         i = 5;
128         /*
129          * This is a multi line comment,
130          * explaining the call to complex_fn1()
131          */
132         ret = complex_fn1();
133         if (ret != 0) {
134         ...
136         /**
137          * @brief This is a doxygen comment.
138          *
139          * This is a more detailed explanation of
140          * this simple function.
141          *
142          * @param[in]   param1     The parameter value of the function.
143          *
144          * @param[out]  result1    The result value of the function.
145          *
146          * @return              0 on success and -1 on error.
147          */
148         int example(int param1, int *result1);
150 This is bad:
152         ...
153         int i;
154         /*
155          * This is a multi line comment,
156          * which explains the logical steps we have to do:
157          *
158          * 1. We need to set i=5, because...
159          * 2. We need to call complex_fn1
160          */
161         /* This is a one line comment about i = 5. */
162         i = 5;
163         /*
164          * This is a multi line comment,
165          * explaining the call to complex_fn1()
166          */
167         ret = complex_fn1();
168         if (ret != 0) {
169         ...
171         /*This is a one line comment.*/
173         /* This is a multi line comment,
174            with some more words...*/
176         /*
177          * This is a multi line comment,
178          * with some more words...*/
180 Indention & Whitespace & 80 columns
181 -----------------------------------
183 To avoid confusion, indentations have to be tabs with length 8 (not 8
184 ' ' characters).  When wrapping parameters for function calls,
185 align the parameter list with the first parameter on the previous line.
186 Use tabs to get as close as possible and then fill in the final 7
187 characters or less with whitespace.  For example,
189         var1 = foo(arg1, arg2,
190                    arg3);
192 The previous example is intended to illustrate alignment of function
193 parameters across lines and not as encourage for gratuitous line
194 splitting.  Never split a line before columns 70 - 79 unless you
195 have a really good reason.  Be smart about formatting.
198 If, switch, & Code blocks
199 -------------------------
201 Always follow an 'if' keyword with a space but don't include additional
202 spaces following or preceding the parentheses in the conditional.
203 This is good:
205         if (x == 1)
207 This is bad:
209         if ( x == 1 )
211 Yes we have a lot of code that uses the second form and we are trying
212 to clean it up without being overly intrusive.
214 Note that this is a rule about parentheses following keywords and not
215 functions.  Don't insert a space between the name and left parentheses when
216 invoking functions.
218 Braces for code blocks used by for, if, switch, while, do..while, etc.
219 should begin on the same line as the statement keyword and end on a line
220 of their own. You should always include braces, even if the block only
221 contains one statement.  NOTE: Functions are different and the beginning left
222 brace should be located in the first column on the next line.
224 If the beginning statement has to be broken across lines due to length,
225 the beginning brace should be on a line of its own.
227 The exception to the ending rule is when the closing brace is followed by
228 another language keyword such as else or the closing while in a do..while
229 loop.
231 Good examples:
233         if (x == 1) {
234                 printf("good\n");
235         }
237         for (x=1; x<10; x++) {
238                 print("%d\n", x);
239         }
241         for (really_really_really_really_long_var_name=0;
242              really_really_really_really_long_var_name<10;
243              really_really_really_really_long_var_name++)
244         {
245                 print("%d\n", really_really_really_really_long_var_name);
246         }
248         do {
249                 printf("also good\n");
250         } while (1);
252 Bad examples:
254         while (1)
255         {
256                 print("I'm in a loop!\n"); }
258         for (x=1;
259              x<10;
260              x++)
261         {
262                 print("no good\n");
263         }
265         if (i < 10)
266                 print("I should be in braces.\n");
269 Goto
270 ----
272 While many people have been academically taught that "goto"s are
273 fundamentally evil, they can greatly enhance readability and reduce memory
274 leaks when used as the single exit point from a function. But in no Samba
275 world what so ever is a goto outside of a function or block of code a good
276 idea.
278 Good Examples:
280         int function foo(int y)
281         {
282                 int *z = NULL;
283                 int ret = 0;
285                 if (y < 10) {
286                         z = malloc(sizeof(int)*y);
287                         if (!z) {
288                                 ret = 1;
289                                 goto done;
290                         }
291                 }
293                 print("Allocated %d elements.\n", y);
295          done:
296                 if (z) {
297                         free(z);
298                 }
300                 return ret;
301         }
304 Checking Pointer Values
305 -----------------------
307 When invoking functions that return pointer values, either of the following
308 are acceptable. Use your best judgement and choose the more readable option.
309 Remember that many other persons will review it:
311         if ((x = malloc(sizeof(short)*10)) == NULL ) {
312                 fprintf(stderr, "Unable to alloc memory!\n");
313         }
317         x = malloc(sizeof(short)*10);
318         if (!x) {
319                 fprintf(stderr, "Unable to alloc memory!\n");
320         }
323 Primitive Data Types
324 --------------------
326 Samba has large amounts of historical code which makes use of data types
327 commonly supported by the C99 standard. However, at the time such types
328 as boolean and exact width integers did not exist and Samba developers
329 were forced to provide their own.  Now that these types are guaranteed to
330 be available either as part of the compiler C99 support or from
331 lib/replace/, new code should adhere to the following conventions:
333   * Booleans are of type "bool" (not BOOL)
334   * Boolean values are "true" and "false" (not True or False)
335   * Exact width integers are of type [u]int[8|16|32|64]_t
338 Typedefs
339 --------
341 Samba tries to avoid "typedef struct { .. } x_t;" so we do always try to use
342 "struct x { .. };". We know there are still such typedefs in the code,
343 but for new code, please don't do that anymore.
345 Make use of helper variables
346 ----------------------------
348 Please try to avoid passing function calls as function parameters
349 in new code. This makes the code much easier to read and
350 it's also easier to use the "step" command within gdb.
352 Good Example:
354         char *name;
356         name = get_some_name();
357         if (name == NULL) {
358                 ...
359         }
361         ret = some_function_my_name(name);
362         ...
365 Bad Example:
367         ret = some_function_my_name(get_some_name());
368         ...
370 Control-Flow changing macros
371 ----------------------------
373 Macros like NT_STATUS_NOT_OK_RETURN that change control flow
374 (return/goto/etc) from within the macro are considered bad, because
375 they look like function calls that never change control flow. Please
376 do not use them in new code.
378 The only exception is the test code that depends repeated use of calls
379 like CHECK_STATUS, CHECK_VAL and others.