3

I have method that parses a list of String records into objects and returns List of objects. So my method signature is like this.

public List<ParsedObject> parse(String[] records);

But I also want to return, other metrics like number of string records that were not parsed successfully. Now I get confused, how to return this metric. One option would be to create another wrapper class that holds both list of parsed records and members to store these metrics.

But I face this situation very often and this way I would end up in creating many wrapper classes.

Not sure if I explained well. Any suggestions here?

RandomQuestion
  • 6,778
  • 17
  • 61
  • 97
  • You can include other variables in the class in which this parse() method is declared (if it fits in OOP concept) and write getter methods to access those values. – JProgrammer Feb 22 '12 at 19:02
  • Are you expecting to parse 1 object per record? If so the calling code can compare the size of the list to the length of records. – jlewis42 Feb 22 '12 at 19:03
  • That was just example, though it would work in this case. – RandomQuestion Feb 22 '12 at 19:07

8 Answers8

5

Java does not support returning multiple values from a function, unfortunately. You can create a wrapper class, like you said. Another option is to pass in an array of integers or a "metrics" object or something like that and modify it inside Parse. Slightly better might be to have one or more instance variables (rather than method variables) to keep track of any sort of diagnostic information you need.

Alexander Corwin
  • 1,097
  • 6
  • 11
  • 1
    A quick wrapper class isn't that big an investment, especially if you only need it inside one file and can just whip up an inner class that's a few lines long. – Louis Wasserman Feb 22 '12 at 18:59
  • I would argue that, if it's only in one class, using instance variables to track the information is a cleaner approach, but of course that's a reasonable approach too. The author did specifically mention not wanting to create a lot of wrapper classes, though, so I thought it would be good to explain alternatives. – Alexander Corwin Feb 22 '12 at 19:01
  • 2
    Using instance variables will make it harder to use the class in a thread-safe way, I'd avoid it. – ARRG Feb 22 '12 at 19:02
  • Yes, that is one possibility, but is it good practice to design method that accepts some object in parameter and updates it. For instance metrics object in this case? – RandomQuestion Feb 22 '12 at 19:03
  • 1
    I prefer avoiding side effects like that. Depending on what the class that this parse method is in is responsible for, I would either use instance variables or a wrapper class. @ARRG is right that if it needs to be thread-safe you should avoid the instance variables. – Alexander Corwin Feb 22 '12 at 19:06
  • Hrrrrrm. Immutability is a more relevant practice, I'd argue, and it's awkward for callers to have to deal with an object that's properly just a return value. – Louis Wasserman Feb 22 '12 at 19:07
5

Your question has already been discussed (see this for example: Should Java method arguments be used to return multiple values? ). I personally think that you should either make two method calls, if the returned data is not related. If they are, then you should create a "wrapper" class as you call it. If they really are related data then they probably belong in the same class anyway.

I don't personally favor modifying passed in objects because to me it is a side effect, and it is not clear to see what the method really does.

Another way to think of it is to use the factory pattern (see http://en.wikipedia.org/wiki/Factory_method_pattern) if the object you are building is complex.

Community
  • 1
  • 1
1

Create a ParseResult object. You could include the List, number of records parsed, errors, etc. Make it generic enough so that it could be returned from different methods. You could even make it a base class and return classes that extend from it. Just keeping thinking in terms of objects.

picciano
  • 22,341
  • 9
  • 69
  • 82
1

You can return a complex object containing the list and all the information you need.

jddsantaella
  • 3,657
  • 1
  • 23
  • 39
0

Maybe this could help http://www.yoda.arachsys.com/java/parameters.html

Vlad
  • 23
  • 3
0

I was going to suggest to you some kind of 'c++ pair' type, but then I found this: What is the equivalent of the C++ Pair<L,R> in Java?

Community
  • 1
  • 1
Santiago V.
  • 1,088
  • 8
  • 24
0

A wrapper class is the standard way of returning more information from a function. Another alternative would be to pass another parameter by reference and modify it in your function, thus effectively returning new information to the caller. For example, in your case you would pass an empty list and add all the parsed elements in that list. The return type could be a metric or an error code. Thus the caller will have both pieces of information.

Bogdan
  • 934
  • 7
  • 13
0

This is a very common problem for many people who develop using Java. In other languages, such as Scala, one can create tuples, which are anonymous objects which can hold multiple values, and use them as arguments or return values.

uzilan
  • 2,554
  • 2
  • 31
  • 46