CSE Roadmap

Common tier · about 3 hours · 7 parts

Part 4 of 7 · 3 min read

Coupling

Coupling counts how many ties a module has to others, in and out.

Think of a contact list. Some people are called by many others. Some people call many others. A person who many rely on is hard to replace. A person who relies on many is easy to disrupt.

FSA, citing Yourdon and Constantine (1979), defines two counts for any piece of code (component, class or function):

Afferent coupling (Ca)

Afferent coupling is the number of incoming connections to a code artifact, meaning how many others depend on it.

Example: a logging class used by 12 other classes has afferent coupling 12.

Efferent coupling (Ce)

Efferent coupling is the number of outgoing connections from a code artifact, meaning how many others it depends on.

Example: a checkout class that calls the cart, the payment and the stock classes has efferent coupling 3.

The names are easy to mix up. The book gives two memory aids: "a" comes before "e" in the alphabet, as incoming comes before outgoing; and "e" matches the first letter of "exit".

From these two, Robert Martin derived more measures, which FSA presents.

Instability shows how likely a module is to break when something changes around it:

Instability I = Ce / (Ce + Ca)

A module that calls many others and is called by few has a high I (near 1). It is exposed to change in all it calls. A module that many depend on and that calls few has a low I (near 0). It is stable but hard to change, since changes spread to its many users.

Abstractness is the ratio of abstract elements (interfaces, abstract classes) to concrete ones: A = abstract / concrete. A program of 5,000 lines in one function has A near 0. A program drowned in abstractions has A high and is hard to follow.

Distance from the main sequence combines the two: D = |A + I - 1|. The ideal line is A + I = 1: stable modules should be abstract, and unstable ones should be concrete. Code near the line is balanced. The book names two bad corners:

  • Zone of pain: low abstractness and low instability. Lots of concrete code that many modules depend on. It is brittle and hard to change.
  • Zone of uselessness: high abstractness and high instability. Abstractions that nobody uses.

A worked example (made up):

Module Ca Ce I = Ce/(Ce+Ca) Abstract / concrete A D = abs(A+I-1)
Pricing 9 1 0.10 0 / 10 0.00 0.90
Orders 2 6 0.75 2 / 10 0.20 0.05
Interfaces 1 0 0.00 6 / 6 1.00 0.00

Pricing is in the zone of pain: nine modules depend on it, it calls almost nothing, and it has no abstractions. Any change risks breaking nine users. Adding an interface for it would raise A and bring D down. Orders is close to the ideal line. Interfaces is stable and abstract, which is fine for a module whose job is to be depended upon.

Common mistake

Reading a metric as a verdict. FSA says code metrics are blunt tools. They need interpretation, and a number alone does not tell essential complexity from accidental complexity.

Check yourself:

  • A class is used by 8 classes and uses 2. Find Ce, Ca and instability.
  • What does a high distance from the main sequence mean?