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 together.
12
You don't have to like them or even agree with them, but once put in place
13
we all have to abide by them (or vote to change them). However, coding
14
style should never outweigh coding itself and so the the guidelines
15
described here are hopefully easy enough to follow as they are very
16
common and supported by tools and editors.
18
The basic style, also mentioned in prog_guide4.txt, is the Linux kernel coding
19
style (See Documentation/CodingStyle in the kernel source tree). This closely
20
matches what most Samba developers use already anyways.
22
But to save you the trouble of reading the Linux kernel style guide, here
25
* Maximum Line Width is 80 Characters
26
The reason is not for people with low-res screens but rather sticking
27
to 80 columns prevents you from easily nesting more than one level of
28
if statements or other code blocks. Use source/script/count_80_col.pl
29
to check your changes.
31
* Use 8 Space Tabs to Indent
34
* No Trailing Whitespace
35
Use source/script/strip_trail_ws.pl to clean you files before committing.
37
* Follow the K&R guidelines. We won't go throw them all here. You have
38
a copy of "The C Programming Language" anyways right? You can also use
39
the format_indent.sh script found in source/script/ if all else fails.
49
Add the follow to your $HOME/.emacs file:
51
(add-hook 'c-mode-hook
54
(c-toggle-auto-state)))
59
(Thanks to SATOH Fumiyasu <fumiyas@osstech.jp> for these hints):
61
For the basic vi editor including with all variants of \*nix, add the
62
following to $HOME/.exrc:
67
For Vim, the following settings in $HOME/.vimrc will also deal with
68
displaying trailing whitespace::
70
if has("syntax") && (&t_Co > 2 || has("gui_running"))
72
function! ActivateInvisibleCharIndicator()
73
syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
74
highlight TrailingSpace ctermbg=Red
76
autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
78
" Show tabs, trailing whitespace, and continued lines visually
79
set list listchars=tab:»·,trail:·,extends:…
81
" highlight overly long lines same as TODOs.
83
autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
86
=========================
87
FAQ & Statement Reference
88
=========================
93
Comments should always use the standard C syntax. C++
94
style comments are not currently allowed.
97
Indention & Whitespace & 80 columns
98
-----------------------------------
100
To avoid confusion, indentations are to be 8 character with tab (not
101
8 ' ' characters. When wrapping parameters for function calls,
102
alignment parameter list with the first parameter on the previous line.
103
Use tabs to get as close as possible and then fill in the final 7
104
characters or less with whitespace. For example,
106
var1 = foo(arg1, arg2,
109
The previous example is intended to illustrate alignment of function
110
parameters across lines and not as encourage for gratuitous line
111
splitting. Never split a line before columns 70 - 79 unless you
112
have a really good reason. Be smart about formatting.
115
If, switch, & Code blocks
116
-------------------------
118
Always follow an 'if' keyword with a space but don't include additional
119
spaces following or preceding the parentheses in the conditional.
128
Yes we have a lot of code that uses the second form and we are trying
129
to clean it up without being overly intrusive.
131
Note that this is a rule about parentheses following keywords and not
132
functions. Don't insert a space between the name and left parentheses when
135
Braces for code blocks used by for, if, switch, while, do..while, etc...
136
should begin on the same line as the statement keyword and end on a line
137
of their own. NOTE: Functions are different and the beginning left brace
138
should begin on a line of its own.
140
If the beginning statement has to be broken across lines due to length,
141
the beginning brace should be on a line of its own.
143
The exception to the ending rule is when the closing brace is followed by
144
another language keyword such as else or the closing while in a do..while
161
printf("also good\n");
168
print("I'm in a loop!\n"); }
174
While many people have been academically taught that goto's are fundamentally
175
evil, they can greatly enhance readability and reduce memory leaks when used
176
as the single exit point from a function. But in no Samba world what so ever
177
is a goto outside of a function or block of code a good idea.
181
int function foo(int y)
187
z = malloc(sizeof(int)*y);
194
print("Allocated %d elements.\n", y);
204
Checking Pointer Values
205
-----------------------
207
When invoking functions that return pointer values, either of the following
208
are acceptable. Use you best judgement and choose the more readable option.
209
Remember that many other people will review it.::
211
if ((x = malloc(sizeof(short)*10)) == NULL ) {
212
fprintf(stderr, "Unable to alloc memory!\n");
217
x = malloc(sizeof(short)*10);
219
fprintf(stderr, "Unable to alloc memory!\n");
226
Samba has large amounts of historical code which makes use of data types
227
commonly supported by the C99 standard. However, at the time such types
228
as boolean and exact width integers did not exist and Samba developers
229
were forced to provide their own. Now that these types are guaranteed to
230
be available either as part of the compiler C99 support or from lib/replace/,
231
new code should adhere to the following conventions:
233
* Booleans are of type "bool" (not BOOL)
234
* Boolean values are "true" and "false" (not True or False)
235
* Exact width integers are of type [u]int[8|16|32|64]_t