Friday 15 October 2010

What is coupling?

Coupling is a measure of dependencies. Where Component A depends on Component B, coupling exists.

The direction of coupling matters, there are two kinds:

  • Afferent – who depends on you.
  • Efferent – who you depend on.

There are characteristics and rules of thumb for different coupling “signatures”. For example:

  • An “Observer” class might have lots of efferent coupling but no afferent coupling i.e. the Observer will depend on lots of classes but few (or perhaps no) classes will depend on the observer. This is okay, it is probably expected.
  • Conversely, a framework component might have lots of afferent coupling but no efferent coupling i.e. lots of components might depend on the framework but the framework will depend on few (or perhaps no) components itself. Again, this is okay and is probably expected.

You may find coupling patterns within your application, depending on the specifics of your business domain.

When measured in this way, coupling becomes a useful metric. It enables you to ask questions like “If I change Component A, how many components will this impact?”. This information allows you to assess risk when making changes, therefore you are able to determine if you can change a component safely.

You should minimise both forms of coupling but having no coupling is not possible.


0 comments:

About Me