Coupling And Cohesion

What do we mean by coupling between classes?

Coupling is usually defined as the degree by which one class depends on others. This is usually measured through the number of references both in and out of different types (global data, protected variables etc).

Why should we worry about coupling between classes?

High coupling in a class implies that there is much dependence on other classes. This tends to imply that changing the class may force changes in the dependent classes. This is sometimes known as dependency rot.

Conversely Low coupling increases encapsulation, which makes classes easier to;

  1. Understand and Change: Reducing dependency fan out from a class decreases the number of classes that must be considered to understand the function of the class.
  2. Reuse: Low coupling increases encapsulation. This in turn makes a class easier to reuse as it has less associated ‘baggage’ to be considered.

Coupling is often considered in conjunction with Cohesion (see below) as design principals when reflecting on the responsibilities of a class. However there are no hard and fast rules. This is just a trade off to consider when making design decisions.

How is coupling measured?

In a metrics sense coupling is measured simply as the number of classes that are called or call a single class. The standard measure, originally proposed by Chidamber and Kemerer in the early 1990’s, takes this approach.

So does the Type of coupling matter?

Yes, very much so. The worst kind of coupling occurs when one class modifies the data in another class. Passing large (partially unused) data structures around is also found upon.

The types of coupling, in order of lowest to highest coupling, are as follows:

So far we have been considering static coupling. What about dynamic coupling. Why might this add complexity to the issue?

By considering Dynamic coupling we are considering temporal dependencies (i.e. dependencies in execution order) between classes. Problems can occur in highly coupled classes when they expose observable state as the order (or even worse the timing) in which individual methods on a class are called must be maintained.

So what about cohesion?

Cohesion is a measure of how strongly related and focused the responsibilities of a class are.

The general design principal is to promote Low Coupling and High Cohesion.

Cohesion from a metric point of view is measured by comparing the use of variables within a class.

Cohesion measures of 0ne are ideal. However it should be noted that this is by no means an ideal metric and its measurement can be misleading in certain circumstances.

See Wikipedia Definition


Have your say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




Safari hates me
IMPORTANT! To be able to proceed, you need to solve the following simple problem (so we know that you are a human) :-)

Add the numbers ( 15 + 15 ) and SUBTRACT two ?
Please leave these two fields as-is:

Talks (View on YouTube)