Which one of the below three examples would you prefer?
It depends :-)
sub sg { if (COND) { return 1; } else { return 2; } }
I'd do this if both paths through the code were equally likely.
sub sg { if (COND) { return 1; } else { return 2; } die "should never reach this"; }
I'd never do this. I'd trust my test suite to catch any stupid code changes I might make rather than littering my code with die statements.
sub sg { if (COND) { return 1; } return 2; }
I'd do this if 2 would be the "normal" result - although I'd probably write it as
sub sg { return 1 if COND; return 2; }
As another option - if this was a method in some OO code I'd be looking to see whether that if statement was a sign that I was missing some polymorphic behaviour somewhere that I could factor out into separate methods.
In reply to Re: if else and not reachable coding style
by adrianh
in thread if else and not reachable coding style
by szabgab
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |