Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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

by runrig (Abbot)
on Jan 04, 2002 at 03:46 UTC ( #136146=note: print w/replies, xml ) Need Help??

in reply to (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
in thread Why I Hate Nested If-Else blocks

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

Replies are listed 'Best First'.
Re: Re: (jeffa) 2Re: (Ovid - Why I love nested If-Else blocks)
by dws (Chancellor) on Jan 04, 2002 at 03:58 UTC
    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. ;)


      || : ||

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://136146]
[Discipulus]: shmem the first time I was in germany I was so smart to learn by heart the address of the hostel: Einbahnstraße..
[Discipulus]: james28909 you are welcome
james28909 doesnt drink much alcohol
james28909 does enjoy blazing up though
james28909 also walks around hunting for his cell phone while using its flashlight function

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-05-20 16:17 GMT
Find Nodes?
    Voting Booth?