Understand the complexity and maintainability of your code using Code Metrics in Visual Studio – Depth of Inheritance

By | December 21, 2015

This is the fourth post in the series of Understand the complexity and maintainability of your code using Code Metrics in Visual Studio. In this post we will explore another important code metrics called “Depth of Inheritance” . As the name says, this code metrics is very easy to understand with respect to the concept of Inheritance.  This indicates the index or position of of a class with in a class hierarchy .

To start with, if you verify the Depth of Inheritance for a newly created blank console application, the value would be always 1.



As all classes inherit  directly from Object hence they would have a depth of inheritance value of 1.

Now add two additional level of inheritance like Base –> External –> Program and run the code metrics.


You will find the level of changes on the Inheritance, and Base Class with value 1, External with value 2 and Program is the 3rd level of inheritance.  This is just simply calculated with the position of the hierarchies they are in.


Note, we have an interface IBase that implemented by BaseClass, but that does not counted in the depth calculation

Below snapshot shows the mapping of Depth values with the Classes in the Class Diagram .


Couple of points to remember :

  • This Depth of Inheritance value does not include the depth of any implemented interfaces.
  • You will find the highest overall depth in the namespace and project level within the respective solution.

Nested type hierarchies in classes can be difficult to follow, understand, and maintain; hence it is recommended to have a minimum level of inheritance hierarchies.  As a standard, it is recommended to have a level of inheritance level below four.

Here is the list of previous post from this series.