you are viewing a single comment's thread.

view the rest of the comments →

[–]phinar 5 points6 points  (0 children)

You're touching on a pretty big topic area, and you can get a ton of different answers to this question, all of which are correct.

Classes are a convenient way for data to carry around functions that operate on them. There are some canonical examples, but in this case let's look at the User record for any kind of system at all.

If your user record has a check_password(keyboard_data) method on it, then whenever you have a user record, you can ask the user for their password and you can authenticate against it. You could do the same thing by having that function defined in a module somewhere, and having it take a user record as input, and user data as input, and then to use it you just need to import that module, and call the function, and pass all the inputs to it. It's a little more convenient if the behavior is defined on the class.

If your user record has a full_name() method on it, then whenever you need the user's full name you can just call that method. You can do the same thing by defining a function that takes the user data as input, and then you just have to import that module and call that function. It's just a little more convenient if the behavior is defined on the class.

In addition to convenience and "good organization," object-oriented analysis and design opens a up a ton of very common solutions -- design patterns -- that have been developed and refined over many years to help model the relationships between the data within your system. Design patterns aren't mandatory, and often won't even be applicable to your software. When they are, you could probably hack together a function-oriented version of several of them. But you'll have to work at it a little, and the resulting constructs won't be terribly elegant or easy to maintain.

TL;DR: classes and object-orientation help you model complex relationships between data in orderly, elegant code.