Trouble deciding return type of a method that returns a SortedSet

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-05-21T22:00:55Z Indexed on 2010/05/21 22:10 UTC
Read the original article Hit count: 160

Filed under:
|
|

I am supposed to make a class that should be a container for an interval of values (like in mathematics). I have already decided that I'll use internally a SortedSet.

One of the the things I'm supposed to implement is a method that "gets an ordered set with all the elements in the interval".

class Interval {
    private SortedSet sortedSet = new something();

    ...

    <<method that should return an ordered set of values>>
}

My question resides in what should be both the method's return type and name. Several hypothesis arise:

  1. SortedSet getSortedElements(); I am internally using a SortedSet, so I should return that type. I should state that intent in the method's name.
  2. SortedSet getElements(); I am internally using a SortedSet, but there's no point in stating that in the method name(I don't see a big point in this one).
  3. Set getElements(); I should try to always return the most basic type, thus I am returning a Set. By the contract and definition of the method, people already know all the elements are in order.
  4. Set getSortedElements(); For the method return type, the same as above. About the method name, you are stating clearly what this method is going to return: a set of elements that are sorted.

I'm inclined to use 4. , but the others also seem alright. Is there a clear winner? Why?

© Stack Overflow or respective owner

Related posts about java

Related posts about naming