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

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

Code Metrics is one of the important software measures that give you an insight of your code maintainability and complexity. No one likes over engineered or too complex code.  As a developer when you are writing your code, you must adhere those boundary values of metrics to ensure your code is well written, understandable and maintainable. In short, Code Metrics is an important measure that let us understand the complexity and maintainability of the code.

While discussion with many developers recently, I found there are  lot of developers still didn’t know about this tool or such metrics or why should you use ?  or not clear about it   – Well, By taking advantage of these metrics, as a developer, you can understand which classes, which methods, which module should be reworked or refactor. You can identify complexity,  potential risks, implementation flaw with in your project.

Visual Studio uses five code metrics to help you understand your code better. They are :

  1. Line of Code
  2. Class Coupling
  3. Depth of Inheritance
  4. Cyclomatic Complexity
  5. Maintainability Index

codemetrics

I will explain explain each one of them in details in individual posts so that you have better understanding of each of them.  In this post lets have a look how to start seeing code metrics of your applications.

Start Calculating Code Metrics in Visual Studio

You can start calculate code metrics from the project context menu Analyze –> Calculate Code Metrics

image

You can run the code metrics for a single project, multiple projects or even for entire solution.  Alternatively you can start code metrics from the main menu, Analyze –> Calculate Code Metrics –> For Solution / For Selected Project

image

In case you have multiple project selections, the menu option will change to “Calculate Code Metrics for Selected Project ( s )

image

Viewing and Analyzing the Code Metrics Results

Once you run the code metrics, Visual Studio will analyze the selected project / projects and display the code metrics results in the “Code Metrics Results” windows as shown in below screenshot.

image

image

Once Project level metrics is calculated, you can drill down to each and individual  method to analyze their metrics data.

image

That’s it. You have the detailed metrics listed for the projects.  Now you need to understand

  • these metrics values
  • how they differ
  • what is the boundary value / min / max
  • How does these value gets calculate
  • and how to optimize them

In the next post we will start with Code Metrics – Line Of Code

Hope this helps !

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.