s3-passdb: Make arguments for python wrapper enum_group_mapping() optional
[amitay/samba.git] / README.Coding
1 Coding conventions in the Samba tree
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
4 .. contents::
5
6 ===========
7 Quick Start
8 ===========
9
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.
18
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.
23
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.
27
28 But to save you the trouble of reading the Linux kernel style guide, here
29 are the highlights.
30
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.
36
37 * Use 8 Space Tabs to Indent
38   No whitespace fillers.
39
40 * No Trailing Whitespace
41   Use source3/script/strip_trail_ws.pl to clean up your files before
42   committing.
43
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.
47
48
49
50 ============
51 Editor Hints
52 ============
53
54 Emacs
55 -----
56 Add the follow to your $HOME/.emacs file:
57
58   (add-hook 'c-mode-hook
59         (lambda ()
60                 (c-set-style "linux")
61                 (c-toggle-auto-state)))
62
63
64 Vi
65 --
66 (Thanks to SATOH Fumiyasu <fumiyas@osstech.jp> for these hints):
67
68 For the basic vi editor included with all variants of \*nix, add the
69 following to $HOME/.exrc:
70
71   set tabstop=8
72   set shiftwidth=8
73
74 For Vim, the following settings in $HOME/.vimrc will also deal with
75 displaying trailing whitespace:
76
77   if has("syntax") && (&t_Co > 2 || has("gui_running"))
78         syntax on
79         function! ActivateInvisibleCharIndicator()
80                 syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
81                 highlight TrailingSpace ctermbg=Red
82         endf
83         autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
84   endif
85   " Show tabs, trailing whitespace, and continued lines visually
86   set list listchars=tab:»·,trail:·,extends:…
87
88   " highlight overly long lines same as TODOs.
89   set textwidth=80
90   autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
91
92
93 =========================
94 FAQ & Statement Reference
95 =========================
96
97 Comments
98 --------
99
100 Comments should always use the standard C syntax.  C++
101 style comments are not currently allowed.
102
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.
107
108 This is good:
109
110         ...
111         int i;
112
113         /*
114          * This is a multi line comment,
115          * which explains the logical steps we have to do:
116          *
117          * 1. We need to set i=5, because...
118          * 2. We need to call complex_fn1
119          */
120
121         /* This is a one line comment about i = 5. */
122         i = 5;
123
124         /*
125          * This is a multi line comment,
126          * explaining the call to complex_fn1()
127          */
128         ret = complex_fn1();
129         if (ret != 0) {
130         ...
131
132         /**
133          * @brief This is a doxygen comment.
134          *
135          * This is a more detailed explanation of
136          * this simple function.
137          *
138          * @param[in]   param1     The parameter value of the function.
139          *
140          * @param[out]  result1    The result value of the function.
141          *
142          * @return              0 on success and -1 on error.
143          */
144         int example(int param1, int *result1);
145
146 This is bad:
147
148         ...
149         int i;
150         /*
151          * This is a multi line comment,
152          * which explains the logical steps we have to do:
153          *
154          * 1. We need to set i=5, because...
155          * 2. We need to call complex_fn1
156          */
157         /* This is a one line comment about i = 5. */
158         i = 5;
159         /*
160          * This is a multi line comment,
161          * explaining the call to complex_fn1()
162          */
163         ret = complex_fn1();
164         if (ret != 0) {
165         ...
166
167         /*This is a one line comment.*/
168
169         /* This is a multi line comment,
170            with some more words...*/
171
172         /*
173          * This is a multi line comment,
174          * with some more words...*/
175
176 Indention & Whitespace & 80 columns
177 -----------------------------------
178
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,
184
185         var1 = foo(arg1, arg2,
186                    arg3);
187
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.
192
193
194 If, switch, & Code blocks
195 -------------------------
196
197 Always follow an 'if' keyword with a space but don't include additional
198 spaces following or preceding the parentheses in the conditional.
199 This is good:
200
201         if (x == 1)
202
203 This is bad:
204
205         if ( x == 1 )
206
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.
209
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
212 invoking functions.
213
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.
219
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.
222
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
225 loop.
226
227 Good examples:
228
229         if (x == 1) {
230                 printf("good\n");
231         }
232
233         for (x=1; x<10; x++) {
234                 print("%d\n", x);
235         }
236
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++)
240         {
241                 print("%d\n", really_really_really_really_long_var_name);
242         }
243
244         do {
245                 printf("also good\n");
246         } while (1);
247
248 Bad examples:
249
250         while (1)
251         {
252                 print("I'm in a loop!\n"); }
253
254         for (x=1;
255              x<10;
256              x++)
257         {
258                 print("no good\n");
259         }
260
261         if (i < 10)
262                 print("I should be in braces.\n");
263
264
265 Goto
266 ----
267
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
272 idea.
273
274 Good Examples:
275
276         int function foo(int y)
277         {
278                 int *z = NULL;
279                 int ret = 0;
280
281                 if (y < 10) {
282                         z = malloc(sizeof(int)*y);
283                         if (!z) {
284                                 ret = 1;
285                                 goto done;
286                         }
287                 }
288
289                 print("Allocated %d elements.\n", y);
290
291          done:
292                 if (z) {
293                         free(z);
294                 }
295
296                 return ret;
297         }
298
299
300 Checking Pointer Values
301 -----------------------
302
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:
306
307         if ((x = malloc(sizeof(short)*10)) == NULL ) {
308                 fprintf(stderr, "Unable to alloc memory!\n");
309         }
310
311 or:
312
313         x = malloc(sizeof(short)*10);
314         if (!x) {
315                 fprintf(stderr, "Unable to alloc memory!\n");
316         }
317
318
319 Primitive Data Types
320 --------------------
321
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:
328
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
332
333
334 Typedefs
335 --------
336
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.
340
341 Make use of helper variables
342 ----------------------------
343
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.
347
348 Good Example:
349
350         char *name;
351
352         name = get_some_name();
353         if (name == NULL) {
354                 ...
355         }
356
357         ret = some_function_my_name(name);
358         ...
359
360
361 Bad Example:
362
363         ret = some_function_my_name(get_some_name());
364         ...
365