Pretty much, the standard anti-OOP fare. Most of the article is extremely vague, and doesn't make many concrete points. Any article these days that doesn't actually start by defining OOP carefully, and discussing what exactly is being criticized, is basically a waste of time. Things that people have had in mind under the general OOP moniker:
Any usage of inheritance
Deep inheritance
Implementation inheritance
Encapsulation of data
Objects holding references to each other
Another tip: if you are reading an article about C++ and OOP, just hit Ctrl-f and search for "invariant". If you can't find it, there's a good chance you're safe to move on.
Some particular parts I found objectionable:
Even though C++ is described as a multi-paradigm programming language, the truth is that most programmers will use C++ exclusively as an Object Oriented language
Citation needed? Especially if you predominantly associate OOP with heavy usage of inheritance. I would say that over-use of inheritance is down significantly. Every C++ developer I know, knows about and uses lambdas and std::function when appropriate. Everyone writes free functions regularly. Etc.
I prefer simple solutions, that add the minimum amount of nodes and edges to the dependency graph of the code.
Don't we all? But this article doesn't show what exact aspect of OOP is over-complicating (or even what it is).
There’s also the fact that OOP features are inherently poor performance-wise. I implemented a simple OOP hierarchy with an interface and two derived classes that override a single pure virtual function call in Compiler Explorer. This example code either prints “Hello, World!” or not, depending on the number of arguments passed to the program.
This comparison is so bad and unfair it borders on parody. OOP provides totally open ended run-time polymorphism; that is you can add new behavior to existing code without changing the original code, without even re-compiling it. Because it's so open ended, it tends to be a tough barrier for compiler optimization. The same way that totally non-OOP techniques like function pointers and std::function tend to be difficult barriers for a compiler as well. If you know based on the domain that only 2 behaviors are possible, or only N data types are possible, then indeed a bool or a variant is probably a better choice than using inheritance.
Over the years, though, I’ve seen three main arguments that prevent people from giving the other side a chance:
“Good OOP wouldn’t do this.”, “This is badly designed OOP.”, “This code doesn’t follow OOP principles.” and similar variants. I’ve heard this one when showing examples of OOP gone bad (I’ve already talked about why OOP code invariably goes wrong). This is a clear case of the No true Scotsman fallacy.
No, it's not. Articles like this one however *are* a clear example of the Strawman fallacy. You don't get to tell people that are advocating using OOP, or using OOP as a tool, what their view is. This article doesn't have any code at all, but most such articles typically start with a horrible example of a class with getters and setters for every single data member, no discussion of invariants, virtual used strangely, implementation and interface inheritance mixed together, etc, and then rip it apart. I would definitely agree, most OO code written in this vein of articles, is bad code! But it doesn't show anything. As for OOP "invariably going wrong": all code goes wrong. If the requirements change more than refactoring efforts are put back into it, over time the mismatch will continue to increase. Really the question is how to write code to minimize the amount of effort and mismatch over time; this trait could be called robustness. I've yet to see even a half-decent case made that OOP techniques in C++ have no place in robust code.
What you interpret as a strawman is usually simply a bad experience related to real code bases a few of us are unfortunate to work with.
Yes given competent programmers a "sea of objects" should not happen, nor should the GoF book be considered what defines "architecture". Yet it happens regularly, and I, and others, have been witnessing it, and are affected by that kind of insanity and incompetence on a daily basis. It puts you into a particular state of mind where you wished OOP were taught more appropriately, and certainly not as a major structuring element, because yes it often breaks down when interpreted like this and when the other tools are forgotten.
As for "all code goes wrong"; I strongly disagree. Linux for example, has not gone wrong. OTOH I can't think of a single example of a software developed with an object-first and everything-is-object mindset that has not gone wrong. Usually what really matters is an afterthought in such contexts, and not even the "patterns" employed make sense for long.
"Every C++ developer I know, knows about and uses lambdas and std::function when appropriate."
That I don't understand. Classes are lambdas on steroids. Lambdas preserve data that can be accessed later by calling the lambda. Classes preserve data that can be accessed and changed later by calling one or more methods. Classes are a superset of lambdas.
The problem I find with C++ is that it still has pointers. It's managing pointers that can cause real confusion.
Functors/function object classes are often the optimal solution, but lambdas are terser and easier to read when you just want to write a function/closure inline.
Raw pointers or all pointers? unique_ptr is perfectly fine and necessary. shared_ptr is useful, but should be used sparingly. Raw pointers are necessary for interop, and are generally ok if they're non-owning and backed by an owning unique_ptr.
31
u/quicknir Jan 15 '19
Pretty much, the standard anti-OOP fare. Most of the article is extremely vague, and doesn't make many concrete points. Any article these days that doesn't actually start by defining OOP carefully, and discussing what exactly is being criticized, is basically a waste of time. Things that people have had in mind under the general OOP moniker:
Any usage of inheritance
Deep inheritance
Implementation inheritance
Encapsulation of data
Objects holding references to each other
Another tip: if you are reading an article about C++ and OOP, just hit Ctrl-f and search for "invariant". If you can't find it, there's a good chance you're safe to move on.
Some particular parts I found objectionable:
Citation needed? Especially if you predominantly associate OOP with heavy usage of inheritance. I would say that over-use of inheritance is down significantly. Every C++ developer I know, knows about and uses lambdas and std::function when appropriate. Everyone writes free functions regularly. Etc.
Don't we all? But this article doesn't show what exact aspect of OOP is over-complicating (or even what it is).
This comparison is so bad and unfair it borders on parody. OOP provides totally open ended run-time polymorphism; that is you can add new behavior to existing code without changing the original code, without even re-compiling it. Because it's so open ended, it tends to be a tough barrier for compiler optimization. The same way that totally non-OOP techniques like function pointers and std::function tend to be difficult barriers for a compiler as well. If you know based on the domain that only 2 behaviors are possible, or only N data types are possible, then indeed a bool or a variant is probably a better choice than using inheritance.
No, it's not. Articles like this one however *are* a clear example of the Strawman fallacy. You don't get to tell people that are advocating using OOP, or using OOP as a tool, what their view is. This article doesn't have any code at all, but most such articles typically start with a horrible example of a class with getters and setters for every single data member, no discussion of invariants, virtual used strangely, implementation and interface inheritance mixed together, etc, and then rip it apart. I would definitely agree, most OO code written in this vein of articles, is bad code! But it doesn't show anything.
As for OOP "invariably going wrong": all code goes wrong. If the requirements change more than refactoring efforts are put back into it, over time the mismatch will continue to increase. Really the question is how to write code to minimize the amount of effort and mismatch over time; this trait could be called robustness. I've yet to see even a half-decent case made that OOP techniques in C++ have no place in robust code.