What is a correct naming convention for Java method returning collection of elements?

Viewed 651

I have a class Person and a method returning Collection<Person>. Is there a naming convention for this in Java? Should I name it findPersons or rather findPeople? Which version is preferable when we have name with irregular plural name in English?

3 Answers

Name it what it does by code.

findPersons // since it would find a collection of Person

As long as it's readable and makes sense to relate to the existing code, its good.

Keep it straightforward AT ALL TIMES. I'm tending towards a more 'functional' approach regarding this to avoid it all :) I'd use just "find()", and perhaps not as part of this class but more as a generic function as it makes it more bulky and less re-usable. Something in the lines of this: "Collection find(args...)"

The result collection itself is revealing enough zero or more is to be expected. (or the Optional if zero or one is to be expected) I mean, it is Object-oriented programming, but not Objects-oriented programming ;)

I think it depends on the name and function of the class.

If the class' function is to manage a collection of Person objects (e.g., PersonManager or PersonList), then the method should just be called find, because the return type is implied.

Otherwise, I would follow @nullpointer's advice by naming the method findPersons, as it explains what the code is doing.

Related