For the sake of my discussion a Bool can have 2 states, True or False. Anything else is non-conformance to the programming langugae specification. If your tool chain is non-conformant to its specification, you are hosed no matter what you do.
If a developer created a type of Bool that had more than 2 states, it's the last thing he would ever do on my codebase.
Option A.
if (var == true) {
...
} else if (var == false) {
...
} else {
...
}
Option B
if (var == true) {
...
} else {
...
}
I assert Option B is more robust.....
Any twit can tell you to handle unexpected errors. They are usually trivally easy to detect once you think of them. The example your professior has given is not something that could happen, so it's a very poor example.
A is impossible to test without convoluted test harnesses. If you can't create it, how are you going to test it? If you have not tested the code, how do you know it works? If you don't know it works, then you are not writing robust software. I think they still call that a Catch22 (Great movie, watch it sometime).
Option B is trivial to test.
Next problem, ask you professor this question "What do you want me to do it about it if a Boolean is neither True nor False?"
That should lead into an a very interesting discussion.....
Most cases, a core dump is approriate, at worst it annoys the user or costs a lot of money. What if, say, the module is the Space shuttle realtime reentry calculation system? Any answer, no matter how inaccurate, cannot be worse than aborting, which will kill the users. So what to do, if you know the answer might be wrong, go for the 50/50, or abort and go fo the 100% failure. If I was a crew member, I'd take the 50/50.
Option A kills me
Option B gives me an even chance of survival.
But wait - it's a simulation of the space shuttle reentry - then what? Abort so you know about it. Sound like a good idea? - NOT - because you need to test with the code you plan to ship.
Option A is better for simluation, but can't be deployed. It's useless
Option B is the deployed code so the simulation performs the same as the live systems.
Let's say this was a valid concern. The better solution would be to isolate the error handling from the application logic.
if (var != true || var != false) {
errorReport("Hell just froze over, var must be true or false")
}
......
if (var == true){
....
} else {
....
}
Futher reading - Therac-25 Xray machine, Ariane 5 Rocket failure and others
(Link has many broken links but enough info that Google will help)
if (var) { } else { }
andif not (var) { } else { }
for the same variable. Of course that may depend on how the language interprets the values that aren't true or false. – Marjan Venema Aug 27 '11 at 14:24(x == y)
and(x != y)
could be bothfalse
is that bothx
andy
are floating point numbers, and that one or both values areNaN
. Good job, IEEE. (As expected, some C++ compilers have an option to ignore this behavior. However, most floating-point source code "out there" can be broken by injectingNaN
s, whether this switch is set to on or off.) – rwong May 16 '15 at 21:43There are cases you haven't thought of, as in the example, and you have to have some sort of mitigating code. This is also a possible definition of what robust code is.
– dawidg May 25 '20 at 00:16