Flipped Coding

About Classes…

learn more about About Classes…
It’s time to break down one of the most used, yet vague concepts in all of object-oriented programming. Classes. There isn’t a web project alive that doesn’t use classes somewhere (yes, web projects are living). Understanding this highly used concept will make your code make more sense to you and it might give you a new appreciation for how artistic code can be.

Quick background on OOP

To start, we need to know a bit about object-oriented programming or OOP. In a nutshell, OOP uses objects to design and build applications. You probably already know what objects are, but here’s a refresher if you don’t remember: 1. Objects basically act like super variables that can hold multiple parameters and methods. So you can have numbers, functions, arrays, and other stuff inside the object.

There’s at least one object-oriented language you have to deal with in web development and that’s JavaScript. Most likely your back-end will also be an object-oriented language so there’s no escape.

What a class is

Now let’s talk about how classes fit into all of this. A class is just a template for a custom object. It handles all of the properties and methods for the custom object. So in a class, you can limit certain properties to a range of values, you can set default values, and you can define the methods that each new instance of the object will use.

Some beginners think that a class and an object are the same. The difference between them is really conceptual. Your classes are the templates for your object. You can make any number of objects based on your class and they could all implement the methods in the class differently. A class is like a custom object that you make.

How it works

Each class you create should only be responsible for one thing. For example, if you have a class called “Team” the parameters and methods in the class should be focused on “Team” things. You could have name and score properties, but you probably shouldn’t include details about cars here.

When you make a new class it should have a name that reflects what it’s going to be used for. When you see a class named “Team” it probably means that you’ll be doing something with teams. If you see a class named “Food”, it probably means you’re doing something with food.

Classes are also used because they provide a level of abstraction. You don’t necessarily have to know the code behind a method to use it and you might not want other people see that code. In a way, classes are like a subtle security feature. Nobody ever has to see the code for the class to create new objects with it.

Why we use them

Because we have to. 😂 I’m only sort of kidding too. If you use OOP and you know you’ll need to create multiple instances of a custom object then you should use classes. Can you imagine writing the same properties, the same methods, and handling all the other stuff on every object you needed? Development would slow down drastically.

There would be way more issues to debug because the code wouldn’t stay consist between different developers at different times. Anytime you needed to add a new property or method to some objects, you would have to go through all of your code and find all of those objects that need updates. Classes save us from more headaches than we would ever want to deal with.

When you use classes, adding new features isn’t a problem because all of the objects based on those classes automatically get any new properties or methods. It also helps with testing if you need to use some default data in certain places. They really help keep your code organized and keep the meaning of it clear.

See, none of this web development stuff is really that bad. It’s just hidden behind so. Much. Jargon. Hopefully this helps you understand classes better because they really are that simple. OOP is what we use for web development so it doesn’t hurt to have a better understanding of its components! 😁

Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding