|There's more than one way to do things|
Clever vs. Readableby Anonymous Monk
|on Aug 09, 2008 at 17:08 UTC||Need Help??|
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.
* Effective Perl:So whose side are you on?
"This wonderfully symmetrical one-liner contributed by Phil Abercrombie returns the lesser of $a and $b.
$result = ($a, $b)[$b <= $a]"
* 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.
[...] However, it's also possible to write that same expression in a way that's so obvious, straightforward, and plain-spoken that it requires no effort at all to verify that it implements the desired behaviour:
It's not "clever" and it's even marginally slower, but it is clean, clear, efficient, scalable, and easy to maintain. And that's always a much better choice."
Note: I changed the variable names so that both books are consistent with each other.