This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In Perl, I sometimes want to return multiple values from a routine. For example:
How would I do something similar in Java? I could just place the values in an array and return the array, except that the values aren't of the same type. I suppose I could make it an array of Strings and then convert the $total to numeric after it has been returned, but that feels a bit clunky.
i suppose you could wrap the things to be returned in wrapper classes (if they're primitives, that is), then cast them to Object, and return an array of Objects. it still seems clumsy, but i guess that's just static typing for you. :roll:
The way I see it is this, if you find yourself in a situation where a method needs to return more than one (primitive) value it suggests a missing object type. Sometimes this will be one you can use from the API, other times you will need to define a new class of your own.
From my own experience of learning Java I would suggest doing it to OO way rather than going for a quick fix which I used to do a lot before I got it. If you are new to Java, coming from something like Perl, then the practice doing it "the right way" will help you get your head round it much quicker IMO.
I'll risk being burned for heresy but if an object is really just a data structure, make the two variables public and lose the getters & setters. The regulars will know that I side mostly with Holub on "getters are evil" and all data should ideally be private, but let's not mechanically turn a lowly struct into a rich object.
More on topic, Kent Beck describes something called a "collecting argument" that is passed to one or more methods to collect all the bits it needs. Any of the methods could populate one or more attributes on the collector. So the thing described above could be an argument as well as a return type.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Another solution is to not have the method return anything, but introduce a Method Object - that is, have the method on its own class and let it set two instance variables, which's values you then can get via getters.
And there are probably a whole bunch of other options. To help you more in choosing one, we'd need to know more about why you have a method that wants to return two values.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jan 14, 2005
the return-two-things idiom is common in dynamic scripting languages, in cases where strictly typed languages would return either an array, a collection, or a custom object. it's just that having dynamic typing and syntax for datatypes like tuples and lists as primitive types makes it convenient enough that nobody bothers creating custom classes for those custom objects.
consider what you might do in Java if there were clear, handy syntax for creating collections (lists or sets) of arbitrary objects, returning those collections from methods, then "unwrapping" them into their component parts on the other end. that's roughly what Wally was doing in the original example.
Joined: Jul 11, 2001
Originally posted by M Beck: it's just that having dynamic typing and syntax for datatypes like tuples and lists as primitive types makes it convenient enough that nobody bothers creating custom classes for those custom objects.
Which doesn't mean that not creating those classes is a good thing...
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com