1 00:00:00,670 --> 00:00:06,100 I want to talk quickly about the idea of clean coal, something that we've talked about before, and 2 00:00:06,310 --> 00:00:10,390 this is a good example to do with something like a function. 3 00:00:10,900 --> 00:00:19,090 For example, let's say we had a function that perhaps checks if a number is even or odd. 4 00:00:20,390 --> 00:00:21,590 So let's say. 5 00:00:22,700 --> 00:00:33,050 Is odd for even as a function that takes in a number and we're going to hear you say, well, how are 6 00:00:33,050 --> 00:00:33,770 we going to check? 7 00:00:34,200 --> 00:00:45,050 We can use the modulo operator, I can say if no modulo two, which remember gives us the remainder. 8 00:00:45,320 --> 00:00:49,490 And if it's an even number, the remainder is always going to be zero. 9 00:00:49,700 --> 00:00:52,100 So this is going to tell us if it's an even number. 10 00:00:52,520 --> 00:00:54,200 So if it's an even number. 11 00:00:55,310 --> 00:00:57,740 Then I want you to return. 12 00:01:01,080 --> 00:01:06,420 And if it's not an even number, so let's say now. 13 00:01:08,100 --> 00:01:08,670 To. 14 00:01:09,670 --> 00:01:10,630 Doesn't equal. 15 00:01:12,270 --> 00:01:12,780 Zero. 16 00:01:12,960 --> 00:01:20,300 So this is an odd number, there's always a reminder in that case we return false. 17 00:01:21,150 --> 00:01:28,800 So maybe let's change this function to be more descriptive is even so, it's going to return true. 18 00:01:28,890 --> 00:01:31,530 If it's even false if it's not even. 19 00:01:32,550 --> 00:01:36,930 So in here, if I do, is even and give it 50. 20 00:01:38,230 --> 00:01:44,890 And I run, oh, I get an there because I have to make sure that I do an L if statement here. 21 00:01:46,880 --> 00:01:48,950 We have to print here so less to print. 22 00:01:50,590 --> 00:01:51,430 And click run. 23 00:01:53,070 --> 00:01:55,980 I get true if I do fifty one and click run. 24 00:01:57,240 --> 00:01:58,320 I get false. 25 00:01:58,380 --> 00:02:02,190 All right, it seems to be working, but how can we clean up this code? 26 00:02:02,820 --> 00:02:03,960 Well, here's the thing. 27 00:02:04,140 --> 00:02:09,780 We have some ways that we can actually make this a little bit tighter. 28 00:02:10,770 --> 00:02:20,160 For example, if we're only checking for is even we don't really need to check this again because. 29 00:02:21,170 --> 00:02:29,060 If no modulo two doesn't equal zero, that is if a number divided by two doesn't always have a remainder 30 00:02:29,060 --> 00:02:34,820 of zero, we always know that that's well, it's not going to be even so we don't even need the statement. 31 00:02:34,820 --> 00:02:36,680 We can just say else. 32 00:02:38,100 --> 00:02:40,530 Right, so that if I run this. 33 00:02:41,570 --> 00:02:47,300 This still works, sorry, so we cleaned up our code a little bit, but here's the thing. 34 00:02:47,360 --> 00:02:55,010 Another thing we could do is we know that return excess the function automatically, so we don't even 35 00:02:55,010 --> 00:02:56,410 need the EL statement. 36 00:02:57,020 --> 00:03:05,770 I can just say return here because it's only going to get to line five if this statement is not correct. 37 00:03:05,840 --> 00:03:12,680 So if it's not even well, I'm going to skip line four and go to line five so that if I run this. 38 00:03:13,810 --> 00:03:20,320 This still works, all right, but we can make this even cleaner, right, because right now we're just 39 00:03:20,320 --> 00:03:22,230 returning true or false. 40 00:03:23,200 --> 00:03:27,510 And usually if we have something like this, we're returning true false directly. 41 00:03:28,150 --> 00:03:29,530 There's usually a better way. 42 00:03:29,680 --> 00:03:33,700 And that's using an expression that evaluates to a boolean. 43 00:03:34,390 --> 00:03:43,030 What we can actually do is simply say, hey, remove all of this and I want you to just return like 44 00:03:43,030 --> 00:03:43,330 this. 45 00:03:44,480 --> 00:03:46,220 And if we run this. 46 00:03:47,800 --> 00:03:49,960 This still works, and if I do 50. 47 00:03:51,350 --> 00:03:58,400 This still works because what we're saying here is I want you to return whatever this value is, and 48 00:03:58,400 --> 00:04:02,540 this is an expression, right, that's going to evaluate true false directly. 49 00:04:03,320 --> 00:04:07,320 So we get the exact same result as we had before. 50 00:04:07,580 --> 00:04:09,560 But look how much cleaner that is. 51 00:04:09,860 --> 00:04:12,140 That is just nice, simple code. 52 00:04:12,140 --> 00:04:15,530 And as a developer, I can come in here and right away see that. 53 00:04:15,530 --> 00:04:15,920 All right. 54 00:04:15,950 --> 00:04:20,060 Is even is just performing this operation and returning true or false. 55 00:04:20,810 --> 00:04:23,530 So this is a good way to think about clean code. 56 00:04:23,540 --> 00:04:25,160 How can you clean up your code? 57 00:04:25,550 --> 00:04:27,640 And mind you, there's no perfect solution. 58 00:04:27,650 --> 00:04:34,400 You can always clean up code, but thinking like this really allows you to develop the way that advanced 59 00:04:34,400 --> 00:04:37,740 programmers think I'll see in the next Bhabhi.