Clever vs. Readable
So, I’m looking at some Perl Books (Perl Best Practices By Damian Conway and Effective Perl Programming by By Joseph N. Hall and Randal L. Schwartz) and I found two encountered opinions on how good code should look like. In particular, both adressed (casually?) the following line:
$result = [$a=>$b]->[$b< =$a];
This returns the lowest of the two values.
Let’s see what they said about it!
* Effective Perl:
“This wonderfully symmetrical one-liner contributed by Phil Abercrombie returns the lesser of $a and $b….
* Perl Best Practices:
“[...] The syntactic symmetry is very elegant, of course, and devising it obviously provided the original developer with a welcome diversion from the tedium of everyday coding. But a clever line of code like that is a (recurring) nightmare to understand and to maintain, and imposes an unnecessary burden on everyone in the development and maintenance teams…
My google alert for “Phil Abercrombie” turned this one up recently, and I was pleased to see that something I’d done so casually is causing soul-searching and conflict amongst a new generation of perl monkeys.
To understand the spirit in which this code was offered up, the monkeys should read http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/f935e8ea5637db9c. Readability was hardly a goal when Joseph Hall solicited these contributions. This one was crafted to use the misdirection of symmetry to hide the multiple roles that some characters play in perl’s rich lexicography.
I have used it as an interview question (“What does this do?”) but I wouldn’t expect to see it in any real code.
In a similar vein, here’s a wonderfully symmetric definition for two well-known values in C.
int false = '-'-'-';
int true = '/'/'/';