Why is OOP still so widely spread?
Oh boy. Here we go, here I am commenting on a subject on which _everybody_ has an opinion. A lot of people say that &quot;OOP&quot; is just a tool. Or that it is overused. Or that it isn&#x27;t used properly. I will say that even though I sometimes use objects I am not a fan of almost every &quot;object-oriented&quot; codebase that I&#x27;ve had to work with. The issue is that OOP is presented as an abstraction that, if you understood it, would provide for good readable code. The way I judge good principle is whether when, inevitably, junior to mid-level programmers start working on the codebase will they be able to preserve some amount of sounds structure. I would say that OOP offers very little here, as I&#x27;ve seen even senior developers write what to them was &quot;good OOP code&quot; but in practice nobody could work with.<p>Here are some things that OOP does not give you:<p>* Is your state normalized, i.e. saved only in one place? You do not know.<p>* Is your execution predictable? Parent to child? And how do you do it when it&#x27;s reversed.<p>* What should be public and modifiable and what should be intermediate state internal to the object. And should intermediate computed values be stored next to values determining the true state of your application? (even though there are &quot;public&quot; and &quot;private&quot; modifiers it is quiet difficult to determine what is truly public and cannot be modified through some method. This makes it more difficult to understand what the state of your application truly is and what are intermediate convenience values).<p>* Are there limitations on passing state around? Or can large chunks of state be passed to multiple actors in the form of classes who change it concurrently or might store it for later use?<p>* How much state should be in a single class? In other words: how many classes can a class encapsulate and how complex could those classes be?<p>* Should something that has no state be an object or just a function (in some languages you are not given the option at true, but can&#x27;t say that it&#x27;s awesome).<p>* Should you be allowed to use event buses and how to prevent spaghettification? (cause events really are akin modern goto statements).<p>* etc.<p>Basically the ability to compose the state, type and methods is so wildly open-ended that almost always what I see is just an ever expanding convoluted bloat of state spread ts chaotically that it become extremely difficult to see what the state of your application is at any moment. Everybody has their own idea of what an object _is_, what should be an object and how they should come together. And that is for every developer on the team. So you can follow the absolute best of OOP practices and still end up with a mess.<p>Let&#x27;s look at a counter-example. It is not a system that claims to be universally usable but that is the point. I&#x27;ll use the, quiet banal, example of React. Yes, it is a framework, and in a way it does have objects, but really it borrows heavily from functional programming in the sense that it says that state is king and the single most important thing in your program. That you _must_ have strong constraints as to how your state is created, organized and modified. (In contrast to a class, which is state that you can pass around as much as you want and the whole codebase could modify it at will).<p>* The application is always a predictable tree of dependencies.<p>* State is either stored separately altogether (using a state-store such as redux, mobx, etc.) or bound to a particular element in the tree hierarchy.<p>* State only flows down the tree, ideally immutably.<p>* A component (unlike a class) cannot modify state of anything outside of itself.<p>* Only &quot;has-a&quot; relationships are allowed - no polymorphism.<p>* Only event can be passed up.<p>* State is essentially a collection of easily inspectable primitives. Meaning that even though a component can _contain_ another component a class is not a valid member of component state.<p>While a system like this could be built using OOP primitives (classes). Nothing about OOP suggests this system. Yes, you can build it, but can build something absolutely unrelated. And, really, it enforces things that are the antithesis of many things that &quot;proper OOP&quot; espouses.<p>Perhaps the main thing of any system is to create a predictable and well-structured relationship between elements of state. I think that wonderful examples of the basis of such a system are Go and Rust. To be honest, I barely know the languages, but from what I see is that they make _type_ the most central element of the language. If you have your types well-defined and be separate from how the types are used you are already suggesting many things that could prevent state-sprawl. A struct defines the basic primitive around which your program is based.<p>* Struct has no private intermediately computed values. If you do not want to recompute something - memoize, do not bloat the state with random data that is inseparable from you base state.<p>* Structs provide a very predictable &quot;has-a&quot; top-down dependency between state<p>* No inheritance, so composition is enforced.<p>* Keeping types and functions that operate on those types makes it very explicit what a function does - is it a method or is it a function?