1 00:00:01,060 --> 00:00:05,490 In this video we're going to start to clean up our set color function as I mentioned in the last video. 2 00:00:05,500 --> 00:00:10,060 There's definitely a little bit more appropriate syntax so we can use here to decide exactly what color 3 00:00:10,060 --> 00:00:11,430 we're trying to work on. 4 00:00:11,740 --> 00:00:16,750 So we could refactor this instead of having a series of IF statements we could instead use a switch 5 00:00:16,750 --> 00:00:18,570 statement that would look like this. 6 00:00:18,580 --> 00:00:25,790 I could instead say of instead the if statement right here I could say switch over color and then open 7 00:00:25,790 --> 00:00:30,440 up a curly brace and notice how I've still got the closing Curly race from our if statement we had just 8 00:00:30,440 --> 00:00:31,970 a moment ago right there. 9 00:00:31,970 --> 00:00:37,560 So then inside of here we could add in a case where color is equal to the string red. 10 00:00:37,700 --> 00:00:44,150 So if color is equal to red we can run this entire f statement right here so I'll tab that in like so 11 00:00:44,560 --> 00:00:48,500 and then for this to be a proper switch statement or a proper case. 12 00:00:48,500 --> 00:00:51,340 We always make sure we terminate the case with the return at the very end. 13 00:00:51,410 --> 00:00:55,820 So we don't fall through to any other case statements or in other cases that we might have inside of 14 00:00:55,820 --> 00:01:01,110 here so I'm going to say this really quickly and do another quick test just to make sure everything 15 00:01:01,110 --> 00:01:04,070 still works and yet it still looks good to me. 16 00:01:05,490 --> 00:01:10,680 Now even with this change having the nested IF statement here is still a little bit challenging to read. 17 00:01:10,770 --> 00:01:13,180 So there is yet another way that we could refactor this. 18 00:01:13,230 --> 00:01:17,550 This is a totally optional refactor but it definitely would increase the legibility of what's going 19 00:01:17,550 --> 00:01:18,020 on here. 20 00:01:18,780 --> 00:01:23,610 So I could say I could use something called a ternary expression a Turner expression is going to essentially 21 00:01:23,610 --> 00:01:30,210 check one statement and then if that statement evaluates to a truth you value it will run some immediate 22 00:01:30,450 --> 00:01:32,400 statement like in this case return. 23 00:01:32,400 --> 00:01:35,420 Otherwise it will run some other statement like set red. 24 00:01:35,490 --> 00:01:39,900 So essentially ternary is going to do exactly what we have with our if statement right here already. 25 00:01:40,020 --> 00:01:46,330 But it's just going to kind of condense down this logic so to do so I would write out red plus change 26 00:01:46,360 --> 00:01:51,840 greater than fifty five or read plus change less than zero. 27 00:01:52,160 --> 00:01:57,750 And here's where the ternary comes in we'll put in a question mark like so the question mark means that 28 00:01:57,750 --> 00:01:59,820 this is going to be the expression we want to run. 29 00:01:59,820 --> 00:02:06,470 If all this evaluates to a truth we value unfortunately we cannot return directly from a ternary statement. 30 00:02:06,490 --> 00:02:11,200 So instead we're just going to evaluate this expression to know like so that essentially means don't 31 00:02:11,200 --> 00:02:12,180 do anything. 32 00:02:12,220 --> 00:02:17,890 So if this is true right here if we are going to be outside outside of our bounds of red then don't 33 00:02:17,890 --> 00:02:20,310 do anything at all otherwise. 34 00:02:20,370 --> 00:02:24,970 So I'll put a call in there and that means if this is all SAE then do this. 35 00:02:24,970 --> 00:02:32,620 And in this case I want to update red by calling set red with red let's change now we can clean up that 36 00:02:32,620 --> 00:02:33,880 entire if statement. 37 00:02:33,910 --> 00:02:36,170 We're down to just one line of code. 38 00:02:36,370 --> 00:02:40,660 Now you can definitely argue that this is kind of hard to read right here especially if you're not familiar 39 00:02:40,660 --> 00:02:42,180 with ternary expressions. 40 00:02:42,310 --> 00:02:44,310 But once you start to get a little bit more familiar with them. 41 00:02:44,320 --> 00:02:48,400 Well you're going to be very comfortable kind of taking a glance at this and understanding what's going 42 00:02:48,400 --> 00:02:49,410 on. 43 00:02:49,610 --> 00:02:51,320 So I'm going to again save this. 44 00:02:51,460 --> 00:02:56,320 We'll do another quick test now if you see any error inside of your simulator right now. 45 00:02:56,330 --> 00:03:00,460 I can just guarantee you that you've got a typo somewhere inside of this line. 46 00:03:00,860 --> 00:03:04,820 So do double check this line of code double check the entire switch statement because if you're seeing 47 00:03:04,820 --> 00:03:06,840 an error that's only popping up right now. 48 00:03:06,890 --> 00:03:10,240 You definitely have a typo right here OK. 49 00:03:10,270 --> 00:03:11,910 So I'll do another quick test. 50 00:03:12,070 --> 00:03:15,120 I'll increase red Yep decrease red. 51 00:03:15,190 --> 00:03:15,450 Yep. 52 00:03:15,460 --> 00:03:16,480 Still works. 53 00:03:16,600 --> 00:03:17,950 Very good. 54 00:03:17,950 --> 00:03:23,020 Now we can easily essentially copy paste this statement right here and handle our other two colors of 55 00:03:23,020 --> 00:03:24,800 blue and green as well. 56 00:03:24,880 --> 00:03:32,550 So going to copy the case red paste it once and twice and I'm going to go through it and make sure I 57 00:03:32,550 --> 00:03:34,740 change all the color references inside of here. 58 00:03:35,280 --> 00:03:41,960 So first I'll update the string to be green all then do red red. 59 00:03:42,260 --> 00:03:43,820 Set red and red. 60 00:03:43,820 --> 00:03:45,260 So I have to do four more changes here. 61 00:03:45,260 --> 00:03:48,040 Make sure you do four changes on this line right here. 62 00:03:48,050 --> 00:03:56,120 My first one there's one there's two I'll do red right here inside of the parentheses. 63 00:03:56,120 --> 00:03:57,710 There's three. 64 00:03:57,710 --> 00:04:00,850 And then finally I will update set red to set Green. 65 00:04:00,950 --> 00:04:01,730 There's four. 66 00:04:01,730 --> 00:04:03,670 So make sure you do for updates as well. 67 00:04:04,780 --> 00:04:06,870 Now we can finish up with the last case. 68 00:04:07,000 --> 00:04:07,630 So I'll update. 69 00:04:07,630 --> 00:04:11,560 Case Red I'll change that string right there we want to handle a case of blue. 70 00:04:11,560 --> 00:04:14,640 Here and now I'll update this line again. 71 00:04:14,640 --> 00:04:16,980 Once again we'll have four separate changes. 72 00:04:17,100 --> 00:04:26,450 So here's one here is to here is three and there's four. 73 00:04:26,460 --> 00:04:32,390 I updated the setter as well so once again if you have an error at this point going forward chances 74 00:04:32,390 --> 00:04:34,330 are you made a typo somewhere inside of here. 75 00:04:34,400 --> 00:04:39,860 So I beg you please triple check please make sure that K screen has no mention of red whatsoever and 76 00:04:39,860 --> 00:04:43,610 make sure that case Blue has no mention of red whatsoever as well. 77 00:04:44,630 --> 00:04:46,130 The last thing we will do here. 78 00:04:46,130 --> 00:04:50,990 Technically we don't have to do this but usually by convention in a switch statement we add in kind 79 00:04:50,990 --> 00:04:54,020 of a default case at the very bottom. 80 00:04:54,140 --> 00:04:55,640 We say default like so. 81 00:04:55,660 --> 00:05:00,980 So if our color that we passed in did not match red green or blue we'll instead run this case right 82 00:05:00,980 --> 00:05:01,380 here. 83 00:05:01,430 --> 00:05:05,460 In which case we'll just return and say don't do anything okay. 84 00:05:05,500 --> 00:05:11,850 So now our set color function can handle all these different types of changes now that we've updated 85 00:05:11,850 --> 00:05:12,520 set color. 86 00:05:12,540 --> 00:05:19,320 We can go back down to all of our callbacks down here and we can update set blue and set Green to use 87 00:05:19,320 --> 00:05:24,200 set color instead so we'll start off with blue first. 88 00:05:24,310 --> 00:05:32,520 I'll change this to set color and set color again make sure you update both those then as a first argument 89 00:05:32,520 --> 00:05:39,850 will pass in blue as a string and as a second argument will pass in for the first one positive color 90 00:05:39,850 --> 00:05:47,640 increment and for the second one will do negative 1 times color increment all right. 91 00:05:47,650 --> 00:05:49,330 So once again make sure you've got set color set. 92 00:05:49,330 --> 00:05:56,740 Color blue and blue add strings and then positive color increment and negative color increment now will 93 00:05:56,740 --> 00:05:58,720 repeat that same process on set green. 94 00:05:58,720 --> 00:06:07,420 So we'll do a set color on both those string of green and then our second arguments will add in those 95 00:06:07,420 --> 00:06:15,160 commas and I'll do negative 1 times color increment and the first will be positive 1 All right and that's 96 00:06:15,160 --> 00:06:15,700 pretty much it. 97 00:06:16,300 --> 00:06:20,530 So once again I got a bigger because the changes we're making here you've made a lot of video courses 98 00:06:20,530 --> 00:06:25,510 over time and the changes we just made it's extremely likely likely that you might have just accidentally 99 00:06:25,510 --> 00:06:27,350 made a typo somewhere inside of here. 100 00:06:27,370 --> 00:06:32,710 So if your application is not working after this then I can guarantee you you got a typo somewhere inside 101 00:06:32,710 --> 00:06:35,380 of here or somewhere up inside that switch statement. 102 00:06:35,380 --> 00:06:39,570 So please double check your code make sure you don't have any typos inside of here. 103 00:06:39,570 --> 00:06:45,200 It's going to save this I'll flip back over and we can once again do a test. 104 00:06:45,200 --> 00:06:46,280 It's all increased red. 105 00:06:46,340 --> 00:06:47,640 Yep definitely works. 106 00:06:47,840 --> 00:06:49,220 Increase blue. 107 00:06:49,220 --> 00:06:52,800 Yep definitely works and increased green. 108 00:06:52,820 --> 00:06:53,930 Yep definitely works as well. 109 00:06:54,910 --> 00:06:56,510 All right so that's pretty much it. 110 00:06:56,540 --> 00:07:01,760 So we've now condensed down all of our state logic we don't have any additional callbacks or any additional 111 00:07:01,970 --> 00:07:04,180 hard to read functions going on inside of here. 112 00:07:04,250 --> 00:07:07,970 We have only calls to set color and then inside of set color. 113 00:07:07,970 --> 00:07:12,680 We've got some well kind of challenging to read stuff but I'd say overall once you kind of get used 114 00:07:12,680 --> 00:07:15,070 to the syntax you can definitely understand what's going on. 115 00:07:16,180 --> 00:07:16,490 All right. 116 00:07:16,500 --> 00:07:18,300 So now we've fixed all this stuff up. 117 00:07:18,310 --> 00:07:19,590 Let's take a quick pause right here. 118 00:07:19,600 --> 00:07:25,090 There is technically one more little optimization we could make to this function so we'll take another 119 00:07:25,090 --> 00:07:26,710 pause when we come back the next video. 120 00:07:26,710 --> 00:07:31,120 We're going to take a look at one last way that we can fix all this stuff up just a little bit more 121 00:07:31,330 --> 00:07:32,700 than it is already. 122 00:07:32,700 --> 00:07:33,850 So I'll see you in just a minute.