Question

I sometimes find myself thinking what word to use when referring an IEnumerable<Foo>. I don't think I see a consistent naming when reading.

IEnumerable<Foo>: It's a type name. It isn't helpful when thinking or using in a sentence.

Collection: It's valid but not used everywhere. Although we have the System.Collections namespace.

Sequence: It's valid too and probably defines it better.

Enumerator or Iterator: They emphasize the operation, rarely used.

What would be the most appropriate one? What do you read it in your code?

Was it helpful?

Solution

An IEnumerable<Foo> is not an iterator/enumerator - that is a separate concept for the device that iterates it.

A collection generally refers to a specific device allowing operations such as add/remove; that does not apply here, but the term is not unreasonable.

Personally I tend to use "sequence" in discussion, but I don't use that for naming things in the code - I'd just use the thing the sequence represents, i.e. "orders", "uniqueUsers", etc - or in the case of generic methods, "items", "values", etc.

OTHER TIPS

I definitely lean towards sequence. In my mind, a collection is finite, and an IEnumerable<Foo> may very well represent a sequence of values that are produced on the fly, and so it may represent an infinite sequence of values.

There's a precedent for referring to it in writing as "an IEnumerable of Foo". For example, this MSDN page refers to "an IEnumerable of XElement".

Verbally I would say "An IEnumerable of Foo" or "an Enumerable of Foo".

By far, the clearest and most unambiguous way to describe a variable, parameter, or field of type "IEnumerable<Foo>" is to call it an "IEnumerable<foo>", "IEnumerable(Of foo)", or "IEnumerable of foo". Being a VB programmer, I prefer the middle syntax, especially since it doesn't use punctuation that can be mistaken for HTML markup; in speaking, the latter form would be the most natural pronunciation (with the initial "I" representing its own syllable) except in cases involving nested generics, when other pronunciation cues may be required to indicate the precise nesting.

Some of the classes which implement generic or non-generic (*) IEnumerable interfaces are collections, but some are not. Some are iterators; some not. The terms "collection" and "iterator" are thus not really suitable unless one knows that a particular IEnumerable will in fact be a collection or iterator. The term "iterator" has an additional problem, shared by the term "sequence": not all iterators or sequences implement IEnumerable; some implement IEnumerator, and some implement a GetEnumerator method but do not implement IEnumerable (e.g. because they wish to allow "foreach" but do not wish to promise all the behaviors implied by IEnumerable). If one wishes to avoid jargon, the term "reusable sequence" may be a good one. It's not quite unambiguous, because it could possibly refer to classes which have a GetEnumerator method but don't implement IEnumerable, or possibly to IEnumerator objects whose Reset method actually resets the enumeration, but it excludes one-time-use iterators.

(*) The term "IEnumerable" from here on out will refer to IEnumerable and IEnumerable<T> interchangeably. Likewise "IEnumerator".

Use it in a sentence (code).

IEnumerable<Foo> myFoos = GetFoos();
  //internal vocalization: get some Foos, assign them to myFoos

I suspect that most programmers do not internally vocalize the type in a variable declaration/assignment.

public IEnumerable<Foo> ProcessFoos(IEnumerable<Foo> source)
  //internal vocalization: The method requires some foos and returns some foos.

I guess I use "some" as my internal vocalization. Probably any plural'ish word would work. Batch, chunk, bunch, flock, plethora, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top