in reply to (Ovid - Why I love nested If-Else blocks)
in thread Why I Hate Nested If-Else blocks
You have a point and i do agree, but what happens when you
scale your example up? That's what i am talking about here.
Consider this:
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)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by runrig (Abbot) on Jan 04, 2002 at 03:46 UTC | |
by dws (Chancellor) on Jan 04, 2002 at 03:58 UTC | |
by jeffa (Bishop) on Jan 04, 2002 at 04:30 UTC | |
Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by belg4mit (Prior) on Jan 04, 2002 at 04:40 UTC | |
Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by Sweeper (Pilgrim) on Jan 04, 2002 at 11:43 UTC | |
by tilly (Archbishop) on Jan 07, 2002 at 17:50 UTC | |
(tye)Re: (Ovid - Why I love nested If-Else blocks)
by tye (Sage) on Jan 07, 2002 at 21:10 UTC |
In Section
Meditations