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