(soothing music)
(keyboard clacks)
Frank: 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.
An enumeration is a user-defined data type
that 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,
then 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
in control statements, leaving onlookers with no idea
of what an algorithm was doing.
This often resulted in 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 as input
an integer value corresponding to the state of some system.
The system can be in either state 0, 1, or 2.
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 3, 4, or 5
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 author's questions
since they might have moved on.
If you ever write a piece of code for commercial purposes,
I can guarantee you that you'll 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 gonna 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 a 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 break room refrigerator.
Unfortunately, you use the wrong get_state function,
but because you've defined the state variables in 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 break room 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 get_state function,
that does not return a state type variable,
the program will not compile.
This eliminates any possibility
of the state of the break room 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.