Posts

Showing posts from July, 2011

Good Preprocessor Usage

I was digging through some code and came across the following preprocessor construct: #if FOO == 0 void foo(int bar) { #endif #if FOO != 0 void foo(float baz) { #endif As a general rule, conditionally-compiled code makes the hairs on the back of my neck stand up, but given that this is code for embedded systems, and the same basic code runs on a number of different hardware platforms, I understand that it is sometimes a necessary evil.  The problem is, people don't always treat preprocessing directives as what they really are: code.  They're instructions processed by a computer, and deserve the same good coding practices that one would apply to the rest of their source code.  Preprocessor code may actually deserve more attention to best practices due to the difficult nature of debugging it.  Taking the above as a case study, here are a few thoughts on how to improve this: