Visual Studio Team System
Code Metrics Overview

Code metrics is a set of software measures that provide developers better insight into the code they are developing. By taking advantage of code metrics, developers can understand which types and/or methods should be reworked or more thoroughly tested. Development teams can identify potential risks, understand the current state of a project, and track progress during software development.

Software Measurements

The following list shows the code metrics results that Visual Studio calculates:

  • 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.

Anonymous Methods

An anonymous method is just a method that has no name. Anonymous methods are most frequently used to pass a code block as a delegate parameter. Metrics results for an anonymous method that is declared in a member, such as a method or accessor, are associated with the member that declares the method. They are not associated with the member that calls the method.

For more information about how Code Metrics treats anonymous methods, see Anonymous Methods and Code Analysis.

Generated Code

Some software tools and compilers generate code that is added to a project and that the project developer either does not see or should not change. Mostly, Code Metrics ignores generated code when it calculates the metrics values. This enables the metrics values to reflect what the developer can see and change.

Code generated for Windows forms is not ignored, because it is code that the developer can see and change.

Code Metrics Results window

After Visual Studio analyzes your code, it displays the results in the Code Metrics Results window. The window has a toolbar at the top and columns for displaying the results that are calculated.

The Hierarchy column contains a tree view of the code hierarchy that you can expand or collapse to see the level of detail that you need. The remaining columns show the calculated results. You can hide or arrange the result columns as you want. For more information, see How to: Add, Remove, or Rearrange Columns.

The Maintainability column contains an icon in addition to the numeric result. A green icon indicates a relatively high degree of maintainability. A yellow icon indicates a moderate degree of maintainability. A red icon indicates low maintainability and a potential trouble spot. These color indicators correspond to severity categories that are used by the FxCop rule AvoidUnmaintainableCode. This rule fires an error if the maintainability index is lower than 10, a warning if the index is between 10 and 20, and neither an error nor warning if the index is higher than 20. The maintainability index is a synthesis of three metrics: cyclomatic complexity, lines of code, and computational complexity. Its values are not expressed in units.

You can sort the results in the window by any column. To do this, click the column heading. Click the column heading again, and the window will be sorted in reverse order.

See Also

Other Resources

Tags :


Community Content

Proforma
confusion about used metrics

Is visual studio using Halstead Volume or computational complexity in order to calculate the Measurability index?

Tags :

Page view tracker