Having had to audit a lot of code lately, I figured I'd put my coding style, practices, and advice here. Follow it if you want, don't if you don't want, but anyone who sends me mail bitching about it will have their mail nuked in the MTA.

Compiler Stuff

If you're using gcc 2.95.x, make sure your code compiles cleanly with the following: -Wall -W -Wtraditional -Wundef -Wshadow -Wid-clash-32 -Wlarger-than-32 -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wsign-compare -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn -Wredundant-decls -Wnested-externs -Winline -Wlong-long

For gcc 4.1.x, use: -Wall -Wextra -Wfloat-equal -Wdeclaration-after-statement -Wundef -Wshadow -Wlarger-than-32 -Wunsafe-loop-optimizations -Wpointer-arith -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wsign-compare -Waggregate-return -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wmissing-field-initializers -Wmissing-noreturn -Wmissing-format-attribute -Wnormalized=nfc -Wpacked -Wpadded -Wredundant-decls -Wnested-externs -Wunreachable-code -Winline -Wvolatile-register-var -Wdisabled-optimization -Wpointer-sign

Adding -Wshadow is nice, but there's too many third-party libraries that will puke and spit out tons of warnings if you enable that. For local code, though, it will come in quite handy.

Also, when debugging, compile the code without optimization. Otherwise gcc ignores the user and links with its own built-ins even when -fno-builtin -fnostdlib -fnostdinc are used. -fno-strict-aliasing is quite useful, too, as I personally find the need to add casts when you're writing OS-level code to be annoying as hell.

Finding old K&R-style functions can be done in two steps when cleaning up a large source tree:

indent -kr -i8 *.c *.h
egrep  '^([^#    ]{1,} *)+\(([^ ]{1,}(, )*){1,}\)$' *.c
It's not perfect, but it helps a lot when going through several hundred thousand files.

Style

indent -kr -i8 -bad -bap -bbb -di0 -l80 -lc80 -nbbo -nsaf -npcs -npsl \
 -nsai -nsaw -nprs -sc -ss -sob -nbbo -nhnl
If one must look for 80-column compliance per-file and get a list of offending entries for languages indent doesn't handle, assuming one has GNU find/sed available:
find . \( -name '*.[ch]' -o -name '*.cpp' -o -name '*.C' -o -name '*.cxx' \) \
	-exec sh -c 'C=`sed -n "/^.\{81\}/p" {}|wc -c` ; \
	if [ ! $C = 0 ] ; then echo {}  ; fi ' \;



Copyright 2002-2006 Daniel A. Nobuto