Style notes added to HACKING
rra at stanford.edu
Sun Mar 10 06:26:27 UTC 2002
Don't know when people will get this; ISC mail seems to be temporarily
I've added the following notes to the style section of HACKING:
* Write in regular ANSI C whenever possible. Use the normal ANSI and
POSIX constructs and use autoconf or portability wrappers to fix
things up beforehand so that the code itself can read like regular
ANSI or POSIX code. Code should be written so that it works as
expected on a modern platform and is fixed up with portability tricks
for older platforms, not the other way around. You may assume an
ANSI C compiler.
Try to use const wherever appropriate. Don't use register; modern
compilers will do as good of a job as you will in choosing what to
put into a register. Don't bother with restrict (at least yet).
* Avoid #ifdef and friends whenever possible. Particularly avoid using
them in the middle of code blocks. Try to hide all portability
preprocessor magic in header files or in portability code in lib.
When something just has to be done two completely different ways
depending on the platform or compile options or the like, try to
abstract that functionality out into a generic function and provide
two separate implementations using #ifdef; then the main code can
just call that function.
If you do have to use preprocessor defines, note that if you always
define them to either 0 or 1 (never use #define without a second
argument), you can use the preprocessor define in a regular if
statement rather than using #if or #ifdef. Make use of this instead
of #ifdef when possible, since that way the compiler will still
syntax-check the other branch for you and it makes it far easier to
convert the code to use a run-time check if necessary.
(Unfortunately, this trick can't be used if one branch may call
functions unavailable on a particular platform.)
Russ Allbery (rra at stanford.edu) <http://www.eyrie.org/~eagle/>
More information about the inn-workers