http://www.perlmonks.org?node_id=621772


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

When you have to write code that reads left to right and then right to left you are trying to do two things on the same line that lacks clarity.

If I saw this in production code I would hate forever the person who wrote it.

How about this instead,

my $country = (not $card->country) ? q{} : $card->country eq 'gbr' ? q{} : uc "[$country]";

It is only a little more verbose but it is clear at presenting the results of any specific situation.

Replies are listed 'Best First'.
Re^2: How Much Is Too Much (on one line of code)?
by johngg (Canon) on Jun 18, 2007 at 13:47 UTC
    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

      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

      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.