Difference between revisions of "Code Styling Tips"

From eLinux.org
Jump to: navigation, search
 
m (Fixed link for Henry Spencer's coding style document.)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[http://www.chris-lott.org/resources/cstyle/ifdefs.pdf]
+
Here are some miscellaneous tips for good code styling:
  
And the section of the old [http://www.linuxjournal.com/article/5780] article
+
== Proper Linux Kernel Coding Style ==
starting with "No ifdefs in .c Code", about how to move #ifdefs into headers
+
 
so the functions aren't littered with them.  Just a thought...
+
See the kernel coding style guide in any kernel source tree at: Documentation/CodingStyle
 +
(Online [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob_plain;f=Documentation/CodingStyle;hb=HEAD here])
 +
 
 +
Greg Kroah-Hartman wrote some additional tips in his article:
 +
[http://www.linuxjournal.com/article/5780 Proper Linux Kernel Coding Style]
 +
 
 +
Michael S. Tsirkin made a [http://www.openfabrics.org/~mst/boring.txt kernel guide to space] (''a boring list of rules'') which got polished on a worth reading [http://thread.gmane.org/gmane.linux.kernel/317744 thread] in LKML in 2005.
 +
 
 +
=== use of #ifdefs ===
 +
Rob Landley writes:
 +
 
 +
Read: http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful.pdf
  
 
Personally, I tend to have symbols #defined to a constant 0 or 1 depending on
 
Personally, I tend to have symbols #defined to a constant 0 or 1 depending on
Line 11: Line 22:
 
with any optimizer worth its salt.  Borland C for DOS managed simple dead
 
with any optimizer worth its salt.  Borland C for DOS managed simple dead
 
code elimination 20 years ago...)
 
code elimination 20 years ago...)
 +
 +
== See also ==
 +
[[Sparse]]
 +
 +
[[Category:Development Tools]]

Latest revision as of 15:55, 27 February 2012

Here are some miscellaneous tips for good code styling:

Proper Linux Kernel Coding Style

See the kernel coding style guide in any kernel source tree at: Documentation/CodingStyle (Online here)

Greg Kroah-Hartman wrote some additional tips in his article: Proper Linux Kernel Coding Style

Michael S. Tsirkin made a kernel guide to space (a boring list of rules) which got polished on a worth reading thread in LKML in 2005.

use of #ifdefs

Rob Landley writes:

Read: http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful.pdf

Personally, I tend to have symbols #defined to a constant 0 or 1 depending on whether or not a function is enabled, and then just use if(SYMBOL) as a guard and let the compiler's dead code eliminator take it out for me at compile time (because if(0) {blah;} shouldn't put any code in the resulting .o file with any optimizer worth its salt. Borland C for DOS managed simple dead code elimination 20 years ago...)

See also

Sparse