Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Philosophy of List/Collection returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Philosophy of List/Collection returns" Watch "Philosophy of List/Collection returns" New topic
Author

Philosophy of List/Collection returns

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

This is not hard/advanced technically, but its not obvious what the answer is.

q: is it better design to return a high level Collection, or the best match?

Assume you have a Person object, with a Collection of email addresses (since every geek has two or more).

When you define the getEmailAddresses(), should the prototype be



or

public ArrayList<EmailAddress> getEmailAddresses();


or perhaps even




What is the "best" approach, and what is the philosophy behind the choice?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

I almost always use your first example. All I care about is that it is a List of Foo. Unless the implementation of the List (e.g. ArrayList) is relevant, why limit it?

Map is another good example. I've written many a custom Map implementation and can use them anywhere Map (vs. a specific implementation of Map) is specified.

It's sort of like asking "Give me a 3/4-inch wrench" vs. "Give me a 3/4-inch Craftsman wrench". What? A Husky or Stanley wrench won't do?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

I tend to only return a List or Set if it is really important that the return value actually is a List or Set. That way, if I want to change from a List to a Set or vice versa, or return the values() collection of a map, it's easier to change.

However, it is easier for calling code if you return List, and the following example shows exactly why:

So you'll have to make a decision: either make it easier for you to return something else, or make it easier for users of your method. But users of your method can easily convert like my example code if really necessary.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

I also agree with Bear: always define the return type (and parameter types and (instance) variables as well) as interfaces, never as implementing classes.

That is, again, unless you really need something specific from an implementing class. If you need something that's both a List and Queue, you can declare as List, use a LinkedList and cast to queue (or vice versa), or store two references to the same LinkedList object. But in that case, I think declaring as LinkedList is justified.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Bear Bibeault wrote:It's sort of like asking "Give me a 3/4-inch wrench" vs. "Give me a 3/4-inch Craftsman wrench". What? A Husky or Stanley wrench won't do?


Well, I've got some Snap-on wrenches that will fit places where a Craftsman won't. But the concept is the same, most of the time, all you want is a 3/8-inch wrench. If you care, you can say box end, open end, etc. In the real world, you never use an adjustable wrench when you can use a fixed on, and when you need a deep well socket, say so. Never, ever use pliers on a nut. I guess that would be similar to returning a Collection. Even a Collection<EmailAddress> is better than just a Collection

Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

I feel this fits more in the "OO, Patterns, UML and Refactoring" forum. Here it goes ...


apigee, a better way to API!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

I too use the most general type that will work. In this case that would be List or Collection depending on what I plan to do with the result. Is it supposed to be ordered?

The reason I do this is that things might change in the future and I might want a different subtype. This happened with Vector ---> ArrayList. What happens if someone creates a more efficient list in the future? Doesn't seem likely. But then, I imagine people didn't think Vector would become "un-recommended" either.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
I don't think I've ever used Collection as a return type. Most of the time, I either want ordering and/or direct access -in which case List is called for- or the Set quality of no duplicates. Not caring about either is extremely rare.

But I also have a hard time picturing a situation where today I'd want to use a List, and maybe later might want to switch to a Set. Not to say it couldn't ever happen, but it seems very unlikely. So the genericity of using Collection seems overkill.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Philosophy of List/Collection returns