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

By | December 13, 2015

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


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


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


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


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.



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


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 !