The Most Important Concept of Object-Oriented Programming
As it grows, any codebase becomes harder to maintain. One of the ways to solve this problem is to use Object-Oriented Programming. There’s quite a lot of sources of information on how to write clean and nicely structured object-oriented code, and they all put stress on different concepts, sometimes even inventing their own.
I’d like to share my view on the matter. The most important concept of Object-Oriented Programming for me is not inheritance, encapsulation, or polymorphism, but the notion of keeping the data and the related code close. The point of this is being able to keep your program structured and traceable. All other properties of OOP are related to this concept.
Encapsulation makes sure the code of one entity doesn’t access the internals of another entity directly but via an exposed interface. All implementation details of an object, including the data fields, are hidden. This allows the code to be compartmentalized and have a traceable behavior.
You may have stumbled upon some critique of the Inheritance concept lately: many issues with the structuring of complicated code are easier solved by composition than by inheritance. This idea has its merit. However, inheritance is not only about inheriting one class from another, but also about implementing and extending interfaces, which is again related to encapsulation and compartmentalizing the code, allowing to structure it better.
Polymorphism goes hand-in-hand with inheritance. It’s not only about treating the same object as an instance of different interfaces or superclasses. It’s generally about having different levels of access to the object, according to the problem at hand. Drawing on the “apple extends fruit” analogy, if the code is only concerned with the features of the fruit, it doesn’t need to know about the fact that this particular fruit happens to be an apple. This again allows us to have structured and easily traceable behavior.
However, the idea of “keeping data and code close” shouldn’t be driven to extremes, as it often happens. This doesn’t mean that the code and the data should necessarily reside in the same class.
Let me clarify my point with a simple example.
Suppose we have a User entity that only contains fields and no behavior. We only use this entity for database mapping. Then there’s a UserService class that contains only behavior and is related to user management. This may seem like a violation of the object-oriented idea of keeping the code close to the data.
Some say that having such an “anemic” data class and a separate behavior class is not actual OOP, and we should move the methods of UserService inside the User class.
But I argue that there’s nothing wrong with structuring your code that way. Having a clear affinity of the data and the code used to process it, even if only on the naming level, is already enough to make sure your code is nicely structured.
You know where to look for the user-related behavior. You know where to put your next method or function that handles the User-related behavior.
The fact that the behavior and the data are located in different classes doesn’t necessarily make your code less object-oriented.
Even more so, when your codebase grows, you can split the behavior in different services, e.g. UserOnboardingService and UserManagementService. But if you’d adhere to the letter of OOP, instead of the spirit, you’d have put all functionality in the User class, which would make it less readable and maintainable as it grows.
The OOP approach was initially intended to structure large and complex programs hierarchically, and we have to bear this in mind whenever we write code. Don’t use any concept just because it’s “by the book”, use it only if it helps you to structure the code better.