Understanding Why You Need Classes
00:11 When you’re programming a larger application that works with data, maybe different kinds of data, these questions may arise: How do you store the data while the program’s running? Do you keep it in a list?
00:45 How do you operate on that data? So do you just create copies of lists? How do you know whether one dictionary represents one kind of data or another? Do you have some kind of check to make sure if they have certain properties?
00:57 And where do you put that code that operates on certain data? Again, do you spin that up into files? Do you just keep it all together with comments? These are all things that classes can really help with.
01:10 Now again, classes really are to make our life easier as a developer. It doesn’t mean you have to use them everywhere, but there are many situations in which classes can be really helpful to make your code easier to understand, easier to reason about, and easier to write.
Say you have three people here, and you have data associated with these people. In this example, they’re just kept in lists. So for instance, you have
kirk here, and you can tell by the list what’s going on.
01:41 The first item seems to be the name, the full name. The second item is probably the age. The third item is the role. And the fourth item, we’re not really sure what it is. Maybe it’s an ID number.
02:05 The last one, though, there’s something odd going on. It seems to go straight from the name to the role. Now, this can present a very tricky problem if you’re storing your data that way, because how do you know immediately whether items are missing?
02:19 You could look at the length, or you could check that the second item is a number. That said, those types of operations are very prone to errors because maybe your list size changes, or you have an extra field, and where do you put that field?
Not to mention when you’re actually trying to reference this data, looking at the code that references a list, it just says
kirk. That doesn’t really tell you a lot about what’s happening here.
But then for
mccoy, you’re getting
"Chief Medical Officer", which is clearly not an age. Whereas if you had used classes—you’re going to get into actually how do that later on in the course—but if you had used classes, the same operation would look something like this.
kirk.age, and this immediately tells you, I’m getting the age from
kirk. This returns
35 in the case of
spock, and in
mccoy, it will give you an
AttributeError because it’s saying, I don’t have the age from
mccoy. Whereas with the list, it would just return a string value, and it’s not really telling you anything about the object itself, the person that it represents.
But even still, looking at this code, you’re not really sure what’s going on. Maybe
point is actually a list of different points, and
point means the first point, and
point means the second point.
04:27 If someone’s not familiar with your code, they might get confused while looking at it, and you yourself might get confused, because it’s very common for programmers to write something, come back a week later, and not have any memory of what they’ve wrote.
05:02 So now you have some idea of why you need classes. They’re a convenient structure to store and operate on data. It makes it easier to reference stuff in code, and it makes it easier to read the code and understand what it’s doing.
05:16 It’s also a convenient structure to encapsulate code, as you’ll come to see later in the course. It gives you somewhere to place all this code that is related to this type of data. In a nutshell, you need classes to make your life as a developer easier. Now, I’ll say this one last time: you can overdo classes.
Become a Member to join the conversation.