Don’t inherit code

I learned a long time ago that there are essentially three different kinds of relationship between two classes. They are, in order of increasing tightness:

  1. Uses, in which I care only about your public API;
  2. Creates, in which I also need to know your class;
  3. Inherits/extends, in which I can also see some of your workings and I become part of you.

Don’t do that last one. Ever.

Many of the GoF patterns help move code up this list, from tight towards loose coupling. Except TemplateMethod, which not only encourages inheritance, it also creates a circular dependency by having the superclass only “work” in the presence of extensions. Bad.

Don’t inherit code. Bad.

About these ads

3 thoughts on “Don’t inherit code

  1. My rule of programming.

    Don’t say “don’t” ever.

    Inheritance has its place and is incredibly useful. So is composition. Horses for courses.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s