Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by johngg (Canon)
on Jun 18, 2007 at 15:36 UTC ( [id://621808]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: How Much Is Too Much (on one line of code)?
by naikonta (Curate) on Jun 18, 2007 at 15:49 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://621808]
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-03-29 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found