Alex Rivera | Logout

Why should #ifdef be avoided in .c files?

Asked 2009-12-05T05:06:44.133
33

A programmer I respect said that in C code, #if and #ifdef should be avoided at all costs, except possibly in header files. Why would it be considered bad programming practice to use #ifdef in a .c file?

Edit
Report

2 Answers

1

By all means, favor abstraction over conditional compilation. As anyone who has written portable software can tell you, however, the number of environmental permutations is staggering. Some design discipline can help, but sometimes the choice is between elegance and meeting a schedule. In such cases, a compromise might be necessary.

answered 2009-12-05T16:36:53.433
0

It's true that #if #endif does complicate the reading of the code. However I have seen a lot of real world code that have no issues using this and are still going strong. So there may be better ways to avoid using #if #endif but using them is not that bad if proper care is taken.

answered 2009-12-08T05:46:19.457

Your Answer