I’m sorry if I was a bit misleading but this article isn’t about the type of class that lives in a school. Instead today I’m talking about the kind of class that creates ruby objects and how during my first project for my coding bootcamp I learned just why they’re so important and how I could properly use them to make my programs more dynamic, more powerful, and easier to write!
So what exactly is a class? In simple terms a class is a framework to create objects in ruby that all have similar functionality or attributes; but more than that it can be a tool for us to correlate or find data, store information, or just organize our code.
My project was called fridge cleaner, and its basic functionality is to ask the user for some food/ingredients they have that they want to cook and then return to them recipes that use all or most of those ingredients. Because of the API I used this was actually a very simple project but when I first created the program I didn’t utilize my classes to their highest potential and because of that I made it a bit harder on myself than it should have been.
The main thing I want to focus on is the way that classes correlate data and how you can use that to your advantage to write less code in the end. Because of my recipe class which stores all of the recipes that were retrieved from the API I was able to find a recipe to preform actions on it (such as display it to the user) in many different ways (such as name or id). After I had found it I could save it to a class variable in my cli class as the “current” recipe that my user is interacting with so that I can easily retrieve it and display all the information they would want to know about the recipe.
Another thing that is helpful about the way that classes store data is that through attributes or class variables class instances can know things about themselves that will be helpful to you the programmer, such as wether or not their full list of ingredients and instructions have already been retrieved from the api so that you wont need to make another call to retrieve that information again, or if they were part of the most recent group of recipes that were shown to your user so you can show it to them again if they wish. Obviously those two examples are specific to my program but the ideas relate to objects in general as well.
So we’ve talked about how it’s incredibly useful how classes store information and correlates that information so it’s easier to find, work with, and display; but what else can classes do? I could type 100 pages going over every benefit using classes has over not using them but to be short lets just look at a few more on just the surface level:
Classes keep your code clean and organized. If you had a school made of classes you know all your students will be instances of the Student class and all your teachers will be instances of the Teacher class.
Classes can relate to each other! Not only is the correlation of data inside of classes helpful but the correlation of data between classes is helpful! Imagine the school example again. Timmy is a student, and not only does he know things about himself: like his grades, his year, his classes etc. He. also knows who his teacher is and since his teacher is also a class all of that information is connected and available for you to access!
Classes can save you a lot of writing. Since every instance of a class shares methods and variables assigned as instance variables/methods that means you only have to write those methods once and don’t have to pass many, or any arguments to them because all of the information needed is already stored in the instance. On top of that classes can inherit other classes. let’s say for example the seniors in our high school are allowed to listen to music during class.. but only the seniors. Instead of re-writing all of the students code and adding #play_music to the seniors we can just inherit the student class and only need to write that #play_music method.
We’ve only scratched the surface of classes but hopefully its enough to realize just how powerful classes and object-oriented programming are to becoming truly efficient programmers.