Code

The Future of Object-Oriented Programming: A Look Ahead

The Future of Object-Oriented Programming: A Look Ahead / Skillbox Media

Python Basics: A Free Course for All Skill Levels Four Impressive Projects for Your Portfolio Direct Interaction with a Teacher: A Unique Learning Opportunity

Learn More

Rhea Mutafis

About the author

An entrepreneur with a PhD in philosophy from the Sorbonne and an MBA from the CDI. He has written for publications such as TheNextWeb, HP Enterprise, and Built In.

If so many people hate OOP, why aren't they willing to give it up? Photo: Vale Zmeykov / Unsplash

At the beginning of the programming era, in the 1960s, computing machines had limited capabilities. This necessitated the efficient distribution of their resources between tasks and data.

The difficulty lay in the fact that at that time it was impossible to efficiently process large volumes of information without overloading the computer to its maximum capabilities. If it became necessary to solve many different problems, each of them could only operate with a limited amount of data, otherwise the calculations would drag on indefinitely.

In the mid-sixties, Alan Kay put forward the concept of independent minicomputers that would be able to exchange not just data, but messages. This approach would significantly improve the efficiency of computing resource allocation.

from the translator

Alan Kay, with a background in mathematics and molecular biology, drew a parallel between computers and living cells. His concept was that independently running programs (like cells) could exchange messages with each other. At the same time, the internal state of the programs would remain closed to the outside world, implementing the principle of encapsulation.

Kay shares his memories as follows: "I thought of objects as something like biological cells or individual computers connected in a network that could communicate only through messages."

Although the idea itself was quite innovative, the introduction of object-oriented programming did not happen immediately, and it was not until 1981 that it began to gain popularity. Since then, both novice and experienced programmers have been increasingly mastering OOP. As a result, today the market is overflowing with specialists in the field of object-oriented programming.

Object-oriented programming has been a leading paradigm in software development for over forty years. However, in recent years, it has come under increasing criticism. Have new technologies simply surpassed this paradigm?

The Rationale for Integrating Data and Methods: Is It Worth It?

The essence of object-oriented programming is a fairly simple principle: you divide your program into separate, self-contained components. This means that you group all the data and the functions needed to work with that data in one place.

Note that this refers exclusively to the concept of encapsulation, where data and functions are hidden within an object and inaccessible from the outside, thereby providing protection. Interaction with an object's contents is possible only through special messages known as accessor methods, including getters and setters.

Modern object-oriented programming also has other key concepts that were not part of the original design. These include inheritance and polymorphism.

Inheritance is the ability for programmers to create subclasses that inherit all the characteristics of their parent class. This idea appeared in 1976, ten years after the inception of object-oriented programming.

A decade later, the concept of polymorphism entered object-oriented programming. In a general sense, this means that a method or object can serve as a basis for creating other methods and objects. Polymorphism is a more flexible concept than inheritance, as it allows a new entity to not only inherit all the characteristics of the original method or object, but also modify them to suit its needs.

Polymorphism is characterized by the fact that, despite the existence of a dependency between two entities in the source code, the called entity acts as a kind of template for the actual objects used in practice. This significantly simplifies the task of developers, as they do not need to worry about interdependencies during program execution.

In general, the concepts of inheritance and polymorphism are found not only in the context of object-oriented programming. However, it is the encapsulation of data and associated methods that distinguishes OOP from other approaches. In an era when computing power was significantly limited compared to today's, this idea was truly revolutionary.

OOP is definitely not useless. It has made coding much easier. Photo: Windows / Unsplash

Suppose you decide to reuse a class that you previously used in another project. What are the consequences if the base class from which it inherits is changed?

Your code may be at risk even if the changes affect only a small part of it. For example, yesterday all your derived classes worked flawlessly, but today they start to crash. This was caused by a minor fix in the base class, which, nevertheless, had a devastating impact on the entire project. This situation is called the brittle or "unprotected" base class problem.

Choosing to reuse a class may seem like a quick and efficient solution, but it can come at a significant cost later. That is, the more you use inheritance, the more difficult it becomes to maintain your code.

Inheritance can be described as a delicate and seamless process by which the characteristics of one class are passed on to another. But what if you need to combine the qualities of two different classes?

Unfortunately, this cannot be done simply and gracefully.

Consider, for example, the Copier class, which represents a copying device. This device is capable of scanning documents and then reproducing them on a new blank sheet of paper.

The question of which subclass a Copier should belong to—scanner or printer—is important for understanding its functionality. A copier, as a device designed to copy documents, can have characteristics of both a scanner and a printer. However, given its primary purpose, it is more logical to classify it as a subclass of printer, since a copying device typically includes printing functions, which is a key aspect of its operation. On the other hand, the presence of a scanner in the copier function is also important, since without the ability to scan originals, the device cannot perform its primary functions. Ultimately, the decision about which subclass to assign Copier to depends on which aspects of its operation are considered primary—printing or scanning.

There is no single correct solution. This situation is known as the diamond problem. Although the code remains intact, it often causes frustration among programmers.

The previous question asked about the ancestor of the Copier class. I misled you with the answer: there is an elegant solution to this situation. Let's make Copier the parent class, and Scanner and Printer its child classes, which will inherit only some of its properties. That's all!

This is a really interesting question. However, imagine that we have a Copier device that prints only in black and white, and a Printer that is capable of producing color prints. In this case, don't you think Printer is a more universal term compared to Copier? Another question arises: what if Printer requires a Wi-Fi connection, while Copier can work without one?

As the number of properties in a class increases, it becomes more difficult to define a correct hierarchy. In this case, we are talking about the properties of classes like Copier and Printer, which contain both common and specific elements. Trying to organize these classes into a linear inheritance hierarchy, especially within a large and complex project, can lead to various difficulties. The problem is that it is not possible to clearly select a parent class among Copier, Printer, and Scanner.

Don't mix hierarchies to avoid confusion in the clutter of classes. Photo: Emma Dau / Unsplash

When you're faced with hierarchy issues, you have the option of considering an alternative: let's focus on object-oriented programming without hierarchical structures. We could simply use collections of properties, inheriting from them and making modifications or additions as needed. While this approach may seem a bit confusing, it's still quite feasible, right?

Unfortunately, it's not. This introduces another problem. The main idea of ​​encapsulation is to manage data more efficiently while protecting it from each other. Without a clear hierarchy, this is impossible to achieve.

Suppose object A needs to interact with object B. It doesn't matter what the exact relationship is, as long as A is not a direct descendant of B.

Object A contains a reference to object B, allowing them to interact with each other. However, if A has data that is also accessed by B's children, this creates a situation where the data can be modified from multiple sources. As a result, the information in object B loses its protection, and the encapsulation principle is violated.

Despite the fact that a significant number of developers working with an object-oriented approach create their products using such an architecture, this can no longer be called true object-oriented programming. Rather, it becomes just a mess.

The Risks of a Single Paradigm

All of the mentioned difficulties associated with object-oriented programming have one thing in common: the use of inheritance occurs even in cases where it is not the optimal solution. It's worth noting that these difficulties are not unique to the OO approach, as it did not initially imply inheritance. Much more significant here is the fear of deviating from the established paradigm and blind adherence to it. However, excessive adherence to a single paradigm is not limited to object-oriented programming. In the context of functional programming, for example, significant difficulties arise when working with data input and display. For such problems, either object-oriented or procedural methods are more appropriate. However, some programmers prefer to implement input and output as pure functions, resulting in their code becoming excessively cumbersome and difficult to understand. At the same time, alternative approaches could solve these problems with just a few clear lines of code. Within different paradigms, just as in religious teachings, balance is important. Certainly, figures such as Jesus, Muhammad, and Buddha brought outstanding ideas to the world. However, strictly adhering to their instructions down to the smallest detail can have a significant negative impact on both your personal life and that of others. It's important to find a balance.

Certainly, functional programming is becoming increasingly popular, while object-oriented programming has been losing ground in recent years and has come under considerable criticism.

Undoubtedly, mastering new paradigms is important, but their application should be based on genuine need. After all, even if object-oriented programming is perceived as a universal tool that developers use for all tasks, is that a reason to abandon it? It might be wiser to supplement your toolbox with a screwdriver, knife, and scissors, allowing you to choose the most appropriate tool for a particular problem.