Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:
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(); } }
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:
goto SKIP unless $foo > 7; if ($bar) { bar_func($foo); } elsif ($baz) { baz_func($foo); } SKIP: # more code
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found