Only a brief explanation, since I did this all about 5 years ago.
There is (with most languages) no syntactic requirement to include "null" else
statement (and unnecessary {..}
), and in "simple little programs" there is no need. But real programmers don't write "simple little programs", and, just as importantly, they don't write programs that will be used once and then discarded.
When one write an if/else:
if(something)
doSomething;
else
doSomethingElse;
it all seems simple and one hardly sees even the point of adding {..}
.
But some day, a few months from now, some other programmer (you would never make such a mistake!) will need to "enhance" the program and will add a statement.
if(something)
doSomething;
else
doSomethingIForgot;
doSomethingElse;
Suddenly doSomethingElse
kinda forgets that it's supposed to be in the else
leg.
So you're a good little programmer and you always use {..}
. But you write:
if(something) {
if(anotherThing) {
doSomething;
}
}
All's well and good until that new kid makes a midnight modification:
if(something) {
if(!notMyThing) {
if(anotherThing) {
doSomething;
}
else {
dontDoAnything; // Because it's not my thing.
}}
}
Yes, it's improperly formatted, but so is half the code in the project, and the "auto formatter" gets bollixed up by all the #ifdef
statements. And, of course, the real code is far more complicated than this toy example.
Unfortunately (or not), I've been out of this sort of thing for a few years now, so I don't have a fresh "real" example in mind -- the above is (obviously) contrived and a bit hokey.
assert(false, "should never go here")
might make sense – Mycosisif (x < 0) { x = 0; } else { if (y < 0) { x = 3; }}
. Or you could just follow such rules, many of which are foolish, simply because you are required to. – Childhoodassert(false, "should never go here")
causes warning C4002 (too many macro argument) in Visual Studio 2015. I'd use&&
. IMHO C++11'sstatic_assert
adds to the confusion. – Recitativo< 0
checks), so that assert is going to crash the program on what is probably the most common case where values are in expected bounds. – Underhanded