1 00:00:00,450 --> 00:00:08,900 It's happening our next developer fundamentals, and that is what is good code, I mean, what is good 2 00:00:08,940 --> 00:00:14,190 that seems so vague, like when somebody says I'm a good coder, I'm an excellent programmer, he's 3 00:00:14,190 --> 00:00:15,420 a senior programmer. 4 00:00:15,510 --> 00:00:17,100 He's an expert programmer. 5 00:00:17,100 --> 00:00:18,120 He's a tech lead. 6 00:00:18,120 --> 00:00:19,520 He's a genius. 7 00:00:19,530 --> 00:00:21,560 What what does that really mean? 8 00:00:22,410 --> 00:00:29,310 Well, you can narrow it down to these simple statements, what is clean, good code? 9 00:00:29,640 --> 00:00:30,720 I kind of gave it away. 10 00:00:30,870 --> 00:00:32,910 The first one is clean. 11 00:00:33,860 --> 00:00:41,570 That is, we want to make sure our code is following a style that lets say the Python community endorses, 12 00:00:41,750 --> 00:00:43,850 are we following the best practices? 13 00:00:43,850 --> 00:00:47,210 And Python has this great feature of the format. 14 00:00:47,420 --> 00:00:54,230 And Python has standard ways of using spaces to make sure that our code is clean. 15 00:00:54,380 --> 00:01:02,630 I'm not using unnecessarily ugly spaces like this or maybe making lists really weird and funky, maybe 16 00:01:02,630 --> 00:01:04,460 no spaces in between here. 17 00:01:04,460 --> 00:01:09,080 Maybe I have a lot of random comments over here. 18 00:01:09,560 --> 00:01:11,210 We're keeping our code clean. 19 00:01:11,210 --> 00:01:20,210 We make sure that every line that we have is easily readable, but also we don't have any extra stuff 20 00:01:20,210 --> 00:01:21,470 that we don't need in there. 21 00:01:22,360 --> 00:01:31,180 And this also relates to the idea of readability now, readability means the ability to want to read 22 00:01:31,180 --> 00:01:35,820 your own code maybe two years down the road, you're going to come back and look at the code. 23 00:01:35,840 --> 00:01:41,320 Are you going to be able to understand it if you work in teams or for companies and other co-workers? 24 00:01:41,320 --> 00:01:42,490 Come and look at your code. 25 00:01:42,880 --> 00:01:44,590 Is it easy to understand? 26 00:01:45,540 --> 00:01:52,720 And here, this is obviously personal preference, but most of the time, the ideas are are simple. 27 00:01:53,070 --> 00:01:57,150 For example, I'm using names here that make sense. 28 00:01:57,780 --> 00:02:03,030 If I had variable names that don't make sense, will be really hard for somebody to read my code. 29 00:02:03,630 --> 00:02:08,280 Maybe people will be confused why I have this print statement here. 30 00:02:08,970 --> 00:02:17,070 In that case, maybe I should comment in here being like I need a new line after every row. 31 00:02:18,880 --> 00:02:24,880 Or maybe they'll be confused why we need this as well, so depending on your style, you might want 32 00:02:24,880 --> 00:02:32,620 to add comments, make sure that a naming of your variables are good, and maybe making sure that it's 33 00:02:32,620 --> 00:02:34,810 not only you that understands the code. 34 00:02:36,190 --> 00:02:43,600 The other one is the idea of predictability, and this is my favorite one, that is sometimes people 35 00:02:43,600 --> 00:02:52,690 try to be really clever with their code, trying to have the most compact code or using the newest features 36 00:02:52,690 --> 00:03:00,070 or some really obscure tools that or functions that are not very common just to look well, frankly, 37 00:03:00,070 --> 00:03:00,520 smart. 38 00:03:01,000 --> 00:03:07,120 But you want to have code that makes sense, that does things just one thing really well. 39 00:03:07,630 --> 00:03:12,250 Now, this code is quite small, so it's quite predictable. 40 00:03:12,790 --> 00:03:18,000 You know, it just has one pass through and it's easy to predict what's going to happen. 41 00:03:18,220 --> 00:03:23,980 We're going to print, but as we get more and more into the course, you'll see that our code is going 42 00:03:23,980 --> 00:03:28,810 to get larger and larger and having predictable, easy to understand, code is really important. 43 00:03:29,500 --> 00:03:35,780 And then finally, this is an important principle that you're here everywhere is deha. 44 00:03:35,800 --> 00:03:37,660 Why do not repeat yourself? 45 00:03:38,050 --> 00:03:43,950 You don't want to have code that you're constantly repeating yourself over and over again. 46 00:03:43,960 --> 00:03:51,340 This is a small example, but it's very easy as a programmer to say, oh, I'm going to copy this and 47 00:03:51,340 --> 00:03:54,520 then I'm going to run this again so that if I click run. 48 00:03:55,510 --> 00:03:56,080 Look at that. 49 00:03:56,080 --> 00:03:57,330 I have two trees. 50 00:03:57,340 --> 00:03:59,140 Now, this is amazing. 51 00:04:00,840 --> 00:04:07,770 But maybe that's not the best idea, you now have twenty eight lines of code and people might be confused 52 00:04:07,770 --> 00:04:15,630 why we're doing this twice, maybe instead we want to programmatically have a counter here of maybe 53 00:04:15,630 --> 00:04:21,600 saying running this twice or maybe using something like functions, which we'll learn later to make 54 00:04:21,600 --> 00:04:25,020 sure that we can repeat this process over and over and over. 55 00:04:26,310 --> 00:04:31,050 Now, keep these in the back of your mind, because throughout the course we're going to explore these 56 00:04:31,050 --> 00:04:31,620 topics. 57 00:04:32,780 --> 00:04:38,150 But let's say I just came on to this code and I wanted you to clean it up a bit. 58 00:04:38,900 --> 00:04:42,710 Again, this is just personal preference, but these are some of the things that I would do. 59 00:04:43,400 --> 00:04:49,150 One is here pixels equal to one instead. 60 00:04:49,190 --> 00:04:50,500 This is kind of confusing. 61 00:04:50,510 --> 00:04:56,240 I'm just going to say, if there is a pixel here and I know that pixels are either a zero on one, then 62 00:04:56,240 --> 00:05:00,200 print here because this is a truthy value that we'll just evaluate to. 63 00:05:00,200 --> 00:05:00,490 True. 64 00:05:01,040 --> 00:05:05,120 So that just kind of cleaned out my code a little bit better next. 65 00:05:05,150 --> 00:05:11,990 Another thing that I might do, maybe I want to make this more extensible, which might mean doing something 66 00:05:11,990 --> 00:05:21,860 like, say, filled variable that equals to the star and then maybe empty, which equals to an empty 67 00:05:21,860 --> 00:05:26,000 string and change these to Phil. 68 00:05:28,170 --> 00:05:36,420 And empty now this is extra line of code, but for example, if we're doing this multiple times instead 69 00:05:36,420 --> 00:05:42,450 of perhaps maybe we're running print over and over and over, or maybe we have another line of print 70 00:05:42,720 --> 00:05:43,320 over here. 71 00:05:44,780 --> 00:05:50,810 And another line of print over here and there, then maybe another line of print over here that is anti. 72 00:05:52,760 --> 00:05:59,210 Instead of me having to change five locations here, I can now change just this part and maybe instead 73 00:05:59,210 --> 00:06:03,020 of star I want to display, I don't know, dollar sign. 74 00:06:04,320 --> 00:06:05,640 And if I run this. 75 00:06:07,290 --> 00:06:13,710 All right, I get a little different image here, but again, I was able to just change the variable 76 00:06:13,710 --> 00:06:18,120 and the variable was used to fill out whatever I needed done. 77 00:06:18,990 --> 00:06:24,570 Now, these are all things that you do need a bit of experience to get used to, but will hopefully 78 00:06:24,570 --> 00:06:26,100 explore this throughout the course. 79 00:06:26,340 --> 00:06:30,960 And by the end of it, you'll have all these best practices in mind. 80 00:06:31,830 --> 00:06:32,760 I'll see in the next one.