in reply to Can't happen bug follow-up -- Can an if/else be bypassed?

I hate this type of code, there is no reason to an if/else using return statements. To give two good examples of accomplishing the same thing but a little bit clearer,

return $some_condition ? $x : $y;

Or

if ($some_condition) { return $x; } return $y;

I use this last example a lot when I am testing success or failure of subroutines,

return 1 if $self->_condition_1_is_true and $self->_condition_2_is_true and $self->_condition_3_is_true; # etc... return;

The extra else statement sort of implies that something special is going to happen when it is not. Better to get rid of it (as far as return statements are concerned).

Replies are listed 'Best First'.
Re^2: Can't happen bug follow-up -- Can an if/else be bypassed?
by dragonchild (Archbishop) on Jul 05, 2008 at 04:37 UTC
    Uhh ... no. The if-else construct likely means (at least) one of the following:
    • I expect the number of statements in either condition to grow. This is particularly true with transient debugging print statements.
    • I expect to add one or more elsif clauses.
    • I just removed the last elsif clause.

    Code is written for humans, not computers. Please remember that.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?