Sometimes we really do want to disable a block of code temporarily, without discarding it. This may be for many reasons.
Perhaps it's under development so we really don't want people using it yet, but we don't want to maintain separate copies of the code for that development than for release.
Or perhaps it's an idea that we tried that didn't work, but that we want to keep around while we figure out why it didn't work.
Or perhaps it's one of several alternatives that we've considered and we haven't made a final decision among them yet -- in that case you'll see things like #if 0 ... #else ... #endif
We could comment out the code we want to disable, but C before C99 didn't have the // comment-until-end-of-line syntax that C++ does, and that made commenting out a block that has comments painful at best. And while it's possible to do some switching back and forth between alternative blocks of code by abusing comments, it's ugly at best and is NOT the right answer for a language which has a C-like preprocessor.
(The addition of // comments is a fine thing, but lots of us old C coders don't use them and they still don't address the desire to easily switch between alternatives while testing out a new solution.)
We could use #ifdef
. But someone might take that as a suggestion that they could try turning it on, and complain when it blows up. #if 0
says clearly "don't use this."
I agree that #if 0
is ugly. Unfortunately, it's also useful, and is a fairly common idiom. There's absolutely no requirement that you ever use it, but you should get used to seeing it because other folks will do so.