Get's hairy quick. At that point, i would definetaly try a dispatch table with sub references. Now, to _really_ go out on a limb, how about a goto as a replacement for your first example, since you are only concerned with calling the subs if one condition is true:if ( $foo > 7 ) { if ( $bar ) { bar_func( $foo ); } elsif ( $baz ) { baz_func( $foo ); } elsif ( $qux ) { baz_func( $foo ); } } elsif ( $foo <= 7 ) { if ( $bar ) { other_bar_func(); } elsif ( $baz ) { other_baz_func(); } elsif ( $qux ) { other_qux_func(); } }
Notice i didn't get rid of the outer if, i just seperated it from the inner if. Even though i use a goto, it looks more readable to me, simply because the indention level. Well, it looks more readable as long as i don't have to scan more than 10 lines to find where the goto is going! Besides, if this were inside a sub, i could replace the goto with return.goto SKIP unless $foo > 7; if ($bar) { bar_func($foo); } elsif ($baz) { baz_func($foo); } SKIP: # more code
BUT ... I do agree that your first example is more readable than your second, and is much better form than my goto solution.
Did i just use a goto?!?
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle)
In reply to (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by jeffa
in thread Why I Hate Nested If-Else blocks
by jeffa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |