Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
maybe migrate this codereview. –  Hermann Ingjaldsson Sep 1 '13 at 11:10

2 Answers 2

up vote 4 down vote accepted

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) { ... }
  1. 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.

  2. 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.

  3. GetProductsPrices looks strange, but is still clear.

  4. GetProductsPrice looks totally wrong: we expect such method to return Money, not Money[].

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.

share|improve this answer
    
Regarding documentation, I would rather avoid having to document something that can be stated with no ambiguity in the code. How about variable names? would you call each variable pricesOfProducts just to be sure it's not misinterpreted? or just productPrices? This is event more important, since variables can be found further down the code without the context (like the parameters that were passed to the function) –  moranlf Sep 1 '13 at 10:42
    
@moranlf: for the names of variables, if the scope is small enough, why not calling it prices? If the scope is big enough or there are other prices, then pricesOfProducts would be appropriate for a local variable name. –  MainMa Sep 1 '13 at 11:07

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().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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