Skip to content
~/acordi_

2 min read

The class nobody wants to change

Three coupling metrics that explain why some components are so hard to change.

Every system has that difficult class or module that many developers avoid changing.

A highlighted central component with several other components pointing at it

If we think in terms of software architecture, there are three metrics we can use to measure why that happens:

  • CA (Afferent Coupling): how many components depend on me. It is my fan-in.
  • CE (Efferent Coupling): how many components I depend on. It is my fan-out.
  • I (Instability): calculated as the ratio CE / (CA + CE). The result always falls between 0 and 1.

To illustrate, imagine a component that is used by 3 others (CA = 3) and depends on only 1 (CE = 1). Its instability will be:

I = 1 / (3 + 1) = 0.25

How to read these numbers day to day

  • High CA = critical component. Any change to it ripples through everyone that depends on it. It should have very good test coverage, and any change deserves extreme care.
  • High CE = fragile component. It breaks because of others. The more dependencies it has, the greater the chance that a problem that may not even be its own blows up inside it.
  • I close to 0 = stable.
  • I close to 1 = unstable.

Stable does not mean safe

A component with I = 0.25, like the one in the example, may be precisely the most dangerous one to change, because, despite being stable, a lot depends on it.

The worst possible scenario is an unstable core: a component with high CA and rising CE. It remains critical to the system, but it starts importing the fragility of others and spreading potential failures across the entire application.

What is worth measuring

I don't think it is feasible to measure everything.
But by looking at the main components of our applications and tracking these metrics, we could better understand our system's ability to survive and stay relevant over time, and better size the effort required for that.


References: the CA, CE and I metrics were proposed by Robert C. Martin.

cd ../ // all posts