Re: mini-quiz
David Hempy <[email protected]> Tue, 19 Jun 2007 00:06:30 -0400
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
>Ovid, on perlmonks, asks[1] if this code is doing too much on one line:
>
> my $country = $card->country;
> $country = $country eq 'gbr' ? '' : uc "[$country]" if $country;
Okay, I know it's fun to be a perl hacker and all, but is that line really so much easier/faster/cooler than:
my $country = $card->country;
if ($country eq 'gbr') {
$country = '';
} else {
$country = uc "[$country]";
}
Now someone will probably fuss that there's some obscure border case that this expanded logic doesn't behave like the cryptic line. (Heck, I'm not even sure it does the same thing in the normal case!) They're probably right. And that's all the more reason to spell it out. If someone (including the original author) needs to modify that code a year later (or even a week), are they going to remember all the subtle influences of precedence and truthness that made it so slick the first time around?
So, I guess my answer to the original question is pretty much, "Yes, the code is doing too much on one line." Although it really isn't how much it's doing, just the fact that it really makes 98% of perl programmers* think really hard, if not pull out a reference or run tests, to figure out exactly what it does in all cases.
Okay, I'll go back to being a stick-in-the-mud (e.g. employed) perl hack now. ;-)
-dave
* 53% of all statistics are made up on the spot. ;-)
--
David Hempy
Database Administrator
Kentucky Educational Television
(859)258-7164 - (800)333-9764