3. “First off, I'd suggest printing out a copy of
the GNU coding standards, and NOT read it.
Burn them, it's a great symbolic gesture.”
~ Linus Torvalds
24. Spaces
Use a space after these keywords:
if, switch, case, for, do, while
Do not add spaces after these keywords:
sizeof, typeof, alignof,
__attribute__
25. Spaces
Good example:
s = sizeof(struct file);
Suck example:
s=sizeof( struct file );
26. Spaces
Add a space before and after the following
binary and ternary operators:
=,+,-,*,/
<,>,%,&,|,^
<= , >= , == , != , ? , :
27. Spaces
DO NOT Add a space after the following
operators:
&, *, +, -, ~, !, sizeof, typeof, alignof,
__attribute__, define
28. Spaces
DO NOT add a space before and after the
following operators:
++,--
29. Spaces
DO NOT add a space before and after the
following operators:
.
->
44. Indentation
Good examples:
fs/*.c
Bad examples:
drivers/hotplug/ibmphp_res.c
include WTF 370 lines
drivers/usb/serial/usbserials.c
use WTF 21 local variables
49. As the matter of fact, ...
The equivalent to GOTO statement is used
frequently by compiler.
For what?
Unconditional jump instruction
50. Timing of using GOTO
Let function from multiple exit to only one
exit
51. Reason of using GOTO
Unconditional statements are easier to
understand and follow
nested is reduced!!!
errors by not updating individual exit points
when making modifications are prevented
saves the compiler work to optimize
redundant code away ;)
72. Don’t use macro in these cases
1. macros that affect control flow
It looks like a function call but exits the
"calling" function; don't break the internal
parsers of those who will read the code.
74. Don’t use macro in these cases
2. macros that depend on having a local
variable with a magic name
might look like a good thing, but it's
confusing as hell when one reads the code
and it's prone to breakage from seemingly
innocent changes.
75. Don’t use macro in these cases
Example for 2:
#define FOO(val) bar(index, val)
76. Don’t use macro in these cases
3. macros with arguments that are used as
l-values
Ex: FOO(x) = y;
will bite you if somebody e.g. turns FOO
into an inline function
77. Don’t use macro in these cases
4. forgetting about precedence
macros defining constants using
expressions must enclose the expression in
parentheses.
Beware of similar issues with macros using
parameters.
85. EVIL! EVIL! EVIL!
It hides the real type of the variable
Allows programmers to get into trouble
large struct. on the stack
large struct. passed as return value
Can hid e long structure definition
pick a better name
typedef just signify a pointer type
could you be lazier?