The terms “afferent coupling” and “efferent coupling” are used to denote the direction of cross-component dependencies:
- Afferent Coupling (Ca) is the number of external components that depend on the inspected component: inward dependencies.
- Efferent Coupling (Ce) is the number of external components a component depends on: outward dependencies.
Dependencies’ Directions in Balanced Coupling
The balanced coupling model uses the terms upstream and downstream components to denote the direction of the flow of knowledge. Knowledge flows in the opposite direction of dependencies: when referencing an external component, you must first acquire knowledge of how to integrate with it–meaning knowledge flows from upstream to downstream components.
Therefore, in the balanced coupling model, efferent coupling is akin to the number of upstream components a downstream component has, and afferent coupling is the number of consumers an upstream component has.
Software Package Metrics
The concepts of afferent and efferent coupling are primarily used for calculating the following design metrics:
Abstractness (A) is the ratio of the number of abstract classes in the analyzed component to its total number of classes:
A = 0
indicates a completely concrete component, whileA = 1
indicates a completely abstract component.Instability (I) is the ratio of efferent coupling (Ce) to total coupling (Ce + Ca) of a component:
I = Ce / (Ce + Ca)
. This metric is intended to indicate the component’s resilience to change, withI = 0
representing a completely stable package andI = 1
representing a completely unstable one.Distance from the main sequence (D) is the perpendicular distance of a component from the idealized line
A + I = 1
, and is calculated as:D = | A + I - 1 |
. A component with a score located on the main sequence is considered to optimally balance its abstractness (A) and instability (I).
While these metrics are easy to calculate, their practical application is limited. Focusing solely on the number of relationships misses the essence of each dependency and its effect on system design.
Consider a component with one external dependency (efferent coupling) and 100 components depending on it (afferent coupling). The component’s instability score is extremely low: I = Ce / (Ce + Ca) = 1 / (1 + 100) = 0.0099
, suggesting it is very stable. However, assume that the outgoing dependency has high integration strength–intrusive coupling–while the incoming dependencies have low integration strength–contract coupling.
As a result, the single outgoing dependency has the potential to drive significantly more cascading changes than 100 dependencies with low integration strengths.