Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: How Much Is Too Much (on one line of code)?

by naikonta (Curate)
on Jun 18, 2007 at 14:30 UTC ( #621797=note: print w/replies, xml ) Need Help??


in reply to Re^2: How Much Is Too Much (on one line of code)?
in thread How Much Is Too Much (on one line of code)?

I make this into habit when comes to conditional-chain instead of having a number of blocks only for some single-expressions.
my $some = get_word(); my $when = $some eq 'body' ? 'has to fight' : $some eq 'thing' ? 'has to give' : $some eq 'day' ? 'they will know the truth' : $some eq 'where' ? 'in a avery near place to their mind' : $some eq 'time' ? 'can only tell' : 'yes, there is always a space for default';
Compare that to:
my $some = get_word(); my $when; if ($some eq 'body'); { $when = 'has to fight'; } elsif ($some eq 'thing') { $when = 'has to give'; } elsif ($some eq 'day') { $when = 'they will know the truth'; } elsif ($some eq 'where') { $when = 'in a avery near place to their mind'; } elsif ($some eq 'time') { $when = 'can only tell'; } else { $when = 'yes, there is always a space for default'; }
Update: Putting it in the given-when construct would be nicer for me. But, until then....

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Replies are listed 'Best First'.
Re^4: How Much Is Too Much (on one line of code)?
by johngg (Abbot) on Jun 18, 2007 at 15:36 UTC
    Perhaps I'd set that particular example up in a hash.

    my %whenPhrases = ( body => q{has to fight}, thing => q{has to give}, day => q{they will know the truth}, where => q{in a very near place to their mind}, time => q{can only tell}, ); my $some = get_word(); my $when = exists $whenPhrases{some} ? $whenPhrases{some} : q{yes, there is always a space for default};

    I agree, though, that the ternaries look better than the chained if etc. and I'm not sure at all if my scheme is any clearer than yours in this case.

    my $some = get_word(); my $when = $some eq 'body' ? 'has to fight' : $some eq 'thing' ? 'has to give' : $some eq 'day' ? 'they will know the truth' : $some eq 'where' ? 'in a avery near place to their mind' : $some eq 'time' ? 'can only tell' : 'yes, there is always a space for default';

    Looking at both, I think your way is clearer in this case as it is a simple cascade. My way might win out if the logic were more convoluted.

    Cheers,

    JohnGG

      Perhaps I'd set that particular example up in a hash.
      Yeah, I admit it's not a good example :-) My actual code used different conditional expressions. But in that case, I would probably write:
      my $when = $whenPhrases{some} || q{yes, there is always a space for default};
      There was a situation I chose to layout the chain the way you do but fail to recall which one.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

        I used exists just in case any of the hash values was an empty string, as might happen for the phrase "looking somewhat blank". This might result in the hash entry

        what => q{},

        A bit silly in this case but I have been bitten by that in the past.

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://621797]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2017-12-15 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (415 votes). Check out past polls.

    Notices?