The Artima Developer Community
Sponsored Link

Java Answers Forum
What would you do?

6 replies on 1 page. Most recent reply: Jan 26, 2004 4:08 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

What would you do? Posted: Jan 19, 2004 7:04 AM
Reply to this message Reply
Advertisement
I'm sure it's mostly a question of style but:
Given two ints (n and min), which of the lines below would you use to update min (if it is greater than n)?
if (n < min) min = n;
min = (n < min) ? n : min;
min = Math.min(n, min);


Vince.


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: What would you do? Posted: Jan 19, 2004 9:11 AM
Reply to this message Reply
The entire code for Math.min is as follows

public static int min(int a, int b) {
  return (a <= b) ? a : b;
}


So it almost (but not quite) equates to the second version of

min = (n < min) ? n : min;


Thought you might be interested.

My money would be on using the Math.min method. Why? It does exactly what it says on the tin. Anyone reading the code later will know exactly what the original programmer was up to.

Adam

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: What would you do? Posted: Jan 19, 2004 9:25 AM
Reply to this message Reply
> The entire code for Math.min is as follows
public static int min(int a, int b) 
{
return (a <= b) ? a : b;
}
Good point. Although it's obvious afterwards, it hadn't occurred to me to open up the Math.min method to see what it does. I had just assumed its implementation was internal within the JVM rather than remembering that it's a library function.

Vince.

Sachin Joshi

Posts: 12
Nickname: aspire
Registered: Jan, 2004

Re: What would you do? Posted: Jan 23, 2004 12:09 AM
Reply to this message Reply
Hi vince,

I would go for

min = (n<min) ? n: min;

as a straight forward option.

But, I am interested to know what you want to focus on by this question ?

sachin

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: What would you do? Posted: Jan 24, 2004 2:40 AM
Reply to this message Reply
Hi Sachin,

No special focus, really. I was just knocking out a bit of code out the other day that included a loop that repeatedly calculated a value and stored the lowest found.

Being a VB programmer by day, I tend to have an unhealthy coding style that is neither VB nor Java approved. In this case I initially wrote
if (n < min)
{
   min = n;
}
else
{
   min =
which immediately became
if (n < min)
{
   min = n;
}
then
if (n < min)
   min = n;
then
if (n < min) min = n;
then
min = (n < min) ? n : min;
I preferred the last line but noticed it included an implicit min = min; which would be called every time the new value of n wasn't a new minimum. I was also aware of the Math.min function and assumed it would have the most efficient implementation. So I just wondered how others handled this (rather trivial) problem.

Vince.

> Hi vince,
>
> I would go for
>
> min = (n<min) ? n: min;
>
> as a straight forward option.
>
> But, I am interested to know what you want to focus on by
> this question ?
>
> sachin

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: What would you do? Posted: Jan 24, 2004 12:27 PM
Reply to this message Reply
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!

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Great News Posted: Jan 26, 2004 4:08 AM
Reply to this message Reply
> Being a VB programmer by day...

Two years ago, as a direct result of the September 11 fallout, the (UK) company I work for had to can over 90% of its IT projects. Mine - a Java based project - was one of the first to go. Fortunately, I fould a position on a VB project almost immediately and have been hanging in there ever since (doing VB and SQL). The company didn't fold, the IT projects are expanding again and I've finally got a new position doing Java again!

Unfortunately, I've forgotten a scary amout of Java in the meantime (I'm sure the TopCoder practice problems didn't used to be that difficult) and in the new role I am the Java team. I just hope I can hack it (so to speak).

Vince.

Flat View: This topic has 6 replies on 1 page
Topic: instanceExists() ... Previous Topic   Next Topic Topic: copy subject

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use