in reply to if else and not reachable coding style

Definitely the first form.

The second form has a clause which you cannot ever reach, so it is utterly useless. It is not "should never reach this" but "cannot ever reach this", so it might lead the unwary to think that you can somehow --by error-- get to this statement which is simply impossible.

The first form makes it abundantly clear that there are two mutually exclusive possibilities which are chosen on the boolean value of the condition.

This is the same with the third form but only because the first branch ends with a return, so one would have to analyse the content of the branch to note that the last part of the sub can only be reached if the condition is false and not because you can fall through the if part of the sub, which is what one would normally expect. One could say there is some action at a distance which prevents you from normally reaching the last part of the sub.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^2: if else and not reachable coding style
by Aristotle (Chancellor) on Mar 11, 2006 at 05:34 UTC

    It is not “should never reach this” but “cannot ever reach this”,

    It depends. If someone modifies the code and does not put the requisite return in the right place, then you will get to that die statement.

    That said, I’m not a fan either. Having that guard there is only useful If the branches are sp long that there’s a real chance that the screw-up it is supposed to protect against may happen. However, I’d prefer never to let the branches get that long to begin with.

    Makeshifts last the longest.