Is it better to use preprocessor directive or if(constant) statement?

Posted by MByD on Programmers See other posts from Programmers or by MByD
Published on 2011-11-24T17:06:34Z Indexed on 2011/11/24 18:16 UTC
Read the original article Hit count: 156

Filed under:
|
|

Let's say we have a codebase that is used for many different costumer, and we have some code in it that relevant only for costumers of type X. Is it better to use preprocessor directives to include this code only in costumer of type X, or to use if statement, to be more clear:

// some code
#if TYPE_X_COSTUMER  = 1
// do some things
#endif
// rest of the code

or

if(TYPE_X_COSTUMER) {
    // do some things
}

The arguments I can think about are:

  • Preprocessor directive results in smaller code footprint and less branches (on non-optimizing compilers)
  • If statements results with code that always compiles, e.g. if someone will make a mistake that will harm the irrelevant code for the project he works on, the error will still appear, and he will not corrupt the code base. Otherwise he will not be aware of the corruption.
  • I was always been told to prefer the usage of the processor over the usage of the preprocessor (If this is an argument at all...)

What is preferable - when talking about a code base for many different costumers?

© Programmers or respective owner

Related posts about best-practices

Related posts about c++