BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2008+

Visual Studio Code Metrics

Judging the maintainability of an application's source code objectively during code reviews can be difficult. To help, some Visual Studio editions can calculate code metrics, such as cyclomatic complexity, depth of inheritance and lines of code.

Class Coupling

The class coupling metric indicates the amount of coupling between types in your source code. The value is the number of other types that a class uses through properties, fields, method parameters, return values, etc.

High levels of coupling limit the ability for a type to be reused, so a lower value is desirable. You should aim for a class coupling lower than ten. Microsoft includes a code analysis warning for class coupling (CA1506) but this is set to a much higher value. If you do see the class coupling warning there is a high likelihood that your code requires refactoring.

Lines of Code

Lines of code describes the amount of source code that exists in your project or solution. If the value is high for an individual member you may consider refactoring that member. However, sometimes it is necessary to have long methods.

The lines of code metric can be useful if you are performing a major task that is dependent upon the size of a project. For example, you may wish to completely redevelop some software using a different language or platform. In this situation the metric gives an indication of the amount of work that might be involved.

It is important to note that the metric is not the number of lines of C# code. It is actually based upon the number of lines of intermediate language (IL) code after building your software. The value includes some, but not all, automatically generated code.

Calculating Code Metrics

Generating code metrics for a project or solution is easy. Open the "Analyze" menu and choose one of the "Calculate Code Metrics" options. These allow metrics to be created for a single solution, a project or a group of projects. You can also right-click solutions or projects in the Solution Explorer and choose "Calculate Code Metrics" from the context-sensitive menu.

Once calculated the values are displayed in the Code Metrics Results window, pictured below:

Visual Studio Code Metrics

Note that the code metrics are organised into a hierarchy of projects, types and type members. You can use the controls at the left of the grid to expand and collapse each item to review the results.

13 April 2013