"hands down fastest" certainly. Fairly simple and reasonably clear I'd agree. As simple, clear and reliable to code as min? Not a chance.
For trivial variables the ternary operator is ok. But, as soon as any indirection or any other complication in obtaining the values is required the ternary solution becomes unclear and starts to lose its speed advantage. If calculating the values has side effects the ternary version simply isn't tenable.
Perl reduces RSI - it saves typing
| [reply] |
- I said "I prefer".
You are of course free to code as you choose, but don't invoke the justifiction of reliability.
The day I cannot correctly code my $min = $a < $b ? $a : $b; is the day I'll go looking for a CPAN module to do it.
At the same time I'll go looking for something to help me with:
if( $a < $b ) {
## Do what is required when $a is the lessor
}
else {
## Do what is required when $b is the lessor
}
Any thoughts? Maybe you'd code that as if( min( $a, $b ) == $a ) ...?
- If I've to demolish a wall, or drive a nail, I'll reach for a hammer.
But when I've a nut to crack, it stays in the toolkit.
- And if you want to play what-if games. What if $a and $b are overloaded objects?
#! perl -slw
use strict;
use X;
use List::Util qw[ min ];
my $a = X->new( .5, .5, .5, 1 );
my $b = X->new( .7, .9, .4, 1 );
print $a < $b ? 'a is the lessor' : 'b is the lessor';
print "\n-------\n";
print min( $b, $a );
__END__
[ 0:38:13.97] c:\test>junk3
a is the lessor
-------
Argument "X=ARRAY(0x1824550)" isn't numeric in subroutine entry at c:\
+test\junk3.pl line 10.
Argument "X=ARRAY(0x2251a4)" isn't numeric in subroutine entry at c:\t
+est\junk3.pl line 10.
X=ARRAY(0x1824550)
Even ignoring the warnings, did min() get the answer right? And if it did, was it for the right reasons or just some fortuitous accident that'll come back and bite once it goes into production?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
I find $a < $b ? $a : $b clearer than min ($a, $b). The former in unambiguous, the latter uses an ambiguous abbreviation. It's likely to return the minimum of both values, but perhaps it returns $a - $b, or (-$a, -$b). There's no doubt in $a < $b ? $a : $b, but there's some doubt in min ($a, $b).
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |