collection naming - singular or plural [closed]
https://softwareengineering.stackexchange.com/questions/210125
-
29-09-2020 - |
Question
This is related to this question but not quite the same. BTW, I'm not a native English speaker.
I keep having a hard time choosing a proper name for collections - Lets say that we have a collection of item s and that each item has a name. How will we call the collection of names such that the collection's name will be the most understandable under standard naming conventions?
var itemNames = GetItemNames(); // Might be interpreted as a single item with many names
var itemsNames = GetItemsNames(); // No ambiguity but doesn't look like proper English
var itemsName = GetItemsName(); // Also sounds strange, and doesn't seem to fit in e.g. a loop:
foreach( itemName in itemsName){...}
As native English speakers, how would you name your collections, and how would you expect them to be named?
Solution
What about GetNamesOfItems
?
Otherwise, GetItemNames
can be a good alternative, if the documentation is clear about what is being returned. This also depends on the items itself:
GetPersonNames
is not clear, since a person can have multiple names,GetProductNames
is more explicit in a context where a product can have one and one only name.
GetItemsNames
looks indeed quite strange.
GetItemsName
is simply incorrect: we expect as a result a common name shared by multiple items, not a sequence of names.
As an example, take a method which returns the prices of products in a category:
public Money[] NameHere(CategoryId categoryId) { ... }
GetProductPrices
could have been interpreted in two ways: either it returns the prices of products, or several prices of a product, one price per currency. In practice, given that the parameter of the method is a category, and not a single product, it is obvious that the first alternative is true.GetPricesOfProducts
appears the most clear alternative. There are zero or more products in a category, and for each, we return its price. The only issue is the case when there are actually several prices per product, one price per currency.GetProductsPrices
looks strange, but is still clear.GetProductsPrice
looks totally wrong: we expect such method to returnMoney
, notMoney[]
.
Also note that in the example above, it may be even better to slightly change the signature itself:
public Tuple(ProductId, Money)[] GetPricesOfProducts(CategoryId categoryId) { ... }
appears easier to understand and removes the ambiguity described in the second point.
OTHER TIPS
In English, this would be "get the items' names". If you want to think of an "item name" as an independent thing, then "get the item names" would also be acceptable. Here I would have used GetItemNames()
without a second thought, so I would recommend either that or GetNames(items)
or items.GetNames()
.