Get Rid of OOP For Good (Part 1)

Joseph Razon
Just Eat Takeaway-tech
5 min readJun 14, 2020
Get Rid of Object-Oriented Programming For Good

In this article, we will discuss one of the main principles of OOP (Object Oriented Programming), versus one of the main principles of POP (Protocol-Oriented Programming), and why you should consider adopting POP over OOP.

If you are familiar with programming, you are probably familiar also with the OOP concept, and if not, here is a small brief to catch up.

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data (known as properties) and procedures (known as functions or methods).

OOP is a way of writing computer programs that are using the idea of objects to represent data (variables) and functions (also called methods), instead of just a list of commands. The way objects are separated depends on one subject or another, thus the functions and data it contains must be related to the same subject or flow.

Enough theory. Ready to begin? Let’s break it down and see how it works.

Think of a Volkswagen Golf car, how would you describe it?

The term can be divided into 3 three parts: Car, Volkswagen, and Golf.

The following diagram will simplify things:

As you can see, Golf is a kind of Volkswagen, which is a kind of a car, which is a kind of a vehicle. Each one is of the objects related to the object it’s made of.

You can think of it as a derivation, or how we call it in programming languages — inheritance.

In simpler terms, it means that car contains all vehicle’s properties and functionality, Volkswagen contains all car’s and vehicle’s properties and functionality, and Golf contains all Volkswagen’s, car’s and vehicle’s properties and functionality.

Let’s complicate it a bit. What will happen if we have more than one car manufacturer?

Looks simple right? Now instead of another car manufacturer, we will add another vehicle type. How would it look like?

The diagram can keep dividing until it gets to the pyramid’s top. And as you can see all these diagrams represent the same idea — an object that is a part of a group, which is also a part of a group, etc.

Here is a code example of OOP implementation:

As shown, the Vehicle class contains two methods: ignite() and accelerate(), and the Car class inherits the Vehicle class and contains only one method — startDriving().

Although the car class does not declare the ignite() nor the accelerate() method, according to its inheritance to the Vehicle class — it can use its “father” methods.

Now that we know OOP, we can move forward to POP. But before that, we will discuss the basics of it: the protocol itself. What is a protocol?

A protocol is also called an interface, and the most simple way to describe it is: A protocol is a set of rules to follow, known as abstraction as well.

So what does it mean in programming?

Given the previous example, let’s say we have a car. What is the main purpose of a car? To drive. In that case, the car is driveable, therefore our protocol will be named driveable, and will contain at least one primary function called drive. The protocol can contain as many variables and functions as we need when the only rule is — it has to be related to the main subject of the protocol.

An object can conform to more than one protocol. The reason for that is to give the ability to a certain object to do as many actions as needed.

Let’s take our car as an example:

How does that come to action in real-life coding?

Assuming we have popups in our app and we know that all of our popups need to have a close button, we will create a dedicated protocol and call it Closeable. The purpose of this protocol will be to grant a closing action(functionality) to every object who conforms to it. To be more precise, at this point the protocol enforces this ability, which means that every popup that will conform to it will have to implement the close action we previously declared.

** Keep in mind that protocols can only declare variables and functions but not implement them. **

Here is a code example of POP implementation:

Conclusions:

In OOP we concentrate on creating objects, our program will be based on objects and what needs to be inherited. All of our planning will be done according to that. When we create an object, we need to consider what will be the parent / child objects we will inherit from / to.

POP is a bit different. The main consideration is the sets of rules and using them to enrich or limit the object’s functionality. In POP we also create objects but the objects conform to one (or more) protocol instead of one to another.

It is essential to remember that the main difference between those two approaches is that when using OOP we are limited to inherit only from one object, while in POP we can conform to as many protocols as we need. Therefore, in my opinion, POP is much more preferable.

--

--