The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Coupling is not neccessarily a bad thing!
by Christopher Diggins
April 30, 2005
Summary
There is appears to be a school of thought that code coupling is to be avoided at all costs. This is a frustrating over-simplification.

Advertisement

A mantra I keep hearing over and over again is: inheritance is dangerous because it introduces unneccessary coupling. I disagree.

Coupling refers to dependencies between modular section of code, be they functions, classes, namespaces, header files, whatever.

Unneccessary coupling of code leads to code which is hard to maintain. Essentially what happens is a domino effect where changes to one section of code neccessitate changes in entirely unrelated sections of code. With some experience programmers quickly learn the harmful effects of over-coupling.

Judicious coupling (such as through inheritance) however is neccessary to a certain degree in order to maintain code cohesion, (i.e. logical grouping of related segments of code), and can be used to reduce unneccessary coupling elsewhere.

Within the context of OOP a class is the primary modular unit. A class which makes all of its fields public (without good reason) is an example of over-coupling. Any change to the class implementation will reverberate and affect any other class which plays with its member fields.

When one class inherits from another it increases coupling, obviously, but it does so in a very controlled manner. The thing that many programmers seem to be overlooking is that when using base classes, often what happens is that a new class is created solely to represent common functionality, so coupling between this base class and inherited classes is to be expected.

Consider the following example:

class Cat {
  void meow();
  void eat(); 
  void sleep(); 
};

class Dog {
  void bark();
  void eat(); 
  void sleep(); 
};

class Bird {
  void tweet(); 
  void eat(); 
  void sleep();
};
Clearly this would be more effectively written as:
class Pet {
  void eat(); 
  void sleep();
}

class Cat : Pet {
  void meow();
};

class Dog : Pet { 
  void bark();
};

class Bird : Pet {
  void tweet(); 
};
There is of course a coupling between Pet and the other classes but this coupling is correct and reduces the neccessity for coupling in other contexts by increasing the level of abstraction which can be used.

My point is simply that coupling is not automatically a bad thing.

Talk Back!

Have an opinion? Readers have already posted 35 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use