1

I want to have two methods, one which returns all items and another, which returns specific Item by name (assume Item has name field):

Item getItem(String itemName)
List<Item> getItems()

How should I name these methods? I'm not sure about getItem and getItems cause these methods just looks too simular (it's hard to catch if we have extra s at the end).

I can imaging many options:

For first method I imagine these names:

getItem
getItemByName
getSingleItem

For second method I imaging these names:

getItems
getAllItems
getItemList
getItemsList

I have 3*4 = 12 combinations in total already, which one is better to use?

3

The problem here is that there is no correct answer to this question as the names chosen are highly subjective and very much personal taste.

I personally would opt for GetItemByName and GetAllItems. In the first case, the method clearly tells me what it does: it gets one item, using its name to identify it. In the second case, names like getItemList leaks the implementation details (a list is returned) into the name. If you then decide to return a different type, then the name won't match the type being returned. GetAllItems avoids that whilst still clearly explaining what it does.

Having said all that though, someone else could equally validly argue the complete opposite of what I've said and not be right or wrong either.

  • Note that no language was specified. IEnumerable assumes OP is using C#. If OP is using Java, a List interface is a reasonable return type. – Adam Bates Jan 16 at 12:41
  • @AdamBates, very good point. Guilty as charged there of assuming it was C#. I've edited the answer to address that. – David Arno Jan 16 at 13:13
  • getItemByName works as long as we have only single parameter. I we have two parameters then it must be something like getItemByNameAndType which looks worse. getSingleItem is more "universal" – javapowered Jan 17 at 8:15
1

As others have stated, it depends. The answer is subjective.

My answer to this question is that it doesn't matter what you choose for these methods specifically, but you should be consistent throughout your codebase.


Now for my personal preference, I like getItems(), I think it says everything it needs to. And I think it looks different enough from "getItem", but I would still change the first one to getItemByName() as it describes what key is used to find the item.

0

getItem and getItems look totally different to me.

If you really want different names, use getSingleItem and getItemList. Not getItemsList. You use a toothbrush, not a teethbrush. But getItem and getItems is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.