Improving Code Quality with Visual Studio Code Metrics

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction

Visual Studio 2017 code metrics is a set of important software tools for measuring the quality, code maintainability, and complexity of code written by a developer. These metrics also provide developers better insight into the code they have written by understanding the types of classes/methods/modules that should be reworked and thoroughly tested, identify potential risks, current state of code, track progress of development, and so forth. Code metrics are available with the VS Community, Premium, and Ultimate editions. Developers also can download the extension and install it in Visual Studio. It's a good practice to use code metrics to validate code; it will not only improve code quality but also make your application maintainable and less complex.

Software Measurements Metrics

Visual Studio provides following the list of the metrics to help you better understand your code:

  • Maintainability Index
  • Cyclomatic Complexity
  • Depth of Inheritance
  • Class Coupling
  • Lines of Code

Maintainability Index

Maintainability Index is a software metric that indicates how maintainable (easy to support and change) the source code is. A high value of the Maintainability Index means better code maintainability. The value of this index is between o to 100. Different color codes are used to signify the maintainability index. For example, a green rating, between 20 and 100, indicates that the code has highly maintainability. A yellow rating, between 10 and 19, indicates that the code is moderately maintainable. A red rating, between 0 and 9, indicates low maintainability.

Cyclomatic Complexity

As per Wikipedia: 'Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a program.' This metric measures the structural complexity of your code. It indicates a number of branches in the code by calculating the number of different code paths in the flow of the program. The value of this metric increases if we have nested if statements or switch statements. Your code will be highly maintainable if the value of this metric is lower.

Depth of Inheritance

This metric calculates how far down a class is declared in the inheritance hierarchy. The deeper the hierarchy in the inheritance tree the class is, the more complex it will be to develop, test, and maintain. A developer must implement inheritance carefully by keeping in mind that code should remain reusable and less complex.

Class Coupling

Class coupling is a measure on how one class is connected or dependent with another class. Good practice is to always implement fewer dependent classes or a low class coupling. A lower value of this metric is preferred because it also increases the complexity in your code.

Lines of Code

This one is relatively simple; it indicates the approximate number of lines of code written in a program. The count is based on the intermediate code generated by the Framework. A developer should design methods so that there are fewer lines of code. A high count indicates a method is trying to do too many things; that means a complex structure and it will be hard to maintain.

How To Generate Metrics Data

You can start calculating code metrics from the Analyze menu -> "Calculate Code Metrics for Solution." In case you have multiple project selections, the menu option will also provide the "Calculate Code Metrics for Selected Project (s)" submenu.

Analyze code metrics
Figure 1: Analyze code metrics

Alternatively, you can start code metrics for a single project or an entire solution from the project content menu. Right-click -> "Calculate Code Metrics."

Calculate code metrics
Figure 2: Calculate code metrics

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" window.  From the result window, you can drill down to each and individual method to analyze their metrics data.

Code metrics result window
Figure 3: Code metrics result window

You also can display the "Code Metrics Results" window by clicking the Analyze menu -> Windows -> "Code Metrics Results."

Code metrics result
Figure 4: Code metrics result

A developer can filter the Code metrics data based on the type of code metrics and by providing min and max value of indexes of the selected metrics. The filter feature helps a developer easily find the code block or function they need to work on.

Filter option
Figure 5: Filter option

You can Add/Remove metrics columns by selecting the options from the "Add/Remove Columns" options. By default, all the metrics are checked.

Add/Remove columns
Figure 6: Add/Remove columns

To export result data in Excel format, right-click in Code metrics results and select "Export to Excel." A developer can use the data for analysis once they have the information exported to Excel format.

Export to Excel
Figure 7: Export to Excel

Conclusion

That's it for today. Visual Studio's Code Metrics features are very powerful and, when used, ensure your code is written properly and well maintained. Every enterprise has their own standards of code maintenance, but I would recommend running the code metrics tool regularly for better code quality and good practice.



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date