Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2014-09-16 11:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (13 votes), past polls