Take some code like
if (person.IsMale()) {
doGuyStuff();
} else {
doGirlStuff();
}
Should this be written to explicitly check if person.isFemale()
, and then add a new else that throws an exception? Maybe you're checking values in an enum, or something like that. You think that no one will add new elements to the enum, but who knows? "Can never happen" sounds like famous last words.
I think you've answered your own question. If you know you're never going to see additional values:
bool isSet = ...
if (isSet)
{
return foo;
}
return bar;
...then don't bother. But if there's a chance there could be more than two possible values, cover yourself. You (or the maintenance programmer two years down the road) will be grateful for it.
I find 'can never happen' sounds good until it bites you in the ass months later, after a colleague has added code, breaking your original code. So, for myself, I would make sure my if 's are solid even if it seems impossible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With