04 July 2006

Dependencies Metrics

In his paper about Stability (1997), Robert C. Martin describes a set of principles and metrics that can be used to measure the quality of a large object oriented designed proyect in terms of the interdependence between its packages.

The metrics

  • Abstract types: The number of abstract types contained in the assembly.
  • Total types: The number of total types contained in the assembly.
  • Abstractness (A): The ratio of abstract types in the assembly to the total number of types. A = abstract types / total types.
  • Efferent Couplings (Ce): The number of types inside the assembly that depend upon types outside the assembly.
  • Afferent Couplings (Ca): The number of types outside the assembly that depend upon types within the assembly.
  • Instability (I): I = Ce / (Ce+Ca). Measure of the facility in changing the assembly.
  • Distance from the Main Sequence (D): In an Abstraction vs Instability space, the distance from the assembly (I,A) possition to the Main Sequence (the line of maximum balance between abstractness and instability). D = |A+I-1|/sqrt(2)
  • Normalized Distance (D'): The above distance ranged between [0,1]. D’ = |A+I-1|
The Principles
  • Stable Abstraction Principle (SAP): Reference's direction must be from a less abstract to a more abstract assembly.
  • Stable Dependency Principle (SDP): Reference's directon must be from a more instable assembly to a more stable assembly.
Examples

<- The figure on the left shows a bad dependency (the red arrow) because As1 depends on As2, but As2 in less abstract than As1 and more instable too.






-> The figure on the right shows a good dependency because it goes in the abstraction's direction (As1 is more abstract thanAs2) and also in the stability's direction (As1 is more stable -less instable- than As2).

Why to use them


  • They are easy to calculate (already exists tools to do the job -at least for java-)
  • They are widely used and approved.
  • They will help a lot in determining the proyect's health in terms of its dependencies, although it is possible that not ALL references seen as bad references below the eye of this principles are REALLY bad references.
  • They allow to continuously monitor the quality aspects of code that can affect the long-term viability of your software architecture.
Tools for calculating them

For Java:
For .Net: