18 ms·
Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)
- kkarakk 7y agoThis is the most obtuse and rambling critique of child extends parent i've ever seen in my life
- LordN00b 7y agoAre you sure? His argument is one simple paragraph: The `Car extends Vehicle` or `Duck extends Bird` type of tutorial obscures more than it illuminates. In good OO programming, we don’t make class hierarchies in order to satisfy our inner Linnaeus. We make class hierarchies in order to simplify the code by allowing different parts of it to be changed independently of each other, and to eliminate duplication (which comes to the same thing). Without any context as to what the code needs to accomplish, you can’t make a judgment about whether a particular design decision is good or bad.
- mlthoughts2018 7y agoAgreed, it’s a really concise and clear articulation of one of the main traps of inheritance thinking. I also think people are very receptive to this once you point it out. You’re not trying to make a class hierarchy because you come to the problem with a presupposed mental model. Instead you should be letting your attempt to solve the problem with no abstraction teach you about the places where abstraction would help. Maybe it should be organized as inheriting behaviors via mixins instead of specializing behaviors based on a hierarchical relationship with a parent type. Or maybe all the classes just need to be simple record / tuple wrappers and the methods that operate on them don’t belong to any class at all. The reason examples like car extends vehicle cause harm is that they can be superficially taken as an endorsement that you should bring preconceived mental models of relationships between components into the solution, before trying to solve it with zero abstraction and observing where this breaks down.
- lonelappde 7y agoIndentation breaks HN. Blockquote should not inherit from Codeblock. Please remove it and use > for quotes. > The `Car extends Vehicle` or `Duck extends Bird` type of tutorial obscures more than it illuminates. In good OO programming, we don’t make class hierarchies in order to satisfy our inner Linnaeus. We make class hierarchies in order to simplify the code by allowing different parts of it to be changed independently of each other, and to eliminate duplication (which comes to the same thing). Without any context as to what the code needs to accomplish, you can’t make a judgment about whether a design decision is good or bad.
- CamperBob2 7y agoAgreed, if the '1337 hax0rs at HN would implement quoting, then > would be a good way to express it. As it stands, > does nothing on HN, so we're treated to endless complaints about people misusing code blocks for quoting. The complaints are valid, since code blocks don't work properly on mobile. Complain enough about either of these issues and they'll gripe at you for complaining. It never occurs to them that fixing the problems would avoid both the need for complaining and the need for griping. Oh, well, life goes on.
- ben509 7y ago> You can’t add code to ducks. > You can’t refactor ducks. > Ducks don’t implement protocols. > You can’t create a new species in order to separate some concerns (e.g. file I/O and word splitting). It's odd he makes these complaints, because the comment he's responding has this caveat: > unless they are talking about writing a clone of The Sims or something In the context of a game or simulation, such classes can absolutely do all these things. And those seem entirely reasonable ways to teach someone about programming, while being more accessible and engaging than "let's talk about how to write an IO stack." * > Penguins don’t implement the “fly” method that can be found in birds. That's an important issue, though, which an OO tutorial needs to deal with. It points at two major problems: 1. The circle-ellipse problem[1]. 2. That nominative types have all the inherent problems that taxonomy has. And since one solution is "duck typing"[2] it's worth noting that it's entirely clear what it means, despite being named after ducks. [1]: https://en.wikipedia.org/wiki/Circle-ellipse_problem https://en.wikipedia.org/wiki/Circle-ellipse_problem [2]: https://en.wikipedia.org/wiki/Duck_typing https://en.wikipedia.org/wiki/Duck_typing * IO stacks are good to learn, but to do them in OO you have to understand IO first, and though it's very satisfying when you get tricky IO code right, it's not really something you can show to your friends.
- overgard 7y agoExperienced modern game programmers would never write a "Duck" class though. For instance, in Unity, it would be a set of small components representing narrow behaviors attached to a generic scene graph object. So the "Duck" would only exist as a data object that happens to have maybe a unique Quack component, along with a lot of common non-duck components like MeshRenderer, AudioSource, Collider, RigidBody, etc. You would see similar things in something like Unreal, or even in-house engines. The implementation is likely also to be way more interested in acting on aggregate data streams rather than enforcing strict encapsulation and object boundaries and such. It's still very OOP, but not in the sense of representing real-world taxonomies. I know there's an argument that this real-world metaphor is good for beginners, but at least in my personal experience I don't think that's the case. I remember when I was a kid, my dad tried to teach me coding with a lot of object oriented examples with like Cats and Dogs and stuff. He was really well meaning, but it never really made sense to me. I only kind of "got it" when I did something nobody would recommend anymore, and learned QBasic, and wrote a bunch of ghastly code with goto's all over the place and practically everything being a global. Awful code, but 10 year old me was able to reason about that a lot easier than polymorphism/encapsulation/scoping/etc.
- Ididntdothis 7y agoI tend to agree. The “duck extends bird” examples sound good at first but they have done a lot of damage to OOP.
- zmix 7y agoBut wasn't that how OOP was supposed to be, according to Alan Kay? That objects should have resembled real life objects, as close as possible and exchange messages with each other?
- hota_mazi 7y agoI find this a largely unsubstantiated claim, much like the "Once you learn BASIC, your brain is damaged beyond repair" from Dijkstra that is so depressingly repeated over and over, and which is complete nonsense. These are introductory metaphors that help people get up to speed with a complex concept. You refine your understanding of these as you become more expert on the subject.
- overgard 7y agoI definitely agree that like these things aren't going to cause any sort of lasting brain damage or something. It gets overwrought. I think that the taxonomy thing is kind of harmful though in that, as far as I can tell anyway, it only seems to be now that a lot of programmers are coming around to the idea that modelling things are taxonomies is less useful than other techniques. Some code sins are pretty easy to cleanup: if someone writes a function that's doing too many things, it's not too hard to break it up, or if someone names things badly, usually it's enough just to rename it to better concepts. But deep inheritance hierarchies are often a massive pain to clean up, if you can even clean it up at all (outside packages might come to depend on your weird structure for instance).
- Ididntdothis 7y ago"These are introductory metaphors that help people get up to speed with a complex concept. You refine your understanding of these as you become more expert on the subject." From my experience a lot of people get stuck with these introductory metaphors and never progress further. That's why I think they are harmful.
- deleted 7y ago[deleted]
- dang 7y agoDiscussed at the time: https://news.ycombinator.com/item?id=2914405 https://news.ycombinator.com/item?id=2914405
- colllectorof 7y ago>In good OO programming, we don’t make class hierarchies in order to satisfy our inner Linnaeus. This correctly identifies one of the worst problems with how people construct OO programs when they get out of college. They are infected with the idiotic desire to make neat taxonomies. They don't know why. They can't even explain the rules that make one taxonomy good and another one bad. They just feel some immense pressure to arrange things in trees. I've been like that when initially learning Java and it took a while to recover from it. It's like fucking brain damage from bad examples. Inheritance is not about creating taxonomies. Namespaces are not about creating taxonomies. Both are practical solutions to practical problems that occur when writing code. Inheritance is a result of several insights: 1. It's convenient to be able to sketch out a protocol which a set of objects will follow - without having to work out every damn detail about its implementation. 2. Most of the protocol can be implemented once and then reused. 3. Often you want to use the default protocol implementation with some changes. Instead of re-implementing the rest of the protocol, it's highly convenient to be able to specify only the differences. #3 is the core reason why inheritance was developed.
- hota_mazi 7y agoWell said. And #3 is also an area where Functional Programming falls completely flat. There is simply no easy way in any FP language that I know to capture such a simple mechanism as: "Reuse 90% of this piece of code but alter just this 10%". Specialization is a very, very powerful aspect of OOP, one that's extremely easy to explain and which comes in handy to model a surprisingly wide variety of problems.
- colllectorof 7y agoFunctional programmers will reply that you can use partial evaluation and higher-order functions to do that. The sad reality is that since so many people are only familiar with the strawman version of OOP rather than the real thing intelligently discussing the advantages and disadvantages of these paradigms is very hard.
- hota_mazi 7y ago
- hota_mazi 7y agoThe article completely misses the point because it sets up a large amount of straw men that are completely unrelated to the point The fact that "Car extends Vehicle" is sometimes a subpar modelization has nothing to do with OOP or inheritance and everything to do with the fact that the English language is not specific enough to describe what a Vehicle is well. Same goes with "Duck extends Bird". Yes, there are a few birds that don't fly. This problem has nothing to do with programming languages, inheritance, or OOP. I still find these two examples, "Duck extends Bird" and "Car extends Vehicle" as extremely powerful to teach beginners what OOP is about. Like all metaphors, they tend to leak if you push them too far, but they do a splendid job at introducing beginners to the concept of OOP, which is going to serve them well for probably most of their professional life.
- kizer 7y agoGeez, chill out. I can't stand profanity.
- kragen 7y agoI don't mean this in a gatekeeping way, but if you've never been inspired to use profanity by an OO design, you probably aren't yet in the target audience.
- remirk 7y agoThat doesn't make sense.
- hinkley 7y agoStart here perhaps: https://www.wired.com/2011/02/cussing-in-commits-which-programming-language-inspires-the-most-swearing/ https://www.wired.com/2011/02/cussing-in-commits-which-progr...
- kragen 7y agoDo you ever use profanity? When do you use it, about which topics?
- deleted 7y ago[deleted]
- hinkley 7y agoYou’ve given other people control over your emotional states. Not even with elaborate statements. Just with individual words. What sort of compromising positions do you suppose that’s put you into, or will in the future?
- deleted 7y ago[deleted]
- inopinatus 7y agoPot, kettle, black. Blasphemy is a subclass of profanity.
- jrochkind1 7y agoThis is good. I've even seen OO tutorials which make it explicit to give the bad advice that all your objects should match real world _things_. This does not match my experience of OO programming or what the actual challenges or rewards are: > We make class hierarchies in order to simplify the code by allowing different parts of it to be changed independently of each other, and to eliminate duplication (which comes to the same thing). Without any context as to what the code needs to accomplish, you can’t make a judgment about whether a particular design decision is good or bad. YES.
- crtlaltdel 7y agoi sat down with tech yourself java in n-days (forget the exact title) around 1998/99 and promptly returned the book because of my frustration with the `vehicle->wheeled vehicle->engine powered vehicle->motorcycle` explanation of OOP. i had been using a niche OOP lang (lpc) for quite a while and the examples in that book felt oddly ridiculous. just my opinion (at the time), but it certainly did not resonate with me
- deleted 7y ago[deleted]
- rdiddly 7y agoOh my god, not all birds fly, it ruins the whole thing!!!!! No, it's a perfectly fine place in the analogy, to have a teachable moment. Since not all birds fly, just don't make flight part of your base class. I mean? Or get clever and have flighted and flightless bird classes inherit from Bird? If you want to be that much of a pain about it, you can; the paradigm supports it.
- lngnmn1 7y agoWhy, these are flavors of CLOS or type-classes (rather type-traits) of Haskell. Trait define what-it-takes-to-be-a instead of just is-a. This idea is fundamental and have been realized by all serious programmers. Type-classes is the most important innovation since structural programming.