Hacker News new | ask | show | jobs
by jiaweihli 4359 days ago
I think this is a tooling issue. People initially tend to favor inheritance because it looks cleaner than composition. Mixing a lot of unrelated code in the same class makes things hard to find. (which method applies to which composed object?)

In languages that build in a concept of traits/mixins however, this isn't an issue.

2 comments

I disagree that mixing remove this issue. Even with mixins, you will still have places where all of that boilerplate from composition comes back, just with a concept like Python's `super` thrown back in.

And mixins in the wild are rarely so pure as people like to think they are - they often inherit from their own parents or other mix-ins, creating the diamond (or worse!) inheritance problem outlined in the article.

I think so too.

In JS it's rather easy to compose objects. In Java it feels clunky, you code looks kinda wrong when you done.

I don't quite understand this. In my opinion JS "classes", if you want to call them that, look, feel and act very clunky. My opinion, of course... Please could you explain to me how you feel Java "classes" feel clunky and look wrong? I'm genuinely curious, and open to being convinced.

Note I substituted your usage of "objects" with classes, because they're vastly different concepts. I know, murky water when it comes to JS, but still.

My point was, classes and inheritance feel natural in Java. Object-Composition feels natural in JS.