Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: What would you do?
|
Posted: Jan 24, 2004 12:27 PM
|
|
In cases such as this (where you are iterating over some collection and finding the max or min value) I would use your first option, but not on one line:
if(n < min)
min = n;
It seems clearer to me and also avoids the "min = min;" silliness. Admittedly, the useless assignment will not be a huge detriment to performance (and may even get optimized out, I'm too lazy to disamble and check it right now), but I think such things can make the code just a little less expressive.
In cases where some other value is to be the lesser of two, then Math.min() is probably most explicit.
Here's a little Python (or Jython) example that makes the question moot:stuff = [13,3,5,7,55,4]
smallest = min(stuff)
Simple as that!
|
|