6 ms·
> If everyone hates it, why is OOP still so widely spread? Most importantly, not everyone hates it. There are definitely warts involved, and IMO the 2 biggest
by hn_throwaway_99 6y ago
> If everyone hates it, why is OOP still so widely spread?
Most importantly, not everyone hates it. There are definitely warts involved, and IMO the 2 biggest warts are now largely understood:
1. People understand the pitfalls of inheritance now, so you'll see "composition over inheritance" MUCH more than you'll see inheritance as the recommended way to do things.
2. I don't know how widespread this feeling is, but as someone who moved from Java to Node/Typescript a couple years ago, I feel like I can confidently say structural typing, in practice, is MUCH easier, better and fun to work with than nominal typing. I can think of times I literally spent days trying to work through some refactoring in Java that was a nightmare because of the constraints of nominal typing, where the structural typing approach of TypeScript makes it trivial.
- formerly_proven 6y agoC++ templates are structural / duck typing. Powerful stuff, though a simple core. Right now Rust has some strong hype as a C++ successor, but I also hear that it doesn't really support OOP per se as a traits-based language. And I'm just thinking -- but I like my classes.
- pornel 6y agoI like Rust's solution. It doesn't have inheritance, which eliminates majority of OOP "architecture astronautics". It still has encapsulation and polymorphism, so you can do OOP, but the language nudges you to keep it simple.
- AnimalMuppet 6y agoELI5: How do you have polymorphism without inheritance? That is, if I have a Foo with a virtual function f(), and I have Bar and Baz that inherit from Foo and have their own implementations of f(), then I have polymorphism. But if I don't have inheritance, then what? Do I have different instances of Foo that have different implementations of f(), but they're still all Foo (rather than something derived from Foo)?
- deleted 6y ago[deleted]
- dralley 6y ago>But if I don't have inheritance, then what? Do I have different instances of Foo that have different implementations of f(), but they're still all Foo (rather than something derived from Foo)? Yes, these are called "Trait objects" in Rust. https://doc.rust-lang.org/book/ch17-02-trait-objects.html https://doc.rust-lang.org/book/ch17-02-trait-objects.html
- PaulDavisThe1st 6y agoput super-simply: Inheritance: T IS-A Foo Composition: T HAS-A Foo Neither: T IS-DESCRIBED-BY Foo
- pjmlp 6y agoNeither => Component Based Programming, "Component Software: Beyond Object-Oriented Programming" https://www.amazon.com/Component-Software-Beyond-Object-Oriented-Programming/dp/0201178885 https://www.amazon.com/Component-Software-Beyond-Object-Orie... https://www.amazon.com/Component-Software-Object-Oriented-Programming-Addison-wesley/dp/032175302X https://www.amazon.com/Component-Software-Object-Oriented-Pr...
- ywei3410 6y agoI see the other replies mention Rust/Haskell's traits and typeclasses; but a better example to use imo is Python's duck typing. If both objects `Foo` and `Bar` have a method called `baz`, calling `object.baz` will dispatch to the objects `baz` function, no inheritance needed. In that way, there are 3 ideas when you have inheritance. The first is reuse of code. The second is a relationship between 2 classes/types such that you get the Liskov Substitution principle. The third is dynamic dispatch based on object (or type/class in most cases). Almost all FP languages have the third (type classes, traits, generic interfaces, the list goes on) and the first (default implementations of traits/typeclasses) but none have the second.
- HelloNurse 6y ago
- centimeter 6y agoIf you're just doing inheritance you're usually much better off with a typeclass-based system. Agreed that structural typing is nice, but it's not restricted in any way to OOP.
- hn_throwaway_99 6y ago> Agreed that structural typing is nice, but it's not restricted in any way to OOP. Totally agree, but what I'm arguing is that a lot of the pain points people associate with OOP are actually more about the pain points of a nominally-typed system, and when you use OOP with a structurally-type language a lot of those pain points go away.
- dang 6y agoOk, we've taken universal hatred out of the title above.
- erik_seaberg 6y agoA little-known social network has just brought about world peace, film at eleven …
- smabie 6y agoAlso row polymorphism can solve most of the problems subtyping can without sacrificing type inference and without the complexity. Too bad it hasn't really caught on.
- wwright 6y agoIt's actually very popular in the JS world these days
- contravariant 6y agoThat seems like a somewhat convoluted way to have informal interfaces.
- thu2111 6y agoSubtyping doesn't require sacrificing type inference. HM type inference yes, but not the kind you get in languages like Kotlin.
- hinkley 6y ago3. Most objects embody shared state, and we have reached a point where the consequences of that can be severe. Object mutability is an attractive nuisance. Which is a shame because I like bits of OOP.
- imtringued 6y agoMutability is one of the most overused constructs but this has nothing to do with OOP. The vast majority of variables and fields simply never mutate. That's why its overused. People use a feature simply because it's the default. Making variables immutable by default and requiring explicit mutability would do a lot of good in many programming languages.
- andrekandre 6y agothe other thing is, alot of "state" is emergent or can be derived... lots of people seem to not think in that way and complicate things with too many variables i see this kind of code alot: person { age: UInt birthday: Date } whereas if you have any sense of when "now" is its much better to just calculate the age based on current date person { age: UInt { birthday.YearsUntilNow() //slightly trivialized ^^ } private(set) birthday: Date }
- colordrops 6y agoAnother major issue that isn't called out almost ever is that, at least within the context of an object, member variables and methods are basically global, so you never know where the state is being modified. This is especially bad in large classes.
- humanrebar 6y agoThat's because OOP advocates presume SOLID principles, including the Single Responsibility Principle.
- hn_throwaway_99 6y agoMeh, I feel that things like "single responsibility principle" are easy to say, but in practice (a) defining what you mean by "single responsibility" is a challenge, and (b) you're often trading off ease of handling things internally within a class for more difficulty in how you have to communicate with other classes (see microservices). I agree though, if your classes are so big it's difficult to keep track of which methods are updating private member variables then that's a good sign your class is too big.
- esperent 6y agoThe problem, then, is large classes. Make your classes small. Single responsibility principle.
- seanmcdirmid 6y ago> I feel like I can confidently say structural typing, in practice, is MUCH easier, better and fun to work with than nominal typing. Structural typing is crazy flexible, but you pay for it with worse type error reporting (forgetting something in an interface is now 100 errors rather than 1) and the inability to encapsulate (a structure type can't model private members, of course). There was a lot of research in addressing these problems in the 90s, but for some reason none of that work has made it into any mainstream structurally typed language today.
- humanrebar 6y agoThere are other ways to encapsulate besides private member functions. And, honestly, private member encapsulation is overrated. I rarely see a design that would have been dramatically improved by language-enforced member accessibility restrictions. The problems are typically more basic than that, like complete disregard of the Single Responsibility Principle.
- seanmcdirmid 6y agoI like private sometimes, but encapsulation is at its core tied to the notion of identity and hence nominal typing. If a name stands to represent something rather than its structure, well, that name is basically encapsulating. Even in functional languages names are used all the time with a huge amount of encapsulation behind them. They just don't have the benefit of a type system to manage that.
- Xenograph 6y ago> There are other ways to encapsulate besides private member functions. Could you elaborate a bit here? I've searched for solutions in this area before without much success. I'd be interested to hear more.
- wa1987 6y agoMaybe he's referring to closures.
- BurningFrog 6y agoI'll point out the obvious: There can only be widespread hate for things that are widely used.
- wvenable 6y ago> so you'll see "composition over inheritance" MUCH more than you'll see inheritance as the recommended way to do things. So much that you'll have people advocate doing inheritance using interfaces and composition which results in exactly the same thing with all the same pitfalls but at least it's not "inheritance".
- apta 6y ago> I feel like I can confidently say structural typing, in practice, is MUCH easier, better and fun to work with than nominal typing. That's the issue with these kinds of topics. For me, it's the other way round. Having worked with golang on significant code bases, the way they handle interfaces is just terrible IMO. It's been quite friction heavy, and loses out on one of the most important part of nominal typing: being able to use interfaces to tag classes. The hacks you see in golang to get around it, while introducing dangerous behavior, is quite astounding.
- deleted 6y ago[deleted]
- fendy3002 6y ago> I feel like I can confidently say structural typing, in practice, is MUCH easier TIL it has name! And I share your opinion, it's much easier, especially when doing immutability due to property spread, etc.
- codygman 6y ago> , I feel like I can confidently say structural typing, in practice, is MUCH easier, better and fun to work with than nominal typing. I can think of times I literally spent days trying to work through some refactoring in Java that was a nightmare because of the constraints of nominal typing, where the structural typing approach of TypeScript makes it trivial. The obvious takeaway here in some respect is gradual typing is better and nominal typing is annoying for little gain. What if the true answer were you need a more flexible and powerful type system. I think that moderate so-called "pragmatic" languages are the worst experience because of their lack of guiding principles.