maezr
maezr

Reputation: 145

ideal way of handling multiple return values in methods in java

I know there are ways to return multiple results and break them up but this feels.. messy.

For practice I'm doing a quadratic equation solver. Obviously there are going to be cases when there are multiple return values needed. I'm just wondering what's the preferred way of handling a case like this-- I'm not so much looking for workarounds (returning a string and breaking it into two, etc) as I am for some sort of general "best practice" for clean/well styled code

Upvotes: 2

Views: 2278

Answers (4)

GETah
GETah

Reputation: 21419

One way to proceed is, as mentioned above, return an array or a collection that contains all your results. However, this approach can get you confused on which return value is stored where. You could do the following:

public static final String TEMPERATURE = "TEMPERATURE";
public static final String SPEED       = "SPEED";
//...
public Map<String, int> doSomeStuff(){
  Map<String, Object> map = HashMap<String, int>();
  // Calculations here...
  map.put(TEMPERATURE, 1);
  map.put(SPEED, 2);
  //...
  return map;
}

// You can then retrieve all results by
int temp = map.get(TEMPERATURE);
int speed = map.get(SPEED);

Upvotes: 1

Paul Bellora
Paul Bellora

Reputation: 55223

As others have mentioned, you can return an instance of a container class used to hold all the results.

Alternatively, you can pass in container objects that the method populates with the results, then pull them out after the call. This is useful for a method overload that returns "extra" information:

public String compute(int arg) {
    return compute(arg, null);
}

public String compute(int arg, MyContainer<String> extraInfoOut) {
    String answer = ...
    if (extraInfoOut != null) {
        String extraInfo = ...
        extraInfoOut.set(extraInfo);
    }
    return answer;
}

...

MyContainer<String> container = MyContainer.empty();
String answer = compute(someInt, container);
String extraInfo = container.get();

Upvotes: 0

pcalcao
pcalcao

Reputation: 15990

Short answer: Create a class that encapsulates the result, and return an instance of that class.

Slightly more expanded version: Java: returning two ints from a method call

I understand how "messy" it feels. Coding in "pure" Java style can have that effect, since you end up creating Classes for everything, and it really feels overkill (trust me, I also code in Python and this really gets to me). But regarding a "clean" way of doing it, I don't see many solutions. Any other assumes that the method that calls your implementation knows details that are not explicit through the interface, like, how is the String that you return supposed to be parsed, etc.

It does seem overkill, but in the long run, it'll make your code (and the code that uses it), more readable, and that goes a long way (like personality).

Upvotes: 12

James Montagne
James Montagne

Reputation: 78650

In this specific situation, why not just return an array or some other collection object, depending on your needs? In a more complex situation you should create a class which holds all of the appropriate information and return an instance of that.

Upvotes: 2

Related Questions