Move the alarm setup/teardown out of another_ldap_try() and into separate
[Samba/gebeck_regimport.git] / README.Coding
blob12997ccf9addaa925f62d1622c5cb55f51aaa29e
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, 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
29 are the highlights.
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
42   committing.
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.
50 ============
51 Editor Hints
52 ============
54 Emacs
55 -----
56 Add the follow to your $HOME/.emacs file:
58   (add-hook 'c-mode-hook
59         (lambda ()
60                 (c-set-style "linux")
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:
71   set tabstop=8
72   set shiftwidth=8
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"))
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:…
88   " highlight overly long lines same as TODOs.
89   set textwidth=80
90   autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
93 =========================
94 FAQ & Statement Reference
95 =========================
97 Comments
98 --------
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.
108 This is good:
110         ...
111         int i;
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          */
121         /* This is a one line comment about i = 5. */
122         i = 5;
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         ...
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);
146 This is bad:
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         ...
167         /*This is a one line comment.*/
169         /* This is a multi line comment,
170            with some more words...*/
172         /*
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,
186                    arg3);
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.
199 This is good:
201         if (x == 1)
203 This is bad:
205         if ( x == 1 )
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
212 invoking functions.
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
225 loop.
227 Good examples:
229         if (x == 1) {
230                 printf("good\n");
231         }
233         for (x=1; x<10; x++) {
234                 print("%d\n", x);
235         }
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         }
244         do {
245                 printf("also good\n");
246         } while (1);
248 Bad examples:
250         while (1)
251         {
252                 print("I'm in a loop!\n"); }
254         for (x=1;
255              x<10;
256              x++)
257         {
258                 print("no good\n");
259         }
261         if (i < 10)
262                 print("I should be in braces.\n");
265 Goto
266 ----
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.
274 Good Examples:
276         int function foo(int y)
277         {
278                 int *z = NULL;
279                 int ret = 0;
281                 if (y < 10) {
282                         z = malloc(sizeof(int)*y);
283                         if (!z) {
284                                 ret = 1;
285                                 goto done;
286                         }
287                 }
289                 print("Allocated %d elements.\n", y);
291          done:
292                 if (z) {
293                         free(z);
294                 }
296                 return ret;
297         }
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");
309         }
313         x = malloc(sizeof(short)*10);
314         if (!x) {
315                 fprintf(stderr, "Unable to alloc memory!\n");
316         }
319 Primitive Data Types
320 --------------------
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
334 Typedefs
335 --------
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.
348 Good Example:
350         char *name;
352         name = get_some_name();
353         if (name == NULL) {
354                 ...
355         }
357         ret = some_function_my_name(name);
358         ...
361 Bad Example:
363         ret = some_function_my_name(get_some_name());
364         ...