1 00:00:00,690 --> 00:00:01,800 -: I wanna talk quickly 2 00:00:01,800 --> 00:00:03,570 about the idea of clean code. 3 00:00:03,570 --> 00:00:05,670 Something that we've talked about before, 4 00:00:05,670 --> 00:00:08,520 and this is a good example to do 5 00:00:08,520 --> 00:00:10,920 with something like a function. 6 00:00:10,920 --> 00:00:13,230 For example, let's say we had a function, 7 00:00:13,230 --> 00:00:18,230 that perhaps, checks if a number is even or odd. 8 00:00:20,400 --> 00:00:25,400 So let's say is odd or even as a function, 9 00:00:27,690 --> 00:00:29,850 that takes in a number, 10 00:00:29,850 --> 00:00:31,860 and we're going to here, 11 00:00:31,860 --> 00:00:34,140 say, well, how are we going to check? 12 00:00:34,140 --> 00:00:36,270 Well, we can use the Modulo operator. 13 00:00:36,270 --> 00:00:40,657 I can say if, number, modulo, two, 14 00:00:42,960 --> 00:00:45,360 which remember, gives us the remainder, 15 00:00:45,360 --> 00:00:46,950 and if it's an even number, 16 00:00:46,950 --> 00:00:49,710 the remainder is always going to be zero. 17 00:00:49,710 --> 00:00:52,530 So this is going to tell us if it's an even number. 18 00:00:52,530 --> 00:00:54,363 So if it's an even number, 19 00:00:55,320 --> 00:01:00,003 then I want you to return true. 20 00:01:01,020 --> 00:01:05,280 And, if it's not an even number, 21 00:01:05,280 --> 00:01:10,280 so let's say num, two, doesn't equal, zero. 22 00:01:12,990 --> 00:01:15,690 So if this is an odd number, 23 00:01:15,690 --> 00:01:17,700 there's always a remainder. 24 00:01:17,700 --> 00:01:21,150 In that case, we return false. 25 00:01:21,150 --> 00:01:23,520 So maybe let's change this function 26 00:01:23,520 --> 00:01:27,360 to be more descriptive is even. 27 00:01:27,360 --> 00:01:28,890 So it's going to return true, 28 00:01:28,890 --> 00:01:31,653 if it's even, false, if it's not even. 29 00:01:32,520 --> 00:01:35,760 So in here, if I do is even 30 00:01:35,760 --> 00:01:37,143 and give it 50, 31 00:01:38,250 --> 00:01:40,530 and I run, oh, I get an error, 32 00:01:40,530 --> 00:01:43,530 because I have to make sure that I do an 33 00:01:43,530 --> 00:01:45,033 elif statement here. 34 00:01:46,860 --> 00:01:47,880 We have to print here, 35 00:01:47,880 --> 00:01:49,113 so let's do print, 36 00:01:50,610 --> 00:01:51,573 and click run. 37 00:01:53,130 --> 00:01:53,963 I get true. 38 00:01:53,963 --> 00:01:58,410 If I do 51 and click run, I get false. 39 00:01:58,410 --> 00:01:59,243 Alright. 40 00:01:59,243 --> 00:02:00,076 It seems to be working, 41 00:02:00,076 --> 00:02:02,820 but how can we clean up this code? 42 00:02:02,820 --> 00:02:04,110 Well, here's the thing. 43 00:02:04,110 --> 00:02:08,910 We have some ways that we can actually make this 44 00:02:08,910 --> 00:02:10,770 a little bit tighter. 45 00:02:10,770 --> 00:02:11,970 For example, 46 00:02:11,970 --> 00:02:14,070 if we're only checking for 47 00:02:14,070 --> 00:02:19,070 is even, we don't really need to check this again, 48 00:02:19,710 --> 00:02:24,540 because, if number, modulo, two, doesn't equal zero, 49 00:02:24,540 --> 00:02:27,510 that is, if a number, divided by two, 50 00:02:27,510 --> 00:02:29,460 doesn't always have a remainder of zero, 51 00:02:29,460 --> 00:02:31,260 we always know that that's, well, 52 00:02:31,260 --> 00:02:32,550 it's not gonna be even. 53 00:02:32,550 --> 00:02:34,830 So we don't even need this statement. 54 00:02:34,830 --> 00:02:36,873 We can just say else. 55 00:02:38,160 --> 00:02:39,270 Right? 56 00:02:39,270 --> 00:02:42,540 So that if I run this, this still works. 57 00:02:42,540 --> 00:02:45,421 So, alright, so we cleaned up our code a little bit. 58 00:02:45,421 --> 00:02:47,340 But here's the thing. 59 00:02:47,340 --> 00:02:49,410 Another thing we could do, 60 00:02:49,410 --> 00:02:51,810 is we know that return exits 61 00:02:51,810 --> 00:02:54,030 the function automatically. 62 00:02:54,030 --> 00:02:57,060 So we don't even need the else statement. 63 00:02:57,060 --> 00:02:59,670 I can just say, return here, 64 00:02:59,670 --> 00:03:03,360 because it's only going to get to line five, 65 00:03:03,360 --> 00:03:05,880 if this statement is not correct. 66 00:03:05,880 --> 00:03:08,550 So if, it's not even, well, 67 00:03:08,550 --> 00:03:10,320 I'm going to skip line four, 68 00:03:10,320 --> 00:03:11,490 and go to line five, 69 00:03:11,490 --> 00:03:15,420 so that if I run this, this still works. 70 00:03:15,420 --> 00:03:16,410 Alright, but, 71 00:03:16,410 --> 00:03:18,960 we can make this even cleaner, right? 72 00:03:18,960 --> 00:03:19,860 Because right now, 73 00:03:19,860 --> 00:03:22,383 we're just returning true or false. 74 00:03:23,220 --> 00:03:25,410 And usually, if you have something like this, 75 00:03:25,410 --> 00:03:28,140 we're returning true false directly, 76 00:03:28,140 --> 00:03:29,700 there's usually a better way, 77 00:03:29,700 --> 00:03:31,890 and that's using an expression 78 00:03:31,890 --> 00:03:34,380 that evaluates to a boolean. 79 00:03:34,380 --> 00:03:36,990 What we can actually do, is simply say, 80 00:03:36,990 --> 00:03:39,300 hey, remove all of this, 81 00:03:39,300 --> 00:03:43,473 and I want you to just return, like this. 82 00:03:44,520 --> 00:03:46,413 And if we run this, 83 00:03:47,820 --> 00:03:48,870 this still works. 84 00:03:48,870 --> 00:03:50,103 And if I do 50, 85 00:03:51,330 --> 00:03:52,770 this still works. 86 00:03:52,770 --> 00:03:54,660 Because what we're saying here, 87 00:03:54,660 --> 00:03:56,100 is I want you to return 88 00:03:56,100 --> 00:03:58,200 whatever this value is, 89 00:03:58,200 --> 00:03:59,880 and this is an expression, right? 90 00:03:59,880 --> 00:04:03,330 That's going to evaluate true false directly. 91 00:04:03,330 --> 00:04:05,760 So we get the exact same result, 92 00:04:05,760 --> 00:04:07,590 as we had before, 93 00:04:07,590 --> 00:04:09,870 but, look how much cleaner that is. 94 00:04:09,870 --> 00:04:12,120 That is just nice, simple code, 95 00:04:12,120 --> 00:04:13,530 and as a developer, 96 00:04:13,530 --> 00:04:14,370 I can come in here, 97 00:04:14,370 --> 00:04:15,960 and right away, see that, alright, 98 00:04:15,960 --> 00:04:18,690 is even is just performing this operation, 99 00:04:18,690 --> 00:04:20,820 and returning true or false. 100 00:04:20,820 --> 00:04:23,490 So this is a good way to think about clean code. 101 00:04:23,490 --> 00:04:25,590 How can you clean up your code? 102 00:04:25,590 --> 00:04:27,600 And mind you, there's no perfect solution. 103 00:04:27,600 --> 00:04:29,520 You can always clean up code, 104 00:04:29,520 --> 00:04:31,140 but thinking like this, 105 00:04:31,140 --> 00:04:32,880 really allows you to develop 106 00:04:32,880 --> 00:04:36,210 the way that advanced programmers think. 107 00:04:36,210 --> 00:04:37,129 I'll see you in the next one. 108 00:04:37,129 --> 00:04:37,962 Bye-bye.