Eric Elliott eric at ericleads.com
Sat Jun 29 09:28:33 PDT 2013


"it could be quickly refactored as following"

Except that a bunch of animals already depend on Swimming, and now they all
need to be refactored as well, or you have to duplicate Swimming
functionality in more than one place, and then you have unexpectedly
divergent implementations across different animals (maybe a bug in one type
of swimming animal that doesn't exist in another, or bugs in both, you fix
it in one place, think it's fixed everywhere, only to discover later that
it wasn't.)

This is the famous unexpected divergence problem that arises from single
ancestor class inheritance... so even if you start out using class
inheritance, it can be problematic to switch to mixins and similar
strategies down the road.

- Eric


On Fri, Jun 28, 2013 at 7:41 PM, Claude Pache <claude.pache at gmail.com>wrote:

>
>
> Le 29 juin 2013 à 00:14, Eric Elliott <eric at ericleads.com> a écrit :
>
> <snip>
>
> I saw a recent example of one of the problems with single-ancestor
> inheritance in a talk. I wish I could remember which one. The illustrations
> were great:
>
> Animal
>   - Walking
>     - Monkey
>     - Human
>   - Flying
>     - Bird
>     - Bee
>   - Swimming
>     - Fish
>     - Whale
>
> Now we need alligator and duck.
>
>
> Interesting challenge! It may be hard to resolve in some languages, but in
> JavaScript, you merely need some imagination. Given:
>
> ```js
> class Flying extends Animal {
>     // implementation of Flying
> }
>
> class Swimming extends Animal {
>     // implementation of Swimming
> }
> ```
>
> it could be quickly refactored as following (I assume that the
> implementation of Swimming is sufficiently well-written; otherwise some
> amendments in its implementation might be needed):
>
> ```js
> class Flying extends Animal {
>     // implementation of Flying
> }
>
> function addSwimming(Animal) {
>     class Swimming extends Animal {
>         // implementation of Swimming
>     }
>     return Swimming
> }
>
> var Swimming = addSwimming(Animal)
>
> var FlyingSwimming = addSwimming(Flying)
>
> class Duck extends FlyingSwiming { }
> ```
>
> (It is true that `Duck instanceof Swimming` doesn't hold; however,
> duck-typing forbids the use of `instanceof` and even any typelike-check.)
>
> Note how JavaScript remains dynamic, even in the face of the
> ES6-class-sugar.
>
>
> —Claude
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130629/9c99848a/attachment.html>


More information about the es-discuss mailing list