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

Interesting. I've looked at all of the answers, and I don't really find any that are deeply satisfying. I don't pretend to claim that my solution is any better, but...

... I dislike both the if and the ternary. That's confusing. But I also dislike the repeated assignments to $country. Which makes me think that the thing $country contains is not really a country until the end of the second line. Before that, it's only a proto-country that needs, say, a few more fjords.

So to separate the distinction between the not-quite ready, and the final value, I would introduce a separate variable. All tied up in a do block to minimise scope:

my $country = do { my $c = $card->country(); ($c and $c ne 'gbr') ? uc "[$c]" : ''; };

This way, there is only the single assignment to $country, and when it's done, you have a country.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: How Much Is Too Much (on one line of code)? (rub)
by tye (Sage) on Jun 18, 2007 at 22:52 UTC

    That was closer to what I came up with but my primary reaction was that if one is spending more than a few seconds on such a very trivial matter (IMHO), then one must not have anything important to work on.

    The apparent assumptions that $card->country() is always lower-case and never "0" were somewhat less trivial to me than the not-quite-perfect layout of the code, but I figured such aren't likely problems in the real environment. In any case, my code looked like:

    my $displayCountry= do { my $c= uc $card->country(); 'GBR' eq $c || '' eq $c ? '' : "[$c]"; };

    which I still consider only trivially different from most of the other gyrations offered. This of course brings to mind the rude term "turd polishing" but this seem more like "rice polishing", making sure each individual grain is shiny before boiling them all. (:

    - tye