Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by johngg (Canon)
on Jun 18, 2007 at 13:47 UTC ( #621788=note: print w/replies, xml ) Need Help??


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

I agree with your approach but would try to avoid using the $card->country call twice.

my $country = $card->country; $country = $country ? $country eq q{gbr} ? q{} : qq{[\U$country]} : q{};

I am unsure of the best way to lay out nested ternaries but have found the above indented multi-line approach, with associated condition, true and false all aligned, quite readable. Others may disagree.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: How Much Is Too Much (on one line of code)?
by naikonta (Curate) on Jun 18, 2007 at 14:30 UTC
    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!

      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!

Re^3: How Much Is Too Much (on one line of code)?
by Herkum (Parson) on Jun 19, 2007 at 11:27 UTC

    I agree with your approach but would try to avoid using the $card->country call twice

    There is basically one thing that needs to be taken into account when using a method call and that is whether or not the method is memory intensive or not.

    I like clarity but the overhead savings is something to seriously consider... Thank you for the suggestion.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2018-10-20 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?