Measure Depth of Inheritance and Class Coupling in Visual Studio

Both come under code matrices and best practices. Now a days, I am writing articles on unit testing, code coverage and best practices, so I thought I should write an article to cover both topics. As a developer our purpose is not only to develop applications but to develop applications in the proper way, to improve performance and maintainability of the application.

People talk about de-coupled architecture, where one component will be very loosely coupled with another one. In the Visual Studio environment we can measure the depth of inheritance and class coupling values. So, let's start with a little theory.

Depth of Inheritance

Depth of Inheritance tells the number of class hierarchies of a class. From an example class C is inherited from class B and again B is inherited from A; then the Depth of Inheritance of class C will be 3. So, the best practice says that, the depth of class should be always be less. The average depth might be 2 or 3, but not more than that. As we know, when we derive one from another again and again, it will increase the complexity.

Class coupling

Class coupling defines how many classes directly depend on a class. And it's good to always implement fewer dependent architectures.

So, the value of class coupling too should less to be a good architecture.

Ok, fine, so now we will see how to measure the Depth of Inheritance and class coupling in the Visual Studio environment. Just copy and paste the following code to a console application.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Drawing;

namespace ConsoleApp

{

    class baseClass

    {

    }

    class deriveClass : baseClass

    {

    }

    class Program

    {

        static void Main(string[] args)

        {

 

        }

    }

}

The code is extremely simple, we have just inherited one class from another class, nothing much. We will now measure the inheritance level and class coupling in the Visual Studio environment. I am using Visual Studio 2012, if you have a different version then you may see a different screen, but the value should be the same.

Fine, now right-click on the application and click on the “Calculate Code Metrics” option.

Calculate Code Metrics

After clicking on that, Visual Studio will calculate the code metrics of this application and you will find the following screen.
 
code metrics of this application

Here we are seeing that the Depth of Inheritance is 2 and the class coupling value is 1, the inheritance depth is 2 because there are 2 level class hierarchies in the deriveClass class. And the class coupling value is 1 because baseClass is coupled with deriveClass. So, one class is dependent on deriveClass.

Now let's make a little modification in the code and run the same test again. Here is our modified code.
 

namespace ConsoleApp

{

    interface Itest

    {

    }

    class baseClass

    {

    }

    class middleClass : baseClass

    {

    }

    class deriveClass : middleClass, Itest

    {

    }

 

    class Program

    {

        static void Main(string[] args)

        {

 

        }

    }

}

The implementation is a little interesting, we see that the derive class is inherited from middleclass and again middleclass is derived from baseClass. So the inheritance level should 3. And we are implementing the Itest interface into deriveClass. So, now deriveClass is coupled with both middleClass and the Itest interface, so that the class cloupling value should 2. Let's run the test and check with an discussion.

cloupling

And we are seeing the same result.

Conclusion


I hope you now have a clear concept of Depth of Inheritance and Class Coupling concepts. And know how to check code metrics of your code.

It's always recommended to implement best practices and analyze code in this way.