views:

275

answers:

9

When reading this question I started to wonder a bit. Say you have these two:

class ProductCollection : ICollection<Product>
class ProductList : IList<Product>

What would you call one that were an IEnumerable<Product>?

class Product--- : IEnumerable<Product>

Before I read that other question I might have called it a ProductCollection actually, but taking the new info into account, that would have been a bit misleading since it does not implement ICollection<Product>. Could you call it Products?

var products = new Products(); // products is/are products

Almost works but sounds a bit strange... What would you call it?

A: 

If you look at the types in the .NET Framework that implements IEnumerable<T>, I would say that the most common suffix is Collection, followed by the plural form of what the list contains. Then there are a number of "special cases" (such as Queue and Stack). I would personally vote for Collection as a first-hand choice.

Fredrik Mörk
A: 

The problem here is that List or Collection impose they behave that way. But IEnumerable is very generic, not only in the C# meaning of generic. Every implementation can be behind it.

Some suggestions:

  • ProductIterator: This iterates Products.
  • ProductRetriever: This retrieves Products.
  • ProductCreator: This creates new Products at runtime.
Dykam
Made it community, add suggestions if you like.
Dykam
I wouldn't call it `ProductIterator`. If you implement `IEnumerable`, there's a `GetEnumerator` method that returns an `IEnumerator` which is the actual iterator.
Tom Lokhorst
+5  A: 

You generally do not base the name of a class off any interface it implements. (Which one do you choose when there are multiple ones, for a start?) It is quite typical to base it off an inherited class, but more often simply on the purpose of the class, and certainly not the interface. (The interface might be named after the class, if anything.)

Your example is somewhat invalidated by the fact that a well-designed ProductCollection should implement ICollection<Product> and IEnumerable<Product> while a well-designed ProductList should implement both those interfaces as well as IList<Product>.

If you look in the BCL of the .NET Framework, you should notice that this is precisely the case. The List<T> class implements all three interfaces, as does the Collection<T> class (though note that in the general case a 'collection' need not implement IList<T>).

Noldorin
Very good answer! That pretty much cleared it up for me :)
Svish
@Svish: Glad it helped. The BCL is a generally good model to go by here (though it may contain the odd inconsistency). So long as you have consistency in your own class names, you can't go far wrong.
Noldorin
+2  A: 

It would depend upon the context, for example, it could be a ProductsCatalog (implying the read-only nature of IEnumerable).

More generally, it could be ProductsView. Of course, the Products fetched would be modifiable, but I feel it "sounds" appropriate nevertheless.

kek444
A: 

In almost every case I can think of from my own experience, I haven't had to think of a name. The compiler does it for me, because I write an iterator method instead of writing a class by hand. And for the method name, it seems natural to just make it a plural word describing the sequence.

Daniel Earwicker
+5  A: 

If it only implements IEnumerable<Product>, then I would name it ProductEnumeration, but I would feel free to name an instance of it products. On the other hand, I don't recall ever creating a class that only implemented IEnumerable<T>. Doesn't seem to be much point if you can't add stuff to it and if you can, then I'd derive from one of the collection classes that implements IEnumerable<T> and inherit that behavior, too.

If I were returning an enumeration of Product entities, I'd simply return it as IEnumerable<Product> without having a special class.

tvanfosson
Very good points as well.
Svish
+2  A: 

I think that "Sequence" would be a good suffix.

Andrew Hare
Was thinking about that one as well actually.
Svish
Indeed. The above posters have not considered that the list of items may be generated dynamically rather than being backed by an actual list or array of some kind.
locster
A: 

Like kek444 I think I'd have to ask what else does it do? It implements IEnumerable<> but that's not all it does, or else you wouldn't need the class right? It is a collection? Does it transform one enumerable to another (alter order, selection, generation, etc)?

n8wrl
A: 

I would say Collection. It might suggest a ReadOnlyCollection or ObservableCollection , but it describes the class well. It is after all a collection of products (whatever the underlying type may be).

To solve this question and the other question, use this ;)
http://www.classnamer.com/

Zyphrax