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

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

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.

image

 

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.

image

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.

image

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 .

image

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.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.