Depth of Inheritance Tree
The number of edges from a class to the root of its inheritance hierarchy.
Overview
Depth of Inheritance Tree (DIT) measures how far down a class sits in the inheritance
hierarchy. A class with no extends clause has DIT = 0. A class that directly
extends another class has DIT = 1. Each additional level of inheritance adds 1.
Deeper inheritance chains mean a class potentially inherits more behaviour from ancestors, making it harder to predict the class's full behaviour and increasing the chance that changes to ancestors break descendants.
Formula
DIT(C) = 1 + DIT(parent) otherwise
How tsmetrics computes DIT
tsmetrics resolves the full chain within the analyzed files using memoized recursive lookup. Cycle detection prevents infinite loops in the rare case of circular declarations.
When a parent class is defined outside the analyzed files (e.g. a
third-party library or a built-in like Error), tsmetrics treats that parent as the
root of the hierarchy. The child class therefore has DIT = 1, not 0, reflecting that it
does inherit from something even if the depth cannot be fully resolved.
Interpretation
| DIT | Interpretation |
|---|---|
| 0 | No inheritance — standalone class |
| 1 – 2 | Shallow hierarchy — generally well-structured |
| 3 – 4 | Moderate depth — review for excessive coupling |
| 5+ | Deep hierarchy — consider flattening or using composition |
Thresholds
DIT is an informational metric with no default warning or error thresholds. It is not configurable via tsmetrics.yaml.
Example
class Animal {} // DIT = 0 class Mammal extends Animal {} // DIT = 1 class Dog extends Mammal {} // DIT = 2 class Labrador extends Dog {} // DIT = 3 // External parent — treated as root class AppError extends Error {} // DIT = 1
Usage in detection strategies
- Refused Bequest — DIT > 0 is a precondition (the class must actually inherit from somewhere)
References
- Chidamber, S. R. & Kemerer, C. F. (1994). A Metrics Suite for Object Oriented Design. IEEE Transactions on Software Engineering.