in reply to Efficiency & Maintenance: if/elsif or Other Dispatch Method

This has come up before; but I can tell you right off the bat your code is more complicated than it needs to be. In the following:

if ($outcome > 97) { $rv .= "99"; } elsif ( ($outcome > 93) && ($outcome <= 97) )
You don't need the second condition in the second case (<= 97) because it will always be true if that test is reached, due to the preceding test.
if ( $outcome > 97 ) { $rv .= "99"; } elsif ( $outcome > 93 ) { $rv .= "95"; } elsif ( $outcome > 85 ) { $rv .= "90"; } elsif ( $outcome > 75 ) { $rv .= "80"; } elsif ( $outcome > 65 ) { $rv .= "70"; } elsif ( $outcome > 55 ) { $rv .= "60"; } elsif ( $outcome > 45 ) { $rv .= "50"; } elsif ( $outcome > 35 ) { $rv .= "40"; } elsif ( $outcome > 25 ) { $rv .= "30"; } elsif ( $outcome > 15 ) { $rv .= "20"; } elsif ( $outcome > 7 ) { $rv .= "10"; } elsif ( $outcome > 3 ) { $rv .= "05"; } elsif ( $outcome > 0 ) { $rv .= "00"; }

We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: Efficiency & Maintenance: if/elsif or Other Dispatch Method
by Herkum (Parson) on Dec 01, 2006 at 21:45 UTC
    A slightly shorter version of the above code.
    $rv .= $outcome > 97 ? 99 : $outcome > 93 ? 95 : $outcome > 85 ? 90 : $outcome > 75 ? 80 : $outcome > 65 ? 70 : $outcome > 55 ? 60 : $outcome > 45 ? 50 : $outcome > 35 ? 40 : $outcome > 25 ? 30 : $outcome > 15 ? 20 : $outcome > 7 ? 10 : $outcome > 3 ? '05' : $outcome > 0 ? '00' : q{};