javamethodsnaming-conventionsnamingmethod-names

Naming of a method that creates a list with new instances


There's a method that creates a list of new instances from some parameters. I would call it this way:

List<NewType> createNewTypeInstances(OldType1 value1, OldType2 value2);

But I've also often seen it named in such a manner:

List<NewType> getNewTypeInstances(OldType1 value1, OldType2 value2);

My question is now: Should every method, that creates (or more generally: returns) something, be named with the prefix get?


Solution

  • It is poor style, and misleading, to have getters that:

    Your method fails on both points.

    Calling it create... is a better choice, and a prefix often used for factory methods, to which your method is closely aligned.