Visual Studio Code Metrics (Current Functionality)
Code Metrics was introduced in Visual Studio 2008. This feature allows developers to calculate various code metrics for projects and solutions. This feature calculates five separate metrics:
- Maintainability Index [higher=better]– Calculates an index value between 0 and 100 that represents the relative ease of maintaining the code. A high value means better maintainability. 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 [lower=better] – Measures the structural complexity of the code. It is created by calculating the number of different code paths in the flow of the program. A program that has complex control flow will require more tests to achieve good code coverage and will be less maintainable.
- Depth of Inheritance [lower=better] – 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.
- Class Coupling [lower=better] – Measures the coupling to unique classes through parameters, local variables, return types, method calls, generic or template instantiations, base classes, interface implementations, fields defined on external types, and attribute decoration. 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 [lower=better] – 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. 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 hard to maintain.
So, we have code metrics, what’s the problem? Well, you can view code metrics in Visual Studio (see image below), however, there is no way to calculate a project’s or solution’s code metrics outside of Visual Studio. That is, until now…
Visual Studio Code Metrics Power Tool (New Functionality)
With the introduction of the Visual Studio Code Metrics Power Tool, you can now calculate code metrics for a set of assemblies via the command line. The various metrics will be saved to an XML file.
To run code metrics via the command line:
- Download and install the Visual Studio Code Metrics Power Tool.
- Open a Windows command prompt.
- Change to the following folder: C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop (or add it to your PATH environment variable).
- Run metrics.exe /? to see the command usage.
Example: Run code metrics for all DLLs
metrics.exe /f:*.dll /out:metrics.xml
This will calculate code metrics for all DLLs (in the current folder) and save the resulting code metrics to the XML file metrics.xml. There are several other options not shown here which you can see if you run metrics.exe /? to view the command usage.
So, Now What?
Now that we have the ability to calculate code metrics outside of Visual Studio, what can it be used for? Well, I am sure there are lots of reasons that you and everyone else will come up with. A few possible cases for using this newfound functionality might include:
- Generate code metrics during an automated build and store the results along with everything else in the drop folder (or where ever makes the most sense for your situation).
- Create a custom build activity that automatically fails (completely or partially) a build if the code metrics exceed certain thresholds.
- Build a custom Team Foundation Server 2010 Warehouse Adapter to house the resulting code metrics.
- Whatever else you might think of… If you have other great ideas for using code metrics, leave a comment.