Thursday, November 20, 2008

Visual Studio Code Metrics Overview

As a developer I have always care about the quality of the code I write. But generally the quality of your code you think you are implementing will depend on the level of expertise you have and how important are best practices, standards and naming conventions. When you work with other experienced developers you will always find that code varies from developer to developer, and although the code can be different and use different coding practices it doesn't necessarily means that the code quality is better or worst.

In Visual Studio Team System 2008 you have a great tool for analyzing your own code or even the other developer's code in an objective way. This tool is called Code Metrics and is pretty easy to use and understand.

First of all, let's understand the different metrics the tool uses:

  • Maintainability Index: Calculates an index value between 0 and 100 that represents the relative ease of maintaining the code. A high value means better maintainability. The calculation is based on the Halstead Volume, Cyclomatic Complexity and Lines of Code. Color coded ratings can be used to quickly identify trouble spots in your code. A green rating is between 20 and 100 and indicates that the code has good maintainability. A yellow rating is between 10 and 19 and indicates that the code is moderately maintainable. A red rating is a rating between 0 and 9 and indicates low maintainability.
  • Cyclomatic Complexity: Measures the structural complexity of the code. It is created by calculating the number of different code paths in the flow of the program such as if blocks, switch cases, and do, while, foreach and for loops then adding 1 to the total. A program that has complex control flow will require more unit tests to achieve good code coverage and will be less maintainable.
  • Depth of Inheritance: Indicates the number of class definitions that extend to the root of the class hierarchy. The deeper the hierarchy the more difficult it might be to understand where particular methods and fields are defined or/and redefined. At the class level the number is created by calculating the number of types that are above the type in the inheritance tree starting from 0 and excludes interfaces. At the namespace and project level the calculation consists of the highest Depth of Inheritance calculation of all of the types within the namespace or project.
  • Class Coupling: Measures the coupling to unique classes through parameters, local variables, return types, method calls, generic or template instantiations, base classes, interface implementations, fields that are defined on external types, and attribute decoration. The calculation excludes primitive and built-in types such as int32, string and object. Good software design dictates that types and methods should have high cohesion and low coupling. High coupling indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types.
  • Lines of Code: Indicates the approximate number of lines in the code. The count is based on the IL code and is therefore not the exact number of lines in the source code file. The calculation excludes white space, comments, braces and the declarations of members, types and namespaces. A very high count might indicate that a type or method is trying to do too much work and should be split up. It might also indicate that the type or method might be difficult to maintain.
Once you are clear of what each metric means and how it is measured you can run and use code metrics to see how healthy is your project code.
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. After you open the project, 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. The window is pretty straight forward on showing you the analysis results for each metric used. It also gives you a quick reference on how healthy your application code is. With this tool you can filter the results by max and min values, add, remove or rearrange columns in the results window, copy results to the clipboard, export results to excel or create work items based on the results.
For this post I calculated the code metrics of a trading small applications I have using WCF duplex communication. The window looks like the following:
As you can see the application code looks healthy. When you perform your tests make sure everything looks right, since letting pass bad code metrics will result in a very expensive maintainability process.

Hope this is useful information for the readers.

Cheers!
-aB

No comments:

Post a Comment