Should you always write code for else cases that "can never happen"?

Posted by johnswamps on Stack Overflow See other posts from Stack Overflow or by johnswamps
Published on 2010-03-19T01:19:22Z Indexed on 2010/03/19 1:21 UTC
Read the original article Hit count: 203

Filed under:

Take some code like

if (person.IsMale()) {
    doGuyStuff();
} else {
    doGirlOtherStuff();
}

(Yes, I realize this is bad OO code, it's an example)

Should this be written so that to explicitly check if person.isFemale(), and then add a new else that throws an exception? Or 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.

© Stack Overflow or respective owner

Related posts about best-practices