1 00:00:00,926 --> 00:00:04,379 If you've been reading up on how to improve your code, 2 00:00:04,379 --> 00:00:07,395 you've probably seen this acronym all ready. 3 00:00:07,395 --> 00:00:12,945 KISS or keep it simple silly, helps developers write code that is easier for 4 00:00:12,945 --> 00:00:16,423 others to understand, including future you. 5 00:00:16,423 --> 00:00:20,596 It's a common problem for developers to come back to their 6 00:00:20,596 --> 00:00:25,377 own projects in the future, and not be able to read their own code. 7 00:00:25,377 --> 00:00:31,116 One important note, KISS doesn't always mean less code, 8 00:00:31,116 --> 00:00:34,228 it just means more simple code. 9 00:00:34,228 --> 00:00:38,323 You can do some pretty cool and complicated things with programming. 10 00:00:38,323 --> 00:00:42,995 But if it takes a paragraph of code comments to describe what's happening, 11 00:00:42,995 --> 00:00:45,116 your code may be too complicated. 12 00:00:45,116 --> 00:00:49,721 And there may be times when you can't simplify a complicated piece of code, and 13 00:00:49,721 --> 00:00:50,625 that's okay. 14 00:00:50,625 --> 00:00:53,119 Just tackle the areas you can, and 15 00:00:53,119 --> 00:00:56,823 add comments to help describe what's happening. 16 00:00:56,823 --> 00:01:00,619 Let's check this out with some code. 17 00:01:00,619 --> 00:01:04,782 If you haven't already, open the KISS file to follow along. 18 00:01:04,782 --> 00:01:07,622 Here we have some code that needs tweaking. 19 00:01:07,622 --> 00:01:13,580 I've listed out a few common ways developers overcomplicate their code. 20 00:01:13,580 --> 00:01:18,925 The first complication that comes to my attention, is over commenting. 21 00:01:18,925 --> 00:01:22,058 Don't get me wrong, I love code comments. 22 00:01:22,058 --> 00:01:27,591 And I wish developers use them more, but you can go too far. 23 00:01:27,591 --> 00:01:32,648 Use code comments to describe elements of your code that aren't easy to understand, 24 00:01:32,648 --> 00:01:34,766 just by reading through your code. 25 00:01:34,766 --> 00:01:38,975 This will make it easier for other developers to understand, and 26 00:01:38,975 --> 00:01:41,052 use code that you've written. 27 00:01:41,052 --> 00:01:46,831 Let's go through and remove the unnecessary code comments in our file. 28 00:01:46,831 --> 00:01:50,096 Line 4, can go. 29 00:01:50,096 --> 00:01:55,757 It states what we already see, that this variable holds the number 5. 30 00:01:55,757 --> 00:02:00,316 I don't think that this is a piece of code, that would be too complicated for 31 00:02:00,316 --> 00:02:02,396 another developer to figure out. 32 00:02:02,396 --> 00:02:06,951 The next set of code comments can be easily remedied by improving the name of 33 00:02:06,951 --> 00:02:07,902 the function. 34 00:02:07,902 --> 00:02:12,564 Naming function is something descriptive, is a good habit to get into. 35 00:02:12,564 --> 00:02:14,546 Let's remove the comments, and 36 00:02:14,546 --> 00:02:18,385 rename the function to something like check greater than 10. 37 00:02:18,385 --> 00:02:20,463 This is much more descriptive. 38 00:02:34,049 --> 00:02:39,395 Inside of this function, is another set of code comments that aren't necessary. 39 00:02:39,395 --> 00:02:43,015 Since the code isn't complicated enough to keep them. 40 00:02:43,015 --> 00:02:47,562 These are the kinds of comments I might write when building a project, and 41 00:02:47,562 --> 00:02:49,697 then remove once it's finished. 42 00:02:53,201 --> 00:02:57,170 Really the only code comments I might keep, is for 43 00:02:57,170 --> 00:03:00,299 the larger go_to_movies function. 44 00:03:00,299 --> 00:03:03,783 Since this function is larger, it will make it easier for 45 00:03:03,783 --> 00:03:07,498 others to know what this function does, right off the back. 46 00:03:07,498 --> 00:03:11,209 I just need to remove the first line, since it's redundant. 47 00:03:14,919 --> 00:03:17,143 The rest of the comments can go. 48 00:03:31,862 --> 00:03:33,227 Nice work. 49 00:03:33,227 --> 00:03:35,700 Let's tackle the next hurdle. 50 00:03:35,700 --> 00:03:40,247 Back at the top in our check greater than 10 function, 51 00:03:40,247 --> 00:03:44,423 we can simplify this logic into one line like this. 52 00:03:53,529 --> 00:03:54,942 Magic, right? 53 00:03:54,942 --> 00:04:01,261 Don't forget that comparisons already give you a true or false response. 54 00:04:01,261 --> 00:04:04,592 So you can just return the value instead. 55 00:04:04,592 --> 00:04:09,438 Comparison logic can be powerful, but also easily forgotten. 56 00:04:09,438 --> 00:04:15,690 Next, let's tackle the large code block inside of the go_to _movies function. 57 00:04:15,690 --> 00:04:17,880 When it comes to functions, 58 00:04:17,880 --> 00:04:23,736 it's typically easier to commit to one task per function for readability. 59 00:04:23,736 --> 00:04:27,276 Now, there may be times when this isn't possible, 60 00:04:27,276 --> 00:04:31,472 but that's where comments can come into play to help you out. 61 00:04:31,472 --> 00:04:34,743 Let's break the info here into two functions. 62 00:04:34,743 --> 00:04:36,430 One to choose our movie. 63 00:04:39,070 --> 00:04:47,793 And the second, to get our snacks Then go_to_movies, 64 00:04:47,793 --> 00:04:52,756 can utilize these new functions and print out the message. 65 00:04:52,756 --> 00:04:57,501 Let's call the first function, random_movie, and return the chosen movie. 66 00:05:08,679 --> 00:05:10,170 We can cut. 67 00:05:15,823 --> 00:05:22,376 And paste, And then return our movie. 68 00:05:22,376 --> 00:05:25,698 And go to movie, we can now call this function and 69 00:05:25,698 --> 00:05:29,195 save the result in a variable called movie choice. 70 00:05:35,868 --> 00:05:39,718 The second function can be called buy_snacks. 71 00:05:47,084 --> 00:05:50,629 It will need a money parameter. 72 00:05:50,629 --> 00:05:53,806 Let's move the logic into this new function, 73 00:05:53,806 --> 00:05:56,421 and then return our purchase snacks. 74 00:06:22,229 --> 00:06:26,945 Inside of go_to_movies, we can use a variable called purchased_snacks. 75 00:06:32,242 --> 00:06:35,915 To save the value of the buy_snacks function. 76 00:06:37,328 --> 00:06:41,543 Don't forget to pass in the money argument, to your new function. 77 00:06:43,516 --> 00:06:47,305 Now, this code block is a lot more readable. 78 00:06:56,510 --> 00:07:01,012 The last section here, includes nested for loops. 79 00:07:01,012 --> 00:07:05,570 Nesting loops may not seem like a big deal with small data sets. 80 00:07:05,570 --> 00:07:08,126 But the larger your data set gets, 81 00:07:08,126 --> 00:07:11,751 the longer this double looping is going to take. 82 00:07:11,751 --> 00:07:13,775 When you run this in the console. 83 00:07:19,841 --> 00:07:24,443 You can see that it first grabs one of the dictionaries, and 84 00:07:24,443 --> 00:07:26,479 then each key value pair. 85 00:07:26,479 --> 00:07:30,451 Can you imagine if you had to work through 1000 pets, 86 00:07:30,451 --> 00:07:36,014 this double looping could take a while and would really slow down your program. 87 00:07:36,014 --> 00:07:43,006 Let's keep this to one loop and print out each attribute, Here instead. 88 00:08:08,863 --> 00:08:14,103 It's common to write more code than you need, especially when new to programming. 89 00:08:14,103 --> 00:08:18,543 That's why it's always good to review your code and old projects. 90 00:08:18,543 --> 00:08:22,890 Great job tackling some common ways to make your code more simple.