1 00:00:00,480 --> 00:00:02,558 (soothing music) 2 00:00:02,558 --> 00:00:05,160 (keyboard clacks) 3 00:00:05,160 --> 00:00:06,420 Frank: In this video, we'll look at some 4 00:00:06,420 --> 00:00:08,130 of the motivating factors that led 5 00:00:08,130 --> 00:00:11,370 to the creation of enumerated types, but first, 6 00:00:11,370 --> 00:00:14,357 let's see what an enumeration is conceptually. 7 00:00:14,357 --> 00:00:17,141 An enumeration is a user-defined data type 8 00:00:17,141 --> 00:00:20,910 that models a set of constant integral values. 9 00:00:20,910 --> 00:00:24,090 That might sound complicated, but it really isn't. 10 00:00:24,090 --> 00:00:26,670 Suppose we want to define a type that holds a value 11 00:00:26,670 --> 00:00:29,280 that represents a day of the week, 12 00:00:29,280 --> 00:00:32,340 an enumeration is a great way to accomplish that. 13 00:00:32,340 --> 00:00:34,440 Once we create an enumeration type, 14 00:00:34,440 --> 00:00:37,500 then any variable of that type can only be used 15 00:00:37,500 --> 00:00:39,540 to represent the days Monday, Tuesday, 16 00:00:39,540 --> 00:00:40,953 Wednesday, and so forth. 17 00:00:41,820 --> 00:00:43,650 There are lots of examples like this, 18 00:00:43,650 --> 00:00:46,290 the months of the year, the suits in a deck of cards, 19 00:00:46,290 --> 00:00:48,210 the values in a deck of cards, 20 00:00:48,210 --> 00:00:50,580 the states in a system, for example, 21 00:00:50,580 --> 00:00:52,590 the state of the player in a game, 22 00:00:52,590 --> 00:00:55,560 the directions on a compass, you get the idea. 23 00:00:55,560 --> 00:00:58,293 Let's see what motivated the creation of enumerations. 24 00:01:01,140 --> 00:01:02,700 Prior to enumerated types, 25 00:01:02,700 --> 00:01:05,640 many algorithms contain unnamed numerical constants, 26 00:01:05,640 --> 00:01:08,310 also known as magic numbers. 27 00:01:08,310 --> 00:01:10,530 These constants are unique values 28 00:01:10,530 --> 00:01:13,680 with often no explanation as to how they were obtained 29 00:01:13,680 --> 00:01:16,440 or whether their values are significant. 30 00:01:16,440 --> 00:01:19,290 To make matters worse, they were often used as conditionals 31 00:01:19,290 --> 00:01:22,890 in control statements, leaving onlookers with no idea 32 00:01:22,890 --> 00:01:24,960 of what an algorithm was doing. 33 00:01:24,960 --> 00:01:27,180 This often resulted in algorithm suffering 34 00:01:27,180 --> 00:01:30,063 from low readability and high numbers of logic errors. 35 00:01:30,990 --> 00:01:33,180 The story of enumerated types is really a story 36 00:01:33,180 --> 00:01:35,790 of the importance of readability and programming 37 00:01:35,790 --> 00:01:38,940 and how it can affect an algorithm's correctness. 38 00:01:38,940 --> 00:01:41,880 To understand how enumerated types increase readability 39 00:01:41,880 --> 00:01:44,763 and algorithm correctness, let's look at an example. 40 00:01:47,310 --> 00:01:50,010 Here we can see a control structure that takes as input 41 00:01:50,010 --> 00:01:53,760 an integer value corresponding to the state of some system. 42 00:01:53,760 --> 00:01:57,870 The system can be in either state 0, 1, or 2. 43 00:01:57,870 --> 00:01:59,460 Depending on the state of the system, 44 00:01:59,460 --> 00:02:01,800 the control structure initiates some sequence 45 00:02:01,800 --> 00:02:03,450 for the system to perform. 46 00:02:03,450 --> 00:02:06,570 The system can perform one of three sequences corresponding 47 00:02:06,570 --> 00:02:10,440 to the integer values 3, 4, or 5 48 00:02:10,440 --> 00:02:12,090 Besides what I just explained, 49 00:02:12,090 --> 00:02:14,790 there's not much more we can deduce from this algorithm. 50 00:02:14,790 --> 00:02:16,770 You might not see this as a problem 51 00:02:16,770 --> 00:02:18,450 since you didn't write the code, 52 00:02:18,450 --> 00:02:20,610 and so why should you care about it? 53 00:02:20,610 --> 00:02:23,190 However, as programmers we're often required to work 54 00:02:23,190 --> 00:02:25,290 on code we didn't write ourselves 55 00:02:25,290 --> 00:02:26,610 and we don't have the ability 56 00:02:26,610 --> 00:02:28,770 to ask the original author's questions 57 00:02:28,770 --> 00:02:30,900 since they might have moved on. 58 00:02:30,900 --> 00:02:33,810 If you ever write a piece of code for commercial purposes, 59 00:02:33,810 --> 00:02:37,350 I can guarantee you that you'll not be the last to read it. 60 00:02:37,350 --> 00:02:39,744 This is why readability is so important and much 61 00:02:39,744 --> 00:02:42,993 of the reason why enumerated types became so popular. 62 00:02:43,980 --> 00:02:45,030 To emphasize this, 63 00:02:45,030 --> 00:02:48,210 let's imagine a scenario involving the code we see here. 64 00:02:48,210 --> 00:02:50,340 Suppose you're a novice programmer stationed 65 00:02:50,340 --> 00:02:53,400 at NASA's Kennedy Space Center and the launch sequence 66 00:02:53,400 --> 00:02:57,067 for the new SLS rocket has accidentally been initiated. 67 00:02:57,067 --> 00:02:59,400 The only way to stop the rocket from launching is 68 00:02:59,400 --> 00:03:01,980 to provide the control structure with a state 69 00:03:01,980 --> 00:03:04,530 that will initiate the abort sequence. 70 00:03:04,530 --> 00:03:05,460 Do you feel confident 71 00:03:05,460 --> 00:03:07,664 that you could provide the correct state? 72 00:03:07,664 --> 00:03:09,330 Of course you don't. 73 00:03:09,330 --> 00:03:11,970 We have no understanding of this algorithm or the mapping 74 00:03:11,970 --> 00:03:15,660 between states and sequences, so how can we fix this? 75 00:03:15,660 --> 00:03:16,590 Well, you might have guessed 76 00:03:16,590 --> 00:03:18,704 we're gonna use enumerated types. 77 00:03:18,704 --> 00:03:22,950 By including two enumerated types, State and Sequence, 78 00:03:22,950 --> 00:03:25,140 we can assign meaningful names to each 79 00:03:25,140 --> 00:03:28,680 of the integral values used in the control structure. 80 00:03:28,680 --> 00:03:31,440 Don't worry about the syntax of the enumeration right now, 81 00:03:31,440 --> 00:03:33,243 we'll cover those in detail soon. 82 00:03:34,080 --> 00:03:36,450 Instead, let's focus on the control structure 83 00:03:36,450 --> 00:03:38,678 and specifically its readability. 84 00:03:38,678 --> 00:03:39,990 Immediately, we can see 85 00:03:39,990 --> 00:03:42,540 the readability has dramatically improved. 86 00:03:42,540 --> 00:03:44,880 We now know what each integral value represents 87 00:03:44,880 --> 00:03:49,110 and can even see the mapping between states and sequences. 88 00:03:49,110 --> 00:03:50,790 If the state is Engine_Failure, 89 00:03:50,790 --> 00:03:53,440 the corresponding sequence is Abort. 90 00:03:53,440 --> 00:03:55,920 Inclement_Weather corresponds to Hold, 91 00:03:55,920 --> 00:03:58,581 and Nominal corresponds to Launch. 92 00:03:58,581 --> 00:04:02,160 Thinking back to our scenario, it now becomes clear 93 00:04:02,160 --> 00:04:04,650 that the Engine_Failure state should be provided 94 00:04:04,650 --> 00:04:06,120 to the control structure 95 00:04:06,120 --> 00:04:09,000 in order to initiate the Abort sequence. 96 00:04:09,000 --> 00:04:11,820 In addition to increasing an algorithm's readability, 97 00:04:11,820 --> 00:04:12,653 I also mentioned 98 00:04:12,653 --> 00:04:15,630 that enumerated types can increase algorithmic correctness. 99 00:04:15,630 --> 00:04:16,740 Let's take a look at an example 100 00:04:16,740 --> 00:04:18,390 using the same control structure. 101 00:04:20,490 --> 00:04:23,070 As is often the case with rocket launch systems, 102 00:04:23,070 --> 00:04:25,470 you usually want the state of the launch to be determined 103 00:04:25,470 --> 00:04:28,680 by a predefined function as opposed to a human. 104 00:04:28,680 --> 00:04:31,680 Suppose again that you're a novice programmer at NASA 105 00:04:31,680 --> 00:04:33,450 but this time they've tasked you 106 00:04:33,450 --> 00:04:36,540 with writing the launch sequence controls for the rocket. 107 00:04:36,540 --> 00:04:37,440 You've been informed 108 00:04:37,440 --> 00:04:40,157 of an already existing function called get_state 109 00:04:40,157 --> 00:04:42,720 which returns the state of the launch. 110 00:04:42,720 --> 00:04:44,280 With this information, 111 00:04:44,280 --> 00:04:46,170 you get to work and write the control structure 112 00:04:46,170 --> 00:04:49,650 using unnamed numerical constants and retrieve the state 113 00:04:49,650 --> 00:04:53,373 of the launch from the get_state function or so you think. 114 00:04:54,240 --> 00:04:57,420 Unbeknownst to you, there are two get_state functions, 115 00:04:57,420 --> 00:04:59,610 one that returns the state of the launch 116 00:04:59,610 --> 00:05:01,050 and another that returns the state 117 00:05:01,050 --> 00:05:03,090 of the break room refrigerator. 118 00:05:03,090 --> 00:05:06,120 Unfortunately, you use the wrong get_state function, 119 00:05:06,120 --> 00:05:08,820 but because you've defined the state variables in integer 120 00:05:08,820 --> 00:05:10,920 and both functions return integers, 121 00:05:10,920 --> 00:05:13,400 the program will compile without errors. 122 00:05:13,400 --> 00:05:16,080 Obviously, we don't want the launch sequence 123 00:05:16,080 --> 00:05:19,080 being determined by the state of the break room refrigerator 124 00:05:19,080 --> 00:05:21,420 but what could we do to prevent this from happening? 125 00:05:21,420 --> 00:05:24,390 Well, this is where enumerated types can help us again. 126 00:05:24,390 --> 00:05:25,920 By using enumerated types, 127 00:05:25,920 --> 00:05:29,460 we can impose a type restriction on the state variable. 128 00:05:29,460 --> 00:05:31,320 Being of the enumerated type state, 129 00:05:31,320 --> 00:05:33,923 it can only assume one of three values, 130 00:05:33,923 --> 00:05:36,210 Engine_Failure, Inclement_Weather, or Nominal. 131 00:05:36,210 --> 00:05:38,520 If we try to assign a value to the state variable 132 00:05:38,520 --> 00:05:40,470 that's not one of these three values, 133 00:05:40,470 --> 00:05:42,600 the compiler will generate an error. 134 00:05:42,600 --> 00:05:44,160 This means that if a programmer were 135 00:05:44,160 --> 00:05:46,320 to call the wrong get_state function, 136 00:05:46,320 --> 00:05:48,960 that does not return a state type variable, 137 00:05:48,960 --> 00:05:50,820 the program will not compile. 138 00:05:50,820 --> 00:05:52,650 This eliminates any possibility 139 00:05:52,650 --> 00:05:54,600 of the state of the break room refrigerator 140 00:05:54,600 --> 00:05:56,760 determining the launch sequence. 141 00:05:56,760 --> 00:05:57,593 Now that we've seen 142 00:05:57,593 --> 00:05:59,400 how enumerated types can increase readability 143 00:05:59,400 --> 00:06:01,110 and algorithm correctness, 144 00:06:01,110 --> 00:06:04,110 it should be obvious how they became so popular. 145 00:06:04,110 --> 00:06:06,450 With the motivation of enumerated types behind us, 146 00:06:06,450 --> 00:06:07,770 let's head over to the next video, 147 00:06:07,770 --> 00:06:09,270 and we'll see their structure.