1 00:00:05,150 --> 00:00:09,680 In this video, we'll look at some of the motivating factors that led to the creation of enumerated 2 00:00:09,710 --> 00:00:10,400 types. 3 00:00:10,670 --> 00:00:13,820 But first, let's see what an enumeration is conceptually. 4 00:00:14,730 --> 00:00:20,610 At Enumeration is a user defined data type, the models a set of constant integral values. 5 00:00:20,790 --> 00:00:23,520 That might sound complicated, but it really isn't. 6 00:00:24,000 --> 00:00:28,680 Suppose we want to define a type that holds a value, that represents a day of the week. 7 00:00:29,160 --> 00:00:31,890 An enumeration is a great way to accomplish that. 8 00:00:32,220 --> 00:00:38,310 Once we create an enumeration type than any variable of that type can only be used to represent the 9 00:00:38,310 --> 00:00:41,010 days Monday, Tuesday, Wednesday and so forth. 10 00:00:41,780 --> 00:00:46,940 There are lots of examples like this the months of the year, the suits in a deck of cards, the values 11 00:00:46,940 --> 00:00:52,760 in a deck of cards, the states in a system, for example, the state of the player in a game, the 12 00:00:52,760 --> 00:00:54,050 directions on a compass. 13 00:00:54,050 --> 00:00:55,160 You get the idea. 14 00:00:55,490 --> 00:00:58,340 Let's see what motivated the creation of enumerations. 15 00:01:01,070 --> 00:01:02,660 Prior to enumerated types. 16 00:01:02,660 --> 00:01:07,550 Many algorithms contain unnamed numerical constants, also known as magic numbers. 17 00:01:08,240 --> 00:01:14,150 These constants are unique values, with often no explanation as to how they were obtained or whether 18 00:01:14,150 --> 00:01:15,680 their values are significant. 19 00:01:16,400 --> 00:01:21,770 To make matters worse, they were often used as conditionals and control statements, leaving onlookers 20 00:01:21,770 --> 00:01:24,530 with no idea of what an algorithm was doing. 21 00:01:24,890 --> 00:01:30,110 This often results in an algorithm suffering from low readability and high numbers of logic errors. 22 00:01:31,000 --> 00:01:35,950 The story of enumerated types is really a story of the importance of readability and programming and 23 00:01:35,950 --> 00:01:38,410 how it can affect an algorithm's correctness. 24 00:01:38,860 --> 00:01:44,080 To understand how enumerated types increase readability and algorithm correctness, let's look at an 25 00:01:44,080 --> 00:01:44,800 example. 26 00:01:47,260 --> 00:01:52,270 Here we can see a control structure that takes his input, an integer value corresponding to the state 27 00:01:52,270 --> 00:01:53,260 of some system. 28 00:01:53,680 --> 00:01:59,460 The system can be in either state zero one or two, depending on the state of the system. 29 00:01:59,470 --> 00:02:03,340 The control structure initiates some sequence for the system to perform. 30 00:02:03,340 --> 00:02:09,789 The system can perform one of three sequences corresponding to the integer values three, four or five. 31 00:02:10,419 --> 00:02:14,440 Besides what I just explained, there's not much more we can deduce from this algorithm. 32 00:02:14,710 --> 00:02:19,750 You might not see this as a problem since you didn't write the code, and so why should you care about 33 00:02:19,750 --> 00:02:20,110 it? 34 00:02:20,590 --> 00:02:23,680 However, as programmers, we're often required to work on code. 35 00:02:23,680 --> 00:02:29,080 We didn't write ourselves and we don't have the ability to ask the original authors questions since 36 00:02:29,080 --> 00:02:30,220 they might have moved on. 37 00:02:30,820 --> 00:02:35,620 If you ever write a piece of code for commercial purposes, I can guarantee you that you will not be 38 00:02:35,620 --> 00:02:36,700 the last to read it. 39 00:02:37,270 --> 00:02:43,030 This is why readability is so important and much of the reason why enumerated types became so popular. 40 00:02:43,930 --> 00:02:45,040 To emphasize this. 41 00:02:45,040 --> 00:02:47,890 Let's imagine a scenario involving the code we see here. 42 00:02:48,100 --> 00:02:53,380 Suppose you're a novice programmer stationed at NASA's Kennedy Space Center, and the launch sequence 43 00:02:53,380 --> 00:02:56,620 for the new SLS rocket has accidentally been initiated. 44 00:02:57,340 --> 00:03:02,140 The only way to stop the rocket from launching is to provide the control structure with a state that 45 00:03:02,140 --> 00:03:04,120 will initiate the abort sequence. 46 00:03:04,480 --> 00:03:07,240 Do you feel confident that you could provide the correct state? 47 00:03:08,110 --> 00:03:09,130 Of course you don't. 48 00:03:09,160 --> 00:03:13,900 We have no understanding of this algorithm or the mapping between states and sequences. 49 00:03:13,900 --> 00:03:15,460 So how can we fix this? 50 00:03:15,550 --> 00:03:18,220 Well, you might have guessed we're going to use enumerated types. 51 00:03:19,340 --> 00:03:25,400 By including two enumerated types state and sequence, we can assign meaningful names to each of the 52 00:03:25,400 --> 00:03:27,980 integral values used in the control structure. 53 00:03:28,670 --> 00:03:31,360 Don't worry about the syntax of the enumeration right now. 54 00:03:31,370 --> 00:03:33,260 We'll cover those in detail soon. 55 00:03:34,040 --> 00:03:38,390 Instead, let's focus on the control structure and specifically its readability. 56 00:03:38,960 --> 00:03:42,290 Immediately we can see the readability has dramatically improved. 57 00:03:42,410 --> 00:03:48,440 We now know what each integral value represents and can even see the mapping between states and sequences. 58 00:03:49,040 --> 00:03:52,910 If the state is engine failure, the corresponding sequence is abort. 59 00:03:53,450 --> 00:03:58,040 Inclement weather corresponds to hold and nominal corresponds to launch. 60 00:03:58,970 --> 00:04:04,640 Thinking back to our scenario, it now becomes clear that the engine failure state should be provided 61 00:04:04,640 --> 00:04:08,330 to the control structure in order to initiate the abort sequence. 62 00:04:08,960 --> 00:04:11,750 In addition to increasing an algorithm's readability. 63 00:04:11,780 --> 00:04:15,530 I also mentioned that enumerated types can increase algorithmic correctness. 64 00:04:15,560 --> 00:04:18,440 Let's take a look at an example using the same control structure. 65 00:04:20,480 --> 00:04:25,460 As is often the case with rocket launch systems, you usually want the state of the launch to be determined 66 00:04:25,460 --> 00:04:28,250 by a predefined function as opposed to human. 67 00:04:28,580 --> 00:04:34,010 Suppose again that you're a novice programmer at NASA, but this time they've tasked you with writing 68 00:04:34,010 --> 00:04:36,200 the launch sequence controls for the rocket. 69 00:04:36,440 --> 00:04:41,840 You've been informed of an already existing function called Get State, which returns the state of the 70 00:04:41,840 --> 00:04:42,470 launch. 71 00:04:43,240 --> 00:04:48,490 With this information, you get to work and write the control structure using unnamed numerical constants 72 00:04:48,490 --> 00:04:51,820 and retrieve the state of the launch from the get state function. 73 00:04:52,180 --> 00:04:53,410 Or so you think. 74 00:04:54,220 --> 00:04:59,770 Unbeknownst to you, there are two get state functions, one that returns the state of the launch and 75 00:04:59,770 --> 00:05:02,740 another that returns the state of the breakroom refrigerator. 76 00:05:03,010 --> 00:05:08,140 Unfortunately, you use the wrong get state function, but because you've defined the state variable 77 00:05:08,140 --> 00:05:12,910 as an integer and both functions return integers, the program will compile without errors. 78 00:05:13,760 --> 00:05:18,980 Obviously we don't want the launch sequence being determined by the state of the breakroom refrigerator. 79 00:05:18,980 --> 00:05:21,350 But what could we do to prevent this from happening? 80 00:05:21,380 --> 00:05:23,900 Well, this is where enumerated types can help us again. 81 00:05:24,320 --> 00:05:30,410 By using enumerated types, we can impose a type restriction on the state variable being of the enumerated 82 00:05:30,410 --> 00:05:31,330 type state. 83 00:05:31,340 --> 00:05:35,900 It can only assume one of three values engine failure, inclement weather, or nominal. 84 00:05:36,140 --> 00:05:41,120 If we try to assign a value to the state variable that's not one of these three values, the compiler 85 00:05:41,120 --> 00:05:42,350 will generate an error. 86 00:05:42,500 --> 00:05:47,750 This means that if a programmer were to call the wrong state function, that does not return a state 87 00:05:47,750 --> 00:05:48,810 type variable. 88 00:05:48,830 --> 00:05:50,510 The program will not compile. 89 00:05:50,720 --> 00:05:56,360 This eliminates any possibility of the state of the breakroom refrigerator determining the launch sequence. 90 00:05:56,660 --> 00:06:01,400 Now that we've seen how enumerated types can increase readability and algorithm correctness, it should 91 00:06:01,400 --> 00:06:06,360 be obvious how they became so popular with the motivation of enumerated types behind us. 92 00:06:06,380 --> 00:06:09,110 Let's head over to the next video and we'll see their structure.