19.6. Cleverness
Tied variables are a clever idea, but "cleverness" is the natural enemy of maintainable code. Unfortunately, Perl provides endless opportunities for cleverness. For example, imagine coming across this result selector in production code: $optimal_result = [$result1=>$result2]->[$result2<=$result1]; 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. Cleverness doesn't have to be nearly that flagrant either. Having finally deduced that the example expression returns the smaller of the two results[*], you would almost certainly be tempted to immediately replace it with something like the following:
$optimal_result = $result1 <= $result2 ? $result1 : $result2; While that's certainly an improvement in both readability and efficiency, it still requires some careful thought to verify that it's doing the right (i.e., minimizing) thing. And everyone who maintains this code will still have to decode that expressionpossibly every time they come across it. 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: use List::Util qw( min ); $optimal_result = min($result1, $result2); 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. |