Inside VSTS

Code Metrics

Mickey covers the basics on code metrics in Visual Studio 2008 Team System.

Code metrics is a new feature added to the Visual Studio 2008 Team System Development Edition and the Visual Studio 2008 Team System Suite Edition. This feature provides a set of five measurements that give you insight into the health of your code. Using code metrics, you can get a better understanding of what aspects of your code should be potentially changed or tested.

Code metrics can also be a useful tool for analyzing code you may not have written, but have to maintain. Using code metrics, you can isolate the potential trouble spots in the code early, giving you plenty of time to refactor the problem sections.

In this article, we'll talk about code metrics in Visual Studio Team System (VSTS). We'll start out by identifying the different code metrics available, and talk about good and bad values for each. Then we'll dive into an example of how to run code metrics in Team System.

The Five Code Metric Types
Team System uses five code metrics to help you understand the health of your code: Class Coupling, Depth of Inheritance, Lines of Code, Cyclomatic Complexity and Maintainability Index.

Class Coupling: Class coupling is a measure of the dependencies a class has on other classes. This dependency is measured through parameters, method calls and interface implementations, just to name a few. The higher this number, the more likely a change in one class will affect other classes. For class coupling, a low value is good and a high value is bad.

Depth of Inheritance: Depth of Inheritance indicates the number of different classes that inherit from one another, all the way back to the base class. Depth of Inheritance is similar to class coupling in that a change in a base class can affect any of its inherited classes. The higher this number, the deeper the depth of inheritance and the more potential for causing breaking changes in your code when modifying a base class. For Depth of Inheritance, a low value is good and a high value is bad.

Lines of Code: Lines of Code indicates the number of executable lines of code in a method. This count is an approximate number, based on the Intermediate Langauge (IL) code. It includes only executable lines of code, so comments, braces, white space and member declarations are excluded. As you would expect, the more lines of code in your application, the more code there is to maintain. For Lines of Code, a low value is good and a high value is bad.

Cyclomatic Complexity: Cyclomatic complexity represents the number of unique paths through your code. More simply, it's the number of "if" statements, loops and switch cases in your code. Code with a high cyclomatic complexity can be hard to test and maintain, as there are a number of different paths through the code that must be tested. For Cyclomatic Complexity, a low value is good and a high value is bad.

Maintainability Index: The Maintainability Index is a combination of several metrics, including Cyclomatic Complexity and Average Lines of Code, as well as computational complexity, based off the Halstead Volume function. The maintainability index is a value between 1 and 100. Unlike the other four code metrics discussed previously, for this metric, the higher the value, the easier your code will be to maintain. There are three rating levels for this index:

  • GREEN: This is a rating between 20 and 100, and indicates code that should be maintainable.
  • YELLOW: This is a rating between 10 and 19, and indicates the code may be semi-difficult to maintain.
  • RED: This is a rating between 0 and 9, and indicates the code may be very difficult to maintain.

Now that you have a basic understanding of the code metrics available in VSTS, let's look at an example of using code metrics.

A Code Metrics Example
To get started with code metrics, you'll need to have either Visual Studio 2008 Team System Development Edition or Team Suite. Open Visual Studio, and then open the project you want to calculate code metrics against.

Running the code metrics is pretty easy. In Solution Explorer, right-click on the project or solution, and select Calculate Code Metrics. The time it takes to calculate the metrics will depend on the size of your project. Once it's done, it will open the Code Metrics Results window, shown in Figure 1.

In this figure, we've run code metrics over the entire solution, which consisted of a Web site project, a C# project and several Test Projects. Code metrics will not run over a Web site project, hence the warning on the first line of results.

In looking at these code metrics results, we have good maintainability indexes all around for the projects. Using the tree view, you can drill down into individual methods. In Figure 2, we have drilled down into the Resume class, to view the code metric results for individual methods.

Overall, this project appears healthy, and should be easy to maintain. Next, let's look at an example of poor code metrics. Figure 3 shows you an example of some extremely poor code metrics.

As you can see, the maintainability index for the entire project is a 7, indicating this project will be very difficult to maintain. It has high numbers for both cyclomatic complexity and class coupling. The DoEvenMoreWork method looks to be the worst off, with a maintainability index of 0. It's obvious this code has a lot of problems, and will require some serious reworking in order to make long-term maintainability feasible.

In conclusion, code metrics is a powerful addition to the Team System tool set, enabling you to quickly and easily measure the health of your application and identify potential trouble spots early on. Whether you're calculating code metrics against code you've written, or using it to identify potential issues in code you have to maintain, you'll find code metrics an extremely useful tool.

About the Author

Mickey Gousset spends his days as a principal consultant for Infront Consulting Group. Gousset is lead author of "Professional Application Lifecycle Management with Visual Studio 2012" (Wrox, 2012) and frequents the speaker circuit singing the praises of ALM and DevOps. He also blogs at ALM Rocks!. Gousset is one of the original Team System/ALM MVPs and has held the award since 2005.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.