In this video, we'll look at some of the motivating factors that led to the creation of enumerated
types.
But first, let's see what an enumeration is conceptually.
At Enumeration is a user defined data type, the models a set of constant integral values.
That might sound complicated, but it really isn't.
Suppose we want to define a type that holds a value, that represents a day of the week.
An enumeration is a great way to accomplish that.
Once we create an enumeration type than any variable of that type can only be used to represent the
days Monday, Tuesday, Wednesday and so forth.
There are lots of examples like this the months of the year, the suits in a deck of cards, the values
in a deck of cards, the states in a system, for example, the state of the player in a game, the
directions on a compass.
You get the idea.
Let's see what motivated the creation of enumerations.
Prior to enumerated types.
Many algorithms contain unnamed numerical constants, also known as magic numbers.
These constants are unique values, with often no explanation as to how they were obtained or whether
their values are significant.
To make matters worse, they were often used as conditionals and control statements, leaving onlookers
with no idea of what an algorithm was doing.
This often results in an algorithm suffering from low readability and high numbers of logic errors.
The story of enumerated types is really a story of the importance of readability and programming and
how it can affect an algorithm's correctness.
To understand how enumerated types increase readability and algorithm correctness, let's look at an
example.
Here we can see a control structure that takes his input, an integer value corresponding to the state
of some system.
The system can be in either state zero one or two, depending on the state of the system.
The control structure initiates some sequence for the system to perform.
The system can perform one of three sequences corresponding to the integer values three, four or five.
Besides what I just explained, there's not much more we can deduce from this algorithm.
You might not see this as a problem since you didn't write the code, and so why should you care about
it?
However, as programmers, we're often required to work on code.
We didn't write ourselves and we don't have the ability to ask the original authors questions since
they might have moved on.
If you ever write a piece of code for commercial purposes, I can guarantee you that you will not be
the last to read it.
This is why readability is so important and much of the reason why enumerated types became so popular.
To emphasize this.
Let's imagine a scenario involving the code we see here.
Suppose you're a novice programmer stationed at NASA's Kennedy Space Center, and the launch sequence
for the new SLS rocket has accidentally been initiated.
The only way to stop the rocket from launching is to provide the control structure with a state that
will initiate the abort sequence.
Do you feel confident that you could provide the correct state?
Of course you don't.
We have no understanding of this algorithm or the mapping between states and sequences.
So how can we fix this?
Well, you might have guessed we're going to use enumerated types.
By including two enumerated types state and sequence, we can assign meaningful names to each of the
integral values used in the control structure.
Don't worry about the syntax of the enumeration right now.
We'll cover those in detail soon.
Instead, let's focus on the control structure and specifically its readability.
Immediately we can see the readability has dramatically improved.
We now know what each integral value represents and can even see the mapping between states and sequences.
If the state is engine failure, the corresponding sequence is abort.
Inclement weather corresponds to hold and nominal corresponds to launch.
Thinking back to our scenario, it now becomes clear that the engine failure state should be provided
to the control structure in order to initiate the abort sequence.
In addition to increasing an algorithm's readability.
I also mentioned that enumerated types can increase algorithmic correctness.
Let's take a look at an example using the same control structure.
As is often the case with rocket launch systems, you usually want the state of the launch to be determined
by a predefined function as opposed to human.
Suppose again that you're a novice programmer at NASA, but this time they've tasked you with writing
the launch sequence controls for the rocket.
You've been informed of an already existing function called Get State, which returns the state of the
launch.
With this information, you get to work and write the control structure using unnamed numerical constants
and retrieve the state of the launch from the get state function.
Or so you think.
Unbeknownst to you, there are two get state functions, one that returns the state of the launch and
another that returns the state of the breakroom refrigerator.
Unfortunately, you use the wrong get state function, but because you've defined the state variable
as an integer and both functions return integers, the program will compile without errors.
Obviously we don't want the launch sequence being determined by the state of the breakroom refrigerator.
But what could we do to prevent this from happening?
Well, this is where enumerated types can help us again.
By using enumerated types, we can impose a type restriction on the state variable being of the enumerated
type state.
It can only assume one of three values engine failure, inclement weather, or nominal.
If we try to assign a value to the state variable that's not one of these three values, the compiler
will generate an error.
This means that if a programmer were to call the wrong state function, that does not return a state
type variable.
The program will not compile.
This eliminates any possibility of the state of the breakroom refrigerator determining the launch sequence.
Now that we've seen how enumerated types can increase readability and algorithm correctness, it should
be obvious how they became so popular with the motivation of enumerated types behind us.
Let's head over to the next video and we'll see their structure.