Live data from Hacker News

Why composition is often better than inheritance

joostdevblog.blogspot.com

91–97 of 97 posts

Re: Why composition is often better than inheritance

#91
post #82

Earlier quoted context omitted.

The type enhancement means that traits could replace classes completely in scala, which is kind of exciting. The problem with traits is that state must be inlined even if implementations are not (so they are a mixture of mixins and small talk style traits). I'm well versed in how they are implemented in scalac. What non-CL mixins are not inheritance based? Python?

Oh absolutely, the type enhancement is what makes it so interesting to use, but the point is that it's not necessary for it to be a mixin. It's a very nice extra. I'm not sure about python, but Ruby completely inlines the mixin's code. It needs to do this because instance variables can be created anywhere, including from inside the mixin, but they still need to apply to the object of the actual class. This gives inte…

I use a mixin pattern extensively in C# that involves an interface to say the object provides a mixin object (C# also provides for partial classes, but its not good enough). Polymorphism is preserved via this method (though all clients delegate through MixX). Actually, I don't see private mixins (that don't at least effect the object's v-table) to be very interesting: why not just create and delegate to an object then?

Re: Why composition is often better than inheritance

#92
post #84

Earlier quoted context omitted.

See Gabriel's Incommensurability essay: http://www.dreamsongs.com/Files/Incommensurability.pdf

Thanks, I'm forgetting this paper too often...

RPG uses mixins as a direct example of the concept, very interesting read if you can get through his style.

Re: Why composition is often better than inheritance

#93
post #69

Earlier quoted context omitted.

> While it's a well-written article, it really seems like beating a dead horse I graduated college about 10 years ago, and I was never taught anything near composition over inheritance. I was told about inheritance, but had to learn from other coworkers and experience that composition is much favored over inheritance. Now I mentor a number of junior developers and they need to be told composition over inheritance oft…

"I was told about inheritance, but had to learn from other coworkers and experience that composition is much favored over inheritance." You weren't taught that, because it isn't true. Inheritance is appropriate sometimes, composition at other times. In particular, when a two objects are modeled by an ISA relationship, you should use inheritance. When they're modeled by HASA, you should use composition. A Dog is not a…

> A Dog is not an Animal that includes Barkable and PeeOnHydrantable, and a SpaceShuttle is not a descendant of Airplane

I think there is probably a lot of room for debate on this.

(Edited to make it less pithy and therefore probably much less irksome)

Re: Why composition is often better than inheritance

#96
post #84

Earlier quoted context omitted.

Thanks, I'm forgetting this paper too often...

RPG uses mixins as a direct example of the concept, very interesting read if you can get through his style.

My Lisp Machine has a lot more examples of this...

Re: Why composition is often better than inheritance

#97
post #95
post #23

Earlier quoted context omitted.

The stuff you mix in has code. Interfaces do not.

What code do you mean? He only wrote "pass". I guess I'm confused because he still used inheritance.

There was a place to write code. In this example, obviously, none was written. But interfaces leave no opportunity.
Post reply on HN