Enumerations
Explore enumerations and their role in front-end applications using plain JavaScript. Understand simple enumerations, code lists, and record enumerations, and learn how to implement these data types for attributes with fixed string values. Gain insights on best practices for naming and using enumeration literals to improve code readability and maintainability.
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:
Unit Symbol | Unit Name | Dimension |
m | meter | length |
kg | kilogram | mass |
s | second | time |
Notice that since both the “Unit Symbol” and the “Unit Name” fields are unique, either of them can be used for the name of the enumeration literals.
Quick summary
In summary, we can distinguish between the following three forms of enumerations:
- Simple enumerations: These define a list of self-explanatory enumeration labels.
- Code lists: These define a list of code or label pairs.
- Record enumerations: These consist of a list of records, so they are defined like classes with simple attributes defining the record fields.
Notice that since enumerations are used as the range of enumeration attributes, they are considered datatypes.
Enumerations may have additional features. For instance, we may want to define a new enumeration by extending an existing enumeration. In programming languages and other computational languages, enumerations are implemented with different features in different ways.