• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple value return type in functions

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Techies,

How the JavaSE6.0 handle the function that return multiple values at a time.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you're going to have to worry about that...

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4222792

State........Closed, will not be fixed
 
Vinayagam Kulandaivel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your ref.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome!
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to return more than one value from a method, a Collection class can be constructed and that can be returned to the called place.

But is it a good way of designing ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.

Do you have an example we could discuss?
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.

I agree; the multiple values you're wanting to return are typically related in some way. These data beg to be encapsulated in a class. If the data are unrelated, they probably shouldn't be returned from the same method...
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja,
I fully agree with you. But once i have faced a situation like this. A business object needs the information of an employee's , login time , supervisors Id , the machine in which he logged in, and some extra stuff like initial petty cash amount he had in cash draw while he logged in.

In this situation, a class with the above members was created , and this was returned to the bo.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe a decade ago we requested mainframe data and could not predict what combination of values might come back. Given a customer number we might get any or all of product detail, other products the customer owns, people related to the customer, etc. This was definitely a design compromise ... we distributed logic to the mainframe and smashed all this stuff together because one large payload across the network was faster than three or four small ones. Anyhow, I could see that being a collection and instanceOf checks. I won't argue that it wasn't a design smell.
[ July 27, 2005: Message edited by: Stan James ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
Anyhow, I could see that being a collection and instanceOf checks. I won't argue that it wasn't a design smell.



An lot of early CORBA systems, following the lead of the famous DISCUS case study, were built this way. Everything was of type "Data:" return values, parameters, everything. Data had named properties, and that was about it. Basically everything was just a bag of bits. I worked on a couple of systems like this. It was bloody awful.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to return two ints, therefore I write an appropriate abstraction for two ints:


...and my method:

...TwoInts is probably a silly abstraction in a typical context, therefore, try to define your abstraction. Wanting to "return two values" is always indicative of a failure to abstract appropriately. In my given example, I didn't "return two values", I returned a single value of type "TwoInts" - a mere abstraction of two int values.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With all due respect Tony, I think "TwoInts" is a very weak "abstraction".

I was more thinking along the lines of returning a Point or something - a "real" abstraction with a specific meaning in the solution domain (preferably even in the problem domain).

And returning an object that contains two integers is just *one* of the possible solutions. Another one is to move the method to a different (new?) class, so that it doesn't need to return two ints anymore.

I think this would be much more enlightening if could discuss a real world example. Anyone has one?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


With all due respect Tony, I think "TwoInts" is a very weak "abstraction".


Agreed entirely. In fact, it is almost certainly an inappropriate abstraction. Quite often, thinking in terms of that abstraction (as opposed to "returning multiple values") will yield something more tangible anyway.
I didn't mean to portray TwoInts as a solution; merely a demonstration.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.



I can't understand why we missed an abstraction here . May you explain me more ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anh Vu:


I can't understand why we missed an abstraction here . May you explain me more ?



Let me take Tony's example a step further - he went for something simple and maybe it was too simple to understand the point properly.

Let's make up a fictitious application - let's say we're making a program that plots the locations of tornadoes on a map. In our application, we have a Map class that invokes a method on our DataGatherer class. That method is responsible for returning the latitude and longitude of the most recent tornado.

We might want to do something like this:



Obviously, that's not valid Java code - Java has no means of returning multiple return values. I just made up some syntax to illustrate what we might want to do. We want our getLatestTornado method to return two values - the latitude and longitude of the latest tornado.

However, this is where we can see that we're missing a layer of abstraction. The latitude and longitude of the latest tornado are most certainly related to one another - let's encapsulate those in a class of their own:



Now, if we rewrite our original code using valid Java and the TornadoLocation class, we'd get something like this:



You can see that, by adding the abstraction, TornadoLocation, we've avoided the need to return multiple values from a single method.

While this is a slightly more elaborate example than Tony gave above, it's essentially the same thing and one could probably say that it is still quite contrived. Nonetheless, I hope it helps illustrate what people have been discussing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic