less_retarded_wiki/oop.md
Miloslav Ciz 0bbafe778e Update
2021-11-17 16:02:35 -06:00

34 lines
4.6 KiB
Markdown

# Object-Oriented Programming
Object-oriented programming (OOP, also object-obsessed programming) is a [programming paradigm](paradigm.md) that tries to model reality as a collection of abstract objects that communicate with each other and obey some specific rules. While the idea itself isn't bad and can be useful in certain cases, OOP has become extremely overused, extremely badly implemented and downright forced in programming languages which apply this abstraction to every single program and concept, creating [anti-patterns](anti_pattern.md), unnecessary issues and of course [bloat](bloat.md). We therefore see OOP as a [cancer](cancer.md) of software development.
Ugly examples of OOP gone bad include [Java](java.md) and [C++](cpp.md) (which at least doesn't force it). Other languages such as [Python](python.md) and [Javascript](javascript.md) include OOP but have lightened it up a bit and at least allow you to avoid using it.
You should learn OOP but only to see why it's bad (and to actually understand 99% of code written nowadays).
## Principles
Bear in mind that OOP doesn't have a single, crystal clear definition. It takes many forms and mutations depending on language and it practically always combined with other paradigms such as the [imperative](imperative.md), so things may be fuzzy.
Generally OOP programs solve problems by having **objects** that communicate with each other. Every object is specialized to do some thing, e.g. one handles drawing text, another one computes some specific equation etc. Every object has **data** (e.g. a human object has weight, race etc.) and **methods** (object's own [functions](function.md), e.g. human may provide methods `getHeight` or `drinkBeer`). Objects may send **messages** to each other: e.g. a human object sends a message to another human object to get his name (in practice this means the first object calls a method of the other object just like we call functions).
Now many OO languages use so called **class OOP**. In these we define object [classes](class.md), similarly to defining [data types](data_type.md). A class is a "template" for an object, it defines its methods and what kind of data it will hold. Any object we then create is then created based on some class (e.g. we create the object `alice` and `bob` of class `Human`). We say a the object is an **instance** of that class.
OOP furthermore comes with some basic principles such as:
- **[encapsulation](encapsulation.md)**: Object should NOT be able to access other object's data directly -- they may only use their methods. (This leads to the setter/getter antipattern).
- **[polymorphism](polymorphism.md)**: Different objects (e.g. of different classes) may have methods with the same name which behave differently for either object and we may just call that method without caring what kind of object that is (the correct implementation gets chosen at runtime). E.g. objects of both `Human` and `Bomb` classes may have a method `setOnFire`, which with the former will kill the human and with the latter will cause an explosion killing many humans.
- **[inheritance](inheritance.md)**: In class OOP classes form a hierarchy in which parent classes can have child classes, e.g. a class `LivingBeing` will have `Human` and `Duck` subclasses. Subclasses inherit stuff from the parent class and may add some more. However this leads to other antipatterns such as the [diamond_problem](diamond_problem.md). Inheritance is nowadays regarded as bad even by normies and is being replaced by [composition](composition.md).
## Why It's Shit
- For simple programs (which most programs should be) OOP is an unnecessarily high and overly complex abstraction.
- OOP is just a bad abstraction for many problems that by their nature aren't object-oriented. OOP is not a [silver bullet](silver_bullet.md), yet it tries to behave as one.
- Great number of the supposed "features" and design-patterns (setters/getters, singletons, inheritance, ...) turned out to actually be anti-patterns and burdens.
- OOP as any higher abstraction very often comes with overhead, memory footprint and performance loss ([bloat](bloat.md)) as well as more complex [compilers](compiler.md).
- The relatively elegant idea of pure OOP didn't catch up and the practically used OOP languages are abomination hybrids of imperative and OOP paradigms that just take more head space, create friction and unnecessary issues to solve.
- The naive idea of OOP that the real world is composed of nicely defined objects such as `Humans` and `Trees` also showed to be completely off, we instead see shit like `AbstractIntVisitorShitFactory`.
- TODO
## History