Understand the complexity and maintainability of your code using Code Metrics in Visual Studio – Class Coupling

Understand the complexity and maintainability of your code using Code Metrics in Visual Studio – Class Coupling

In the previous posts of this code metrics series, you have learned the basics of Code Metrics and how to get started with it and how Lines of Code (LoC) Metrics  indicates how many lines of source code you have in your application and how you can Measure the  complexity in terms of length, cleanness and single responsibility. This post covers the second, and another important aspect of code metrics Called – Class Coupling.

In a very simple term, Class coupling is nothing but a measure on how one class is connected  or dependent with another class.  it’s good to always implement fewer dependent class or a low class coupling.

At each level of code this indicates the total number of dependencies that the class / types has on other types of class / items. This numbers does not include any primitive and built-in types.  The higher the number is, the more complex your app is going to be in terms of architecture.

Let’s try to understand this by following few basic scenarios:

Scenario 1 :

Let’s create a simple console application. With a new project you will have following code snippet.

 class Program
    {
        static void Main(string[] args)
        {
        }
    }

Run the  Code Metrics and observe the Class Coupling column for the results.

image
So, it is clear that, as of now there is dependency or connection with any other class for my class “Program”, hence the Class Coupling is set to 0 for all.

Scenario 2 :

Add the below two highlighted line, and run the code metrics once again.

class Program
{
static void Main(string[] args)
{
string name = "CSharp";
string version = "6.0";
}
}

Are you expecting any changes in result ? Absolutely no! They are primitive data types and using there would be no changes in the metrics results.

image

Scenario 3:

Add  an additional line to the above code base to print the values and run the code metrics once again.

class Program
{
  static void Main(string[] args)
   {
    string name = "CSharp";
    string version = "6.0";
    Console.WriteLine(name + version);
  }
}

This time you will notice some change, as we have a reference of another class – ‘Console’ .  And now the Class Coupling for Main Method turns into 1, and which is bubbling up and sum upto 1 for the entire project.

image
Hope till now it is very much clear.

Scenario 4:

Now don’t think adding / using Console multiple time would increase the class coupling. Now. It won’t.

class Program
{
static void Main(string[] args)
{
string name = "CSharp";
string version = "6.0";

Console.WriteLine(name + version);
Console.WriteLine(DateTime.Now);
}
}

Add another statement with Console.WriteLine, and run the Code Metrics Tools once again.  This time you will have increased code metrics, one for the Console and another for DateTime.

image

Having same class reference multiple time wont increased the coupling.

Scenario 5:

Similarly what you have done so far, and extend it for an external / your custom class, you should be able to map the coupling there after. For this instance, we have created once class called Student, and refer it from Program Class.

class Program
{
static void Main(string[] args)
{
string name = "CSharp";
string version = "6.0";
Console.WriteLine(name + version);
Console.WriteLine(DateTime.Now);
GetStudents();
}

static void GetStudents()
{
Student student = new Student();
var listofStudents = student.GetStudents();
}
}

In this case you will have Class coupling 4, where 2 is for Main() method and another 2 is from GetStudent() method. The student.GetStudents() does return as list<Student> here and which is adding one more reference here as var.

image

That’s it. Hope this is now very much clear that  – What is a Class Coupling and how it’s get calculated.

All in all, to put this together, consider you have following set of class in a project.image

and below class diagram shows how they are interrelated.

image

just try this out, and calculate the class couple for each class. By seeing the dependency of each class, I am sure you should be able to crack the number.

Alternatively, run the code metrics and you should have the result with you.

image

Hope this was helpful !

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.