Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Take a look at my CSV Database Validation program. I use a few nested If-Else-If blocks and I consider them to be very useful. If the next if's are logically dependant on one another, they are a Good Thing. As for the example you provide, I think you could have done better (sorry!). Most (not all) usage of the ref function is due to poor design on the part of the programmer and examining the code can give ideas on how to redesign to avoid that. Ignoring that for a moment, let's look at this code:

if ( $foo > 7 ) { if ( $bar ) { bar_func( $foo ); } elsif ( $baz ) { baz_func( $foo ); } }

The two subroutines are dependant on $foo evaluating as true. If I want to avoid nested ifs, I could do this:

if ( $foo > 7 and $bar ) { bar_func( $foo ); } elsif ( $foo > 7 and $baz ) { baz_func( $foo ); }

Frankly, while that works, it's poor programming style. You have duplicate code and that's not a good thing. If the two functions are logically dependant on $foo's value, they should be grouped so that you only test the value once. If those sections of code are far apart, it's easy to miss updating one of the tests, if the condition needs to change. Sure, I could create dispatch tables or jump through all sorts of hoops to get around this, but nested if statements can add clarity to code (unless you're nesting eight levels deep).

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - Why I love nested If-Else blocks) by Ovid
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 contemplating the Monastery: (5)
As of 2024-04-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found