4 ms·
I don't know if other people do this (or if it's considered bad or not), but I often find myself coding error branches that in theory should never occur, and ar
by tpoacher 4y ago
I don't know if other people do this (or if it's considered bad or not), but I often find myself coding error branches that in theory should never occur, and are marked in the code as such.
This serves two purposes. One, if for whatever reason you end up with a scenario you hadn't anticipated, it will be picked up and the program will exit gracefully, informing you exactly which supposedly impossible condition was violated.
The second purpose is that, often enough, inserting such an "impossible error" branch makes the preceding code much clearer.
I'm so used to this idiom, in fact, that I now rarely have an 'else' clause catching a proper fallback case, when dealing with an if/else block. I treat the fallback case via another elseif, and almost always the else simply raises an error if triggerred.