in reply to if else and not reachable coding style

Personally, I try to parameter validation at the beginning of the sub, and return validation at the end of the sub (provided its not going to cause a performance problem by needing to execute large amounts of unnecessary code).
sub sg { my $result; die "COND isn't valid" if (COND eq undef); if (COND) { $result = 1; } else { $result = 2; }; die "result isn't valid" if ($result eq undef); return $result; }

Replies are listed 'Best First'.
Re^2: if else and not reachable coding style
by radiantmatrix (Parson) on Mar 09, 2006 at 17:29 UTC

    I would do very much the same thing, with a little tweaking:

    sub sg { my $res; # I won't assume COND being undefined is invalid if (COND) { $res = 1; } else { $res = 2; } die "Undefined result" unless defined $res; return $res; }

    Of course, this I'd only use this if the code were much more complex than simply assigning the result.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet