1 00:00:00,810 --> 00:00:04,330 So, before we get too far into the mechanics of programming, 2 00:00:04,330 --> 00:00:07,210 let's talk about the biggest subject of all design. 3 00:00:07,210 --> 00:00:09,750 I don't mean how your software looks on a website or a phone, though. 4 00:00:09,750 --> 00:00:13,480 I'm talking about the organization thinking and layout of your code. 5 00:00:14,550 --> 00:00:17,770 In previous courses, most of our code has been in a single file. 6 00:00:17,770 --> 00:00:19,960 This is fine for small amounts of code, but 7 00:00:19,960 --> 00:00:23,320 you'll quickly see that we don't always have small code footprints. 8 00:00:23,320 --> 00:00:26,200 Libraries and tools can quickly reach hundreds, if not thousands, 9 00:00:26,200 --> 00:00:27,510 of lines of code. 10 00:00:27,510 --> 00:00:30,250 That's just not something you want to read all in one file. 11 00:00:30,250 --> 00:00:32,690 We've all been there, right, a document that just keeps scrolling and 12 00:00:32,690 --> 00:00:34,330 scrolling and scrolling. 13 00:00:34,330 --> 00:00:38,020 Now, imagine you're on line 595 of some script and 14 00:00:38,020 --> 00:00:41,150 see a function reference from all the way up at line 18. 15 00:00:41,150 --> 00:00:43,990 IDEs and other editors can reduce the amount of scrolling, 16 00:00:43,990 --> 00:00:45,910 but wouldn't it be better if you didn't have to do that? 17 00:00:45,910 --> 00:00:49,430 And this is the first case that software design solves. 18 00:00:49,430 --> 00:00:50,900 Well, it helps at least. 19 00:00:50,900 --> 00:00:53,010 We can break our code up into modules or files. 20 00:00:53,010 --> 00:00:57,710 By putting code into logical groupings we can make it easier to take it all in and 21 00:00:57,710 --> 00:01:00,150 think about what we need to do to do our job. 22 00:01:00,150 --> 00:01:02,430 This logical grouping goes even further though. 23 00:01:02,430 --> 00:01:05,350 We already grouped related lines of code together into functions. 24 00:01:05,350 --> 00:01:07,610 Now with classes we have another layer of grouping. 25 00:01:07,610 --> 00:01:10,600 If you have multiple functions that all work together think about putting them 26 00:01:10,600 --> 00:01:12,720 into a class. 27 00:01:12,720 --> 00:01:16,570 OOP provides us with so many tools that it's often hard to know which ones to use, 28 00:01:16,570 --> 00:01:18,520 or, if you should even use any of them. 29 00:01:18,520 --> 00:01:20,280 I don't want to just dump a ton of terms and 30 00:01:20,280 --> 00:01:23,680 features on you, so, I'll be bringing them up as we go through the course. 31 00:01:23,680 --> 00:01:25,360 I'll also be talking about how to use and 32 00:01:25,360 --> 00:01:27,920 think about these features in the design of your software. 33 00:01:27,920 --> 00:01:30,090 But I can only influence your thinking so far. 34 00:01:30,090 --> 00:01:32,950 I need you to also consider how you'd use each of these things 35 00:01:32,950 --> 00:01:34,860 in the design of your own software. 36 00:01:34,860 --> 00:01:38,410 An often overlooked truth of software development is that there's rarely any 37 00:01:38,410 --> 00:01:40,500 one correct way to solve a problem or 38 00:01:40,500 --> 00:01:45,150 design an algorithm, so like I said this organization starts at its 39 00:01:45,150 --> 00:01:48,710 barest of bones with organizing code into functions and classes. 40 00:01:48,710 --> 00:01:51,650 Let's think back to the shopping list app that we built in Python basics and 41 00:01:51,650 --> 00:01:52,420 Python collections. 42 00:01:52,420 --> 00:01:55,385 [SOUND] We had a list that held the things we wanted to shop for, 43 00:01:55,385 --> 00:01:58,583 we had functions to add things to the list, reorganize the list, 44 00:01:58,583 --> 00:02:01,400 and remove things we didn't need or had already bought. 45 00:02:01,400 --> 00:02:04,131 Well right there I see something that makes sense for a class, 46 00:02:04,131 --> 00:02:05,345 the ShoppingList itself. 47 00:02:05,345 --> 00:02:08,389 I can see going a step further though and making each thing on the list and 48 00:02:08,389 --> 00:02:10,320 instance of some other class. 49 00:02:10,320 --> 00:02:11,740 Maybe this class would hold the name for 50 00:02:11,740 --> 00:02:14,960 the thing, how many that I wanted, what store it was at, who knows. 51 00:02:14,960 --> 00:02:18,000 There are many options for the functionality of this class. 52 00:02:19,400 --> 00:02:22,410 And that brings me to another point, ruthless editing. 53 00:02:22,410 --> 00:02:24,940 As you build classes and methods and functionality, 54 00:02:24,940 --> 00:02:29,800 keep in mind a very common acronym, YAGNI, which stands for, you ain't gonna need it. 55 00:02:29,800 --> 00:02:33,020 Keep your code lean and mean and you'll find it easier to use and 56 00:02:33,020 --> 00:02:34,790 better for building awesome things with. 57 00:02:34,790 --> 00:02:38,500 It's always a good idea to keep it simple or as the Zen of Python says, 58 00:02:38,500 --> 00:02:43,060 if implementation is hard to explain, it's a bad idea. Often you'll find that you can 59 00:02:43,060 --> 00:02:46,190 approach software design like you would the design of something in the real world. 60 00:02:46,190 --> 00:02:48,440 Just like you wouldn't add a fish radar to your screwdriver, 61 00:02:48,440 --> 00:02:52,140 you probably shouldn't add a function to send email to your calculator app, 62 00:02:52,140 --> 00:02:55,410 practicality and logical thinking will lead to better software every time. 63 00:02:56,630 --> 00:03:00,980 As part of that practicality, remember you're writing Python not Java or C# or 64 00:03:00,980 --> 00:03:02,200 any other language. 65 00:03:02,200 --> 00:03:04,960 If you come to Python from one of those languages, you might expect me to talk 66 00:03:04,960 --> 00:03:09,040 about getters and setters or access controls on methods and attributes. 67 00:03:09,040 --> 00:03:10,920 Python doesn't rely on these design features. 68 00:03:10,920 --> 00:03:14,140 And you'll rarely find a place where they're absolutely needed in Python. 69 00:03:14,140 --> 00:03:16,970 Stick to writing simple, clean code and as little code as possible, 70 00:03:16,970 --> 00:03:18,830 and you'll find yourself a happier programmer. 71 00:03:19,890 --> 00:03:22,180 All right, I think that's enough talking about design for now. 72 00:03:22,180 --> 00:03:24,760 Go back and look at the code we've written together previously and 73 00:03:24,760 --> 00:03:27,340 see if you can find places to add simple classes. 74 00:03:27,340 --> 00:03:29,720 Even if you can't add them right now, keep those places in mind for 75 00:03:29,720 --> 00:03:31,560 when you hit further through this course. 76 00:03:31,560 --> 00:03:34,790 I'm sure you'll find all sorts of places to improve our old code. 77 00:03:34,790 --> 00:03:36,710 Now though, let's get into the fun stuff and 78 00:03:36,710 --> 00:03:39,960 start talking about the real wealth of object oriented design, inheritance.