In our previous post, we discussed The Acyclic Dependency Principle, in this chapter we will go over next principle called SDP (Stable Dependencies Principle)
Depend in the direction of stability
Robert Martin
We need to ensure modules that are easy to change should not be depended on by modules that are harder to change.
What is a stable component?
Let’s say we have module X, and we have three modules that depend on X. Then X is referred to as Independent or Stable component as it has no external reason to change.
Let’s say, we have module Y which depends on three modules, so any change in any of those three modules would cause Y to change and so Y is referred to as Dependent.
Stability Metrics
Fan-in – Number of incoming dependencies, i.e., number of classes outside component that depend on classes within the component.
Fan-out – Number of outgoing dependencies, i.e., number of classes inside component that depends on classes outside the component.
Instability –
I = Fan-out / (Fan-in + Fan-out)
The range of this metric is between 0 and 1.
I = 0 => maximally stable component, component X from above example
I = 1 => maximally unstable component, component Y from above example
As per SDP, I of a component should be larger than I of the components that it depend on. I should decrease in direction of dependency.
Not all components should be maximally stable as that would create a very rigid system. We need a balance of stable and unstable components. Ideally in our system, we should have unstable components on top and then should be depending on stable components at bottom.
Thanks for stopping by! Hope this gives you a brief overview in to Stable Dependencies Principle. Eager to hear your thoughts and chat, please leave comments below and we can discuss.