Why use Object-Oriented Programming?

Advantages of using OOP.

Silas Author Image

Silas Tay

20th July 2021

Instagram Github Linkedin
Email
Connecting to Google Sheets with JavaScript

Introduction

In a previous article, I talked about how to use Object-Oriented Programming (OOP) in Python. However, OOP is a framework that is used throughout many different languages, not only Python! For programmers new to OOP, it may seem like a huge hassle to create Classes and instantiating Objects.

When I first learned OOP, I too felt that it was unnecessary. The advantages of OOP may not be very clear to someone that’s newer, but after using it for some time, I can give you my assurance that learning to use OOP effectively will save you a lot of time in the future.

In today’s article, I will outline some of the common advantages of using OOP and why most programmers around the globe are in love with using Classes.

Content Page

  1. Breaking down the Problem
  2. Faster Bug-fixing
  3. Cleaner Code
  4. Reusability

Many programmers have hailed OOP for its ability to simplify large programming problems into many smaller steps. This is largely due to Encapsulation in OOP. Encapsulation is grouping blocks of code together for different purposes. In OOP, Encapsulation is demonstrated by defining different Classes for different functions.

How does Encapsulation help us break down our Problems? It helps us to separate our problems into smaller, more digestible pieces! By defining separate Classes for different purposes, we are able to piece together all our different Classes to solve bigger issues!

Breaking down problems

OOP helps make things simpler by breaking problems down.

Imagine we are building a Lego Spaceship! It’s pretty daunting to look at all our pieces and think of building an entire Spaceship one brick at a time. It will be much easier if we break down this project into smaller pieces, such as building the cockpit first, then the engines, then the thrusters etc. By splitting up the project into smaller pieces, we can focus on refining and perfecting each piece before finally bringing them together and forming our entire project!

Space Shuttle

Imagine we are building a Lego Spaceship!

This is exactly like our OOP! By splitting up a problem into smaller pieces, we can work on refining each Class before finally bringing them together to form a complete script!

Personally, I recalled using OOP a lot during my internship to build large scripts for data collection. The data collection process requires multiple steps, and building a separate Class for each step helped me to slowly refine my script. For example, I built one Class to scrape data from websites, one Class to clean and process the data and one Class to send the data to the database. Working within this framework helped me to break down the entire project into simple steps!

Another advantage of using OOP is that it reduces the time needed for bug fixing. This is again due to Encapsulation in OOP. Using OOP, we will have different chunks of code for different purposes clearly separated between Classes. This means that if we ever meet a bug in our script, we know exactly where to look!

No more scrolling through a 1000-line Python script to look for bugs, simply inspect where the error was thrown and look at the specific Class responsible for that function! This is also why it is a good practice in OOP to make sure all your Classes have very distinct, sole purposes. Having each Class with one lone function, it is easy to sieve out which Class is responsible for the error thrown.

Stressful Bug-fixing

Without OOP, Bug-fixing can be a very stressful affair.

Personally, OOP really helped cut down my bug fixing time. Before using OOP, it was a very tedious task to read through all my lines of code (especially if it was not well commented) to find exactly where the bug resided. It was frustrating to feel completely lost, not knowing which parts of my code was the issue.

After implementing OOP, it was a much simpler task! Everything was distinct and each step of my script was done by a different Class. This meant I knew exactly which Class to fix once an error was thrown!

Separate jars

With OOP, our code will be separated very neatly!

In fact, using OOP often also helps programmers to cut down on the number of bugs they produce! By encapsulating our code based on distinct purposes, it allows us to focus on one function at a time. Spending our time on one function at a time, it allows us to be more present-minded and refine each Class before moving on to the next.

Clarity

Encapsulation helps us to focus on pieces of code, one at a time.


Going back to our Lego Spaceship analogy, by splitting up the project into smaller tasks, it allows us to focus on building each piece of the spaceship one step at a time instead of trying to tackle the entire spaceship as a whole. This will definitely help us be more efficient and make less mistakes as we build. The same concept applies to our code!

Using OOP also helps us write cleaner code. As we bring all our different Classes together into one script, it produces a much cleaner script with clearly defined classes that are responsible for different tasks! It cuts down the number of lines in your main script drastically, while making everything very readable and elegant.

It’s important to make sure that your Classes are named appropriately to make readers understand your code better. It is also very helpful if you have thoroughly left comments throughout your code to let readers understand not only what task that Class is responsible for, but also what each method of that Class does!

Imagine the difference between a script that has been split into separate Classes and one that has everything in one single script. The latter will have extremely messy and unreadable code, making it extremely difficult to decipher for people reading the code for the first time or even the author himself/herself! Making sure you have split your tasks into Classes makes everything easier to read, which is something every programmer should strive for.

In bigger software companies, it’s highly likely that you will need to work with team members on projects. Making your code legible and easy to understand is extremely important and prevents any unambiguity between you and any team members. It’s also easier for you to ask for help, since your peers will be more willing to read through cleaner code than struggle to understand your code at all!

Teamwork

Using OOP often leads to better teamwork between programmers.

Personally, I thought this was an especially satisfying benefit of using OOP. Reflecting upon my old code without using OOP, it was often very messy and almost impossible to read. I remember reading through my code and finding lots of lines that were either unnecessary or I had no idea what their purpose was. After implementing OOP, it was much easier to read through my code.

In addition, it was much easier for me to reach out for help as well. Not only was my code much more legible to others, I felt less bad reaching out because I knew that my code was clearer and should not be that much of a hassle for someone else to read through.

Lastly, OOP is probably most regarded for its reusability. Many programmers around the world implement OOP because it has helped them cut down on the time they spend rewriting code for similar purposes. The reusability of OOP is largely due to two concepts - Inheritance and Polymorphism.

Firstly, Inheritance is the act of “inheriting” (or copying) the attributes and methods of another Class. What this means in terms of programming is that I can reuse code from one Class in another Class. The purpose of this becomes clear when we are creating Classes that stem from the same type.

For example, imagine that we have a Vehicle Class. This Vehicle Class will have all the attributes and methods that a Vehicle should have. Now if I want to create several new Classes such as Car and Aeroplane that stem from the Vehicle Class, I can simply inherit the Vehicle Class in both those Classes! This means that the Car and Aeroplane Class will have all the attributes and Methods that the Vehicle Class has! I can then define unique attributes and methods within the Car or Aeroplane Class that defines their unique purpose (e.g. Aeroplane may have a “fly” method, Car may have a “drive” method).

Inheritance flow

Inheritance allows us to branch from another class!

Inheritance allows us to reuse code that we have already written to define new Classes that have a different purpose, but still stems from an existing class!


Next, Polymorphism is the act of redefining a method from a Class. Basically, we use Polymorphism to define different versions of the same method, which will be triggered based on the given parameters. It allows us to reuse code that has already been written but with differing outputs.

There are two types of Polymorphism - Static/Compiling and Dynamic/Run-time.

Static Polymorphism involves the act of Overriding Class methods. This is done by inheriting from another Class and completely redefining one of it’s methods. By doing this, we do not need to rewrite every other attribute and method of the parent Class just because we need to change one method!

Dynamic Polymorphism involves the act of Overloading Class methods. This is done by defining a new method with the same name but with a different number of parameters. As the method is called, the implementation of the method will trigger based on the number of parameters given. Similarly, doing this helps us reuse most of the parent Class’ code.

Polymorphism

Polymorphism allows us to create different iterations of the same Method!


Inheritance and Polymorphism are concepts that are pretty conceptually challenging, especially for beginner programmers, so don’t fret if you are confused! I will be writing a separate article on just these two topics soon enough, so if you are someone that is struggling to understand the beauty of using Inheritance and Polymorphism in OOP, do stay tuned!

Conclusion

Implementing OOP in my code has been a huge step in my programming journey, and I highly encourage all of you to get familiar and comfortable with using OOP in your code as well! Looking back at my code before using OOP, I shudder at the thought of bug fixing and explaining the code to others. I can easily say that OOP is simply a better way of programming in the long run.

However, I sincerely believe there are situations where using OOP may be unnecessary. I believe the true beauty of OOP lies in the encapsulation of code, and if your project is very one-dimensional and linear, it may be more of an inconvenience to separate code into Classes.

I hope today’s article helped you to appreciate why OOP is highly regarded by software developers around the world. If you enjoyed the article or learned something, please subscribe to our email newsletter! I am planning on writing many more articles on OOP, including articles talking about good practices to implement when using OOP and also focusing on specific confusing concepts in OOP (including polymorphism and inheritance), so stay tuned! Stay cool, cucumbers!









STAY IN TOUCH