Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

(jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)

by jeffa (Bishop)
on Jan 04, 2002 at 03:05 UTC ( [id://136139]=note: print w/replies, xml ) Need Help??


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:
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)

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
    At that point, i would definetaly try a dispatch table with sub references

    I might do something like this:

    my $func = ($foo > 7) ? $bar && \&bar_mysub || $baz && \&baz_mysub : $bar && \&bar_other_mysub || $baz && \&baz_other_mysub; $func->($foo) if $func;
    Update: I admit I might reformat it slightly, but except for the '?:' operator, its not any different than the last example in perlsyn under "Basic BLOCKs and Switch Statements". (We do want to encourage newbies to read the FAQs, right? :-)

    Update: For the curious, here's what perltidy does with a longer version of the above (the above code puts makes perltidy put more on one line, so I added a bit more so it would get more broken up):

    my $func = ( $foo > 7 ) ? $bar && \&bar_mysub || $baz && \&baz_mysub || $bam && \&bam_mysub || $bak && \&bak_mysub : $bar && \&bar_other_mysub || $baz && \&baz_other_mysub || $bam && \&bam_other_mysub || $bak && \&bak_other_mysub; $func->($foo) if $func;
    As always, decide for yourself if this is better or worse, more or less readable, or more or less readable to someone else, than whatever else you might come up with (I actually like jeffa's version below better) :-)
      I might do something like this:
      my $func = ($foo > 7) ? $bar && \&bar_mysub || $baz && \&baz_mysub : $bar && \&bar_other_mysub || $baz && \&baz_other_mysub; $func->($foo) if $func;
      Don't align things vertically for the sake of aligning them, align them so that they're readable! The fragment above borders on obfuscation. There's a mind-numbing repetitive pattern on the right, and this
      || : ||
      thing on the left. Yuch. I had to stop and really look at that fragment to get past the visual clutter.

        Awwww man, i like that ... thing:
        || : ||
        gave me an Atari flashback! :D

        I do commend runrig for giving a working piece of code that kicked the dog doo out of it's conterpart. It took me very little time to parse it, but i am not a newbie anymore.

        For a newbie - that's just got to look like a nightmare. I really think that this is why Ovid loves nested if-else blocks:

        Because newbies (especially those that are new to Perl but not to programming!) can understand them must faster.

        Me, i like the dispatch table - once you really start using them, it's hard not to. Point of little return, so to speak. For one thing, it is very scalable - adding two new conditions was very easy:

        my $func = ($foo > 7) ? $bar && \&bar_mysub || $baz && \&baz_mysub || $qux && \&qux_mysub : $bar && \&bar_other_mysub || $baz && \&baz_other_mysub || $qux && \&qux_other_mysub ; $func->($foo) if $func;
        I did move the '?' and the ';' to make it easier to cut-copy and paste it ... but, man have we gotten away from a simple solution:

        Tuck the check for the value of $foo away in the subroutines themselves, redundant, but that should be a piece of cake to follow and seems more configurable in the long run as well as very straight foward. The redundant pieces will end up becoming different anyway, thanks to Murphy's Law. ;)

        jeffa

        || : ||
Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by belg4mit (Prior) on Jan 04, 2002 at 04:40 UTC
    It's worth noting that your elsif could easily be replaced by a simple else ;-).

    I would probably end up opting for:

    $bar && $foo > 6 ? bar_func($foo) : other_bar_func(); $baz && $foo > 6 ? baz_func($foo) : other_baz_func(); $quux && $foo > 6 ? quux_func($foo) : other_quux_func();
    Of course if you've got code in the block and not just subroutine calls that might get a little ugly. But it's dense (I like dense) and (to me) straight forward.

    UPDATE: Yes, I got the condition wrong (that's what I get for thinking in integers ;-). Quoth a maven, "it's a dispatch table without a hash!"

    UPDATE2: Umm it seems the "?" walked off, they are back.

    --
    perl -pe "s/\b;([st])/'\1/mg"

Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by Sweeper (Pilgrim) on Jan 04, 2002 at 11:43 UTC
    Did i just use a goto?!?

    Well, if you think using a goto is more readable than using another construct, then use it. I have seen code that were emulating goto's, and which were worse, much worse than the same code written with goto's.

    I have read somewhere that there was Dijkstra's paper goto considered harmful, (http://www.acm.org/classics/oct95/) and then somebody answered with another paper "goto considered harmful" considered harmful. Unfortunately, I have not been able to find it.

(tye)Re: (Ovid - Why I love nested If-Else blocks)
by tye (Sage) on Jan 07, 2002 at 21:10 UTC

    I don't see anything that improved on the nested if/else blocks so far. You have 6 cases so you are going to end up with six cases. You could factor out the common code like so:

    { no strict 'refs'; &{ ( $foo < 7 ? "" : "other_" ) . ( $bar ? "bar" : $baz ? "baz" : $qux ? "qux" : last ) . "_func" }( $foo ); }
    but that is pretty obfuscated. (:

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://136139]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-24 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found