Object-oriented – What does ‘enumerable’ mean

definitionobject-oriented

My background in mathematics is very poor (i.e. last relevant math class taken was high school Trigonometry two years ago – another story for another time). I'm reading 'Javascript: The Definitive Guide' and it's a term that is being repetitively used and I've sort of just ran with it. But I've come to a chapter (Chapter 6 – Objects) where my lack of understanding of the term and its application in programming/OOP is starting to become detrimental to the learning process. The online dictionaries aren't helping out, so does somebody have a more explainable definition and/or example to show?

Best Answer

Mathematically, enumerable means "can be placed into a one to one correspondence with the natural numbers". Most representations in computing are enumerable because they are made of strings of bits, and strings of bits can be taken to denote numbers in the binary system.

Some data representations are not considered enumerable, though. For instance a floating point number is made out of bits, but we work with it as an approximation to a real number. At the binary level we can "count" through consecutive floating-point numbers, but usually this is not relevant in numeric applications.

In some other situations, enumeration is possible, but there is no agreement on an enumeration. For instance, all the objects in your run-time image constitute a finite set and can be enumerated. The garbage collector does that that when it walks through the live objects. But the application does not enumerate the set of all objects in that way without regard for type or module boundaries; it enumerates smaller of objects that are of the same kind, or related.

Usually where an enumeration exists formally, there is agreement by all parties. Here is a set of values. This one is first. That one is its successor, and so on.

In some programming languages, there exist enumerated types: some disciplined way of defining named symbolic constants which are mapped by the compiler to unique integer values. If such types are strongly typed, then these constants can only be stored in variables of their corresponding type, called an enumerated type. For instance a user-defined enumeration of type month can only take on the values january through december. These are reduced to the values 0 through 11, and may allow arithmetic, such as successor(january) producing february, or the possibility of using the enumeration as an array index: defining an array days[january..december] where days[january] is, say, 31. The purpose of an enumerated type, therefore, is to serve as a subset of the natural numbers, by other names that are distinct and provide clarity of meaning (names are used instead of numeric constants) and type checking: the color red from a color enumeration cannot be assigned to a month type enumeration.

The verb enumerate is also used to denote the concept of querying an API to retrieve a list of objects, usually in stepwise fashion: enumerate the network interfaces, enumerate the files in a directory, and so on. Whenever an object is in a 1:N correspondence with some other objects, it it is susceptible to supporting an enumeration API which lets us walk through those sibling objects, or retrieve them as a list. The terminology is a little murky; for instance in some cases, even if the elements are not in a particular order, but can all be visited without repetition, it is still enumeration. Some dynamic set data structures such as hash tables do not provide a particular order, and can reorganize themselves as they grown and shrink, so that the actual order varies when insertions and deletions are performed. A can come before B, but when we insert C, now B happens to come before A because some internal reorganization took place.