Enumerations
Let’s learn a little about enumerations.
We'll cover the following
Simple enumerations
In all application domains, there are string-valued attributes with a fixed list of possible string values. These
attributes are called enumeration attributes, and the fixed value lists defining their possible string values
are called enumerations. For instance, when we have to manage data about people, we often need to include
information about their gender. The possible values of a gender
attribute may be restricted to one of the
enumeration labels “man”, “woman”, and “other”, or to one of the enumeration codes “M”, “W”, and “O”. Whenever we deal with codes, we also need to have their corresponding labels, at least in a legend that explains the meaning of each code.
Instead of using the enumeration string values as the internal values of an enumeration attribute, it is preferable to use a simplified internal representation for them, such as the positive integers 1, 2, 3, and so on, which enumerate the possible values. However, since these integers do not reveal their meaning, which is indicated by the enumeration label, we use special constants called enumeration literals such as MAN or M, prefixed by the name of the enumeration like in this.gender = GenderEL.MAN
. This is done in program code for readability.Notice that we follow the convention that the names of enumeration literals are written in all upper case. We also use the convention to suffix the name of an enumeration datatype with “EL” (enumeration literal). This way, we can recognize from the name GenderEL
that each instance of this datatype is a “gender enumeration literal.”
Enumerations of records
There are also enumerations with records as their instances, such that one of the record fields provides the name of the enumeration literals. An example of such an enumeration is the following list of units of measurement:
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy