This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How would I add a fourth value?

 
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this statement

return Math.min(Math.min(x, y), z);

when I looked at the documentation for min it only showed 2 values but i see in this solution z is added this way to compare 3 numbers, but how would i add a fourth?

 
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can be written as:
As you probably have guessed by now, the method still takes only 2 parameters.
 
salvin francis
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although, for a four or more values, I can suggest using a simple Stream. (I remember this from some other post in coderanch...):
 
Ranch Foreman
Posts: 117
3
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just taking a stab at it. What about:

 
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, nothing beats Salvin's solution, but Brechts solution is also nice! Let's generalize it:
 
salvin francis
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use reduce:
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pssst:

In Java 8, some of the primitive wrapper classes have gotten sum, max and min methods just so you can use them as method references. I don't really see the need for those max and min methods, but sum can definitely be useful.

In addition, Math got the following:
* addExact (int or long)
* multiplyExact (int or long), multiplyFull (int -> long) and multiplyHigh (long)
* floorDiv (int or long)
* subtractExact (int or long)
 
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just a simple for loop ...
 
salvin francis
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:...but sum can definitely be useful...


I got curious and checked 1.8 source. I found that the code for sum for Integer, Long, Double is exactly the same (i.e.  return a+b;). I also found that min, max are just delegating calls to Math.min and Math.max.
It's good to know that these methods exist  
 
salvin francis
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

I see what you did there  
That's a great way of ensuring that at least one parameter is passed!
 
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Ron McLeod wrote:

I see what you did there  
That's a great way of ensuring that at least one parameter is passed!


Yeah, that's nice - the main down side is that if you start with a single actual array (as opposed to varargs) you can't just pass that array to this function.  You need extra work:

So it's a question of what use case do you want to optimize your API for...
 
Sheriff
Posts: 13553
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very valid point that MS makes about the API.

As a student of "code smells," I think it's interesting how at first I didn't stop to think much about the API that Ron offered. I saw it and did a quick double take thinking, "hmmm..." for about half a second, then moved on. When I read Salvin's response about it being a nice way to ensure at least one parameter was passed, I thought, "Ok, I guess that's fine." But when I read Mike's response, I also thought, "You know, Mike's right about that being somewhat unintuitive."

It seems like it would be perfectly reasonable to expect that you could do this:

If I only had an array of numbers, why should I have to provide one of the numbers separately? In this usage case where you'd have to pass in the head and tail of the array instead, the API breaks the Principle of Least Astonishment (POLA).

How should I address this API smell? Should I overload? Should I throw an exception?

I think in the end, I'd just go with throwing an exception in the exceptional case:

Sorry if it it seems like I'm just rambling; my point here isn't really the solution I'd take but the journey taken towards that decision. The diversity of opinions shared and the recognition of a smell is arguably the only thing that would compel you to refactor and improve the API. If neither of those two things were present, then we'd just keep on truckin' with a potentially flawed API. As a coach who teaches developers to do TDD, I think that's very interesting.


 
Mike Simmons
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's why I went with throwing IllegalArgumentException in my original version.  But none of these versions is really free of code smell - they just have different smells.

I suppose the other option is to return an OptionalInt, as the IntStream min() method was trying to do in the first place.  Though ultimately I think I'll still go with the IllegalArgumentException.
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To OP's question, we've been throwing streams, binaryoperators, simple ifs, recursion and code smells at him. All for the best of course, but I really wonder what OP thinks of all this.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!