Infosys Microsoft Alliance and Solutions blog

« Expression Blend 2.5 still has some issues with Globalization | Main | Interesting times for Customer Service Industry »

Calculate Code Metrics feature in Visual Studio 2008

With more than 250 new features, Visual Studio 2008 includes significant enhancements in every edition, including Visual Studio Express and Visual Studio Team System. In this post,  I would like to draw your attention to this new feature in Visual Studio 2008 called “Calculate Code Metrics”.

In Visual Studio 2008, you will get the context menu option which reads “Calculate Code Metrics” (as shown in the figure below) for a Visual Studio solution as well as projects. This essentially means that you can calculate code metrics at a solution level as well as project level. Many of the third party Code Metrics tools that were available earlier were not capable of analyzing at a solution level, and hence I think it’s great that we have this feature now and what more do you want? – Its integrated with the Visual Studio. But please note that code metrics are available only for C#, Visual Basic, and C++/CLI projects that are not Web Site projects. So if you do a calculation on the solution, the website projects are skipped.

Currently the tool calculates the following 5 code metrics:

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

Now it’s really important to make some sense out of these numbers that the tool comes up with, so let’s briefly discuss what each of these means.

Maintainability Index
This number actually tells you how maintainable and good your code is. Ease of maintainability is a key metric, and a higher number here would mean that you have done a good job in making your code maintainable. This will be a number between 0 and 100, and higher the value better your code is.

Cyclomatic Complexity
Now if you have done FxCop analysis, you would come across this term many a time. At least that’s how I came to know more about this. This is calculated at a method level. It shows the number of linearly independent paths code can execute. The more the number of decision statements like if, switch, while etc.  you have in your method, higher will be the value of cyclomatic complexity, which means that your code is becoming less maintainable. A method with a low cyclomatic complexity is generally better.

Depth of Inheritance
This tells you the number of base classes. This is calculated as the (number of classes between say a class A and System.Object class) + 1. It’s good to keep this value to 6 or below as a value higher than that would mean that the code is less maintainable.

Class Coupling
This metric is calculated at a class level. This number shows the number of classes a particular class depends on. Now the classes being considered here will be the distinct non-inheritance related classes. Again we are expected to keep this number as low as possible, as this would make the class a more stable one and hence makes it a good candidate for reuse.

Lines of Code
Needless to explain what this metric means, isn’t it? This metric gives you the number of executable lines of code. This excludes white space, comments, braces and the declarations of members, types and namespaces themselves.  Again it is important to note that this would count the lines of code regardless of whether it was generated or manually written, so be a little careful when you use this information for project estimation purpose Laughing.

The results for all these metrics would come up in the Code Metrics Results window in Visual Studio 2008 as shown below.

Code Metrics Results Window

Another good feature that you have as part of this tool is the export to excel  feature, which means you can export the whole code metrics information from the VS window into an MS Excel sheet for further analysis.

The tool also provides filtering capability. You can select any metric and specify a range for the value and find the code elements which match the filter criteria.

All in all, I think this will be a very useful feature, which would give us a quick look at the quality of the code we are writing and ensure that we write good code and code that would be easily maintainable.

To know more about this tool, I would recommend you read the Code Analysis Team Blog.

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/microsoft-mt/mt-tb.fcgi/193

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)