3 ms·
What if I told you that OOP only provides illusions of reuseable components and that most OOP components aren't really that reuseable. In order to build reusea
by leafboi 6y ago
What if I told you that OOP only provides illusions of reuseable components and that most OOP components aren't really that reuseable.
In order to build reuseable components you need components that compose well. Object composition does not allow for composition without specific glue code for each integration.
For example:
functions addOne and addTwo can compose to form addThree with a single general function composition operator, no glue code needed and no need for addOne or addTwo to be aware of each other. (addThree = addOne . addTwo)
OneAdder and TwoAdder cannot compose without specific glue code or specific interface code to allow for DI. ThreeAdder = composeAdder(OneAdder, TwoAdder) would be implementation specific. Additionally ThreeAdder(TwoAdder, OneAdder) makes ThreeAdder aware of TwoAdder and OneAdder. These are not qualities of reuseability. The existence of a mutating object breaks reuseability.
OOP does have it's use cases but the hate does comes from somewhere. One of the main downsides of OOP is the fact that current practices makes it one of the least reusable paradigms ever. DI and object composition are illusions. That being said it's not all that bad... not all business apps need the form of extreme reuseability that FP provides, so OOP is often good enough.
- contravariant 6y agoDon't end your class names with -er.
- leafboi 6y agoNaming is a superficial issue that is subjective as opposed to a structural problem that is pervasive. The point of the example wasn't to talk about naming. It was to talk about a fundamental structural problem with OOP.
- PaulDavisThe1st 6y agoClearly you are not familiar with the classic aphorism: "There are 2 hard problems in computer science: cache invalidation, and naming things" or its cousin: "There are 2 hard problems in computer science: cache invalidation, naming things and off-by-one errors".
- thu2111 6y agoOOP languages compose on the level of a class rather than function because an individual function rarely represents something you want to work with independently. That's the whole point of OOP - it says what real programs care about is objects, not functions. But OOP languages do have lambdas and function pointers, so can easily do something very close to that. In Kotlin: fun addOne(value: Int) = value + 1 fun addTwo(value: Int) = value + 2 val addThree = fun (value: Int) { addOne(addTwo(value)) } There are libraries that add a pure functional composition operator via a + operator overload, I think. Even in Java you can do function composition, though it's not well known and a bit verbose: import static java.lang.invoke.MethodHandles.*; import static java.lang.invoke.MethodType.*; static int addOne(int i) { return i + 1; } static int addTwo(int i) { return i + 2; } void combine() throws Throwable { var lookup = lookup(); var type = methodType(int.class, int.class); var first = lookup.findStatic(Main.class, "addOne", type); var second = lookup.findStatic(Main.class, "addTwo", type); var combination = filterReturnValue(first, second); System.out.println(combination.invoke(1)); // Prints 4 }
- ronanyeah 6y agoReal programs care about data, and functions operate on data.