Using Code Metrics in Visual Studio Team System 2008

Posted by: Subodh Sohoni , on 3/31/2008, in Category VSTS & TFS (Azure DevOps)
Views: 40004
Abstract: Developers need to have feedback about the quality of code immediately after the code is written. Team Edition for Software Developers which is part of Visual Studio Team System provides an integrated tool named Code Metrics to calculate how the code is written based upon some quantifiable criterion and shows the results in a separate window. Calculations are based upon criterions like Cyclomatic complexity, number of lines in code and inheritance depth etc. all resulting in maintainability index of the code. In this article we will take an overview of Code Metrics built into VSTS.
Using Code Metrics in Visual Studio Team System 2008
 
Developers need to have feedback about the quality of code immediately after the code is written. Team Edition for Software Developers which is part of Visual Studio Team System provides an integrated tool named Code Metrics to calculate how the code is written based upon some quantifiable criterion and shows the results in a separate window. Calculations are based upon criterions like Cyclomatic complexity, number of lines in code and inheritance depth etc. all resulting in maintainability index of the code. In this article we will take an overview of Code Metrics built into VSTS.
Code Metrics provides feedback to developers on following factors:
1.    Maintainability Index: this index is a computed using a formula as illustrated in Visual Studio Code Analysis Team Blog: Maintainability Index = 171 - 5.2 * log2 (Halstead Volume) - 0.23 * (Cyclomatic Complexity) - 16.2 * log2 (Lines of Code). This formula is based upon the work done at Carnegie Melon University.
 
Range of values for maintainability index is from 0 to 100 where 0 is the least maintainable code and 100 is perfectly maintainable code. Naturally higher values i.e. the ones tending towards 100 are going to be welcome. To make the things simple, code metrics tool in its results provides icons for the part of the code (a class or a method) along with index values to show how much maintainable it is. Red icon represents maintainability index values less than 10 which mean that those parts of the code need immediate attention of the developer. Values between 10 and 20 are accompanied with yellow icon to bring to notice the parts of the code which are not well maintainable. All the values above 20 are adorned with green icon to show that the represented part of the code is maintainable.
 
2.    Cyclomatic Complexity: This value is the count, for each part of the code, of various branches in the part of the code. Branches can be because of various statements which can make decisions like if and switch statements or they can be loop statements like for, while, do etc. Lower values are better as they provide more readable and maintainable code.
 
3.    Class Coupling: This value is a count for each class on which that class depends. If the coupling count for a class is high which means that it depends upon many other classes then the chances that the code of this class will break due to some changes in the dependency classes is high. That is why as a good practice, this number should be as low as possible. Solution is to provide loose coupling or Service Oriented Architecture if possible. To identify common components and abstract them as a service will be a good practice. Class coupling number does not take into account the primitives like int32 and String etc. 
 
4.    Inheritance Depth: This number represents the number of types which are above the type being reported in the inheritance tree. This value is measured from Object class which is at level 0.
Results Window for Code Metrics
Results of the code metrics calculation are made available in a separate window which can be opened from either the View menu or from the Analyze menu. Once the window is opened we can calculate the code metrics by clicking on the analyze button as shown with a red circle in Figure 1. Results are shown in a grid which has a row for each module (e.g. class, method) for which code metrics can be computed. For each measurable criterion as are mentioned above, there is a column in the grid. Figure 1 shows the Results Window
CodeMetricResults
Figure 1
We can filter the results of code metrics based upon values of any of the columns in the results window. To set the filter we can use the menu bar of the code metrics results window. In the menu bar we can select the column which will be the criterion and then set the minimum and maximum values which we want to set filter on. It is not possible to set the complex filter.
It is also possible to remove some of the columns or add them back again in the results window. It is also possible to change the sequence in which they appear from left to right.
We can export the results of code metrics either to the clipboard or to MS Excel in a table form. These results then can be used to do further offline analysis.
One of the best features for management is the ability to create workitems directly from the code metrics results window. We can use the context menu to create various types of workitems and assign them to developers for refactoring the code which does not have good maintainability.
Limitations and Constraints
Some of the things that code metrics feature of Team Edition for Software Developers cannot do are as follows:
1. Calculating code metrics for only part of the code in a module.
2. Providing custom columns in the code metrics results window.
3. Pinpointing the code lines due to which maintainability of the code of a method is reduced.
4. Exclusion of certain type of code like generated code from metrics calculations and results.
5. Calculation of code metrics cannot be set as check-in policy
Conclusion
Microsoft has provided an excellent tool to get an idea about the maintainability of the code, immediately after it is written, in the form of Code Metric Calculation. It provides these results based upon scientifically measured parameters and proven formulae. Results are shown in a separate window and they can be filtered as well as their view can be customized to suite the requirements.
I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed
  
 
 
 
 
 
 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Subodh is a consultant and corporate trainer. He has overall 28+ years of experience. His specialization is Application Lifecycle Management and Team Foundation Server. He is Microsoft MVP – VS ALM, MCSD – ALM and MCT. He has conducted more than 300 corporate trainings and consulting assignments. He is also a Professional SCRUM Master. He guides teams to become Agile and implement SCRUM. Subodh is authorized by Microsoft to do ALM Assessments on behalf of Microsoft. Follow him on twitter @subodhsohoni


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by RizwanSharo on Monday, April 21, 2008 3:58 AM
HiSubodh,

Article is a nice itroduction but it would be much better if you may include more information, a bit more detail and ofcourse a sample project beofore and after factorization proposed by Code Metrics Tool.

Anyway it was helpful.

Best Regards,
Rizwan a.k.a RizwanSharp
Comment posted by Subodh sohoni on Tuesday, April 22, 2008 12:29 PM
Thanks Rizwan for your comments. I will consider your suggestion while writing next articles. May be, I will rewrite part of the same article.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook