Calculate Code Metrics Tools in Visual Studio 2013
Code metrics tool shipped with Visual Studio 2013 provides tools to calculate code metrics for Visual Studio programmers for software quality evaluation and assurance.
To calculate code metrics for your projects in a Visual Studio solution will help you improve your software quality by guiding developers for minimizing code complexity and increasing code maintainability.
In this Visual Studio tutorial, I'll only give a short start up information for code metrics calculation tool in Visual Studio 2013 putting a side all other Visual Studio Diagnostic Tools that will help developers build a better, high quality software at the end of the day.
Code Metrics Tool Output in Visual Studio 2013
Let's start from the end.
Here is a sample calculated code metrics results for a single console application project with a single namespace and two classes displayed on Visual Studio 2013 Code Metrics Results window.
As you will realize at first, Visual Studio provides 5 figures at first hand for developers to measure code metrics of their projects.
Here are the code metrics: Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling, and Lines of Codes.
Code Metrics tool provides a hierarchical view code metrics measurements of codes developed within the Visual Studio solution beginning top level item solution to leaf level class methods.
Starting from top level solution code metrics, a developer can drill down to namespaces, classes and methods in a hierarchical order.
So you can concentrate on code portions with high complexity and poor maintainability for a better software at the end of whole product development efforts.
How to read code metrics? What do these code metrics (maintainability index, cyclomatic complexity, depth of inheritance, class coupling and lines of codes) mean for a software quality assurance developer?
Software Measurements or Code Metrics
Visual Studio 2013 calculates the following code metrics and software measurements required for quality assurance of a software product.
Maintainability Index indicates how easy it is to maintain the measured code.
Maintainability Index is calculated within the range starting from 0 to 100.
The code is as easier as to maintain with increasing maintainability index.
A software developer should target to increase maintainability index with changes on the software.
Visual Studio 2013 also provides a traffic signals representation for the Maintainability Index.
A green signal is shown on the Code Metrics Results windows for measurements between 100 and 20.
Yellow signal is used for measurements between 19 and 10. Yellow means a candidate code block exists for developers to increase its maintainability.
Red light for measured maintainability index within range 0 and 9 means target code has a low maintainability.
Cyclomatic Complexity indicates structural complexity of the measured software code.
If the software has too many different code paths within the program flow, the cyclomatic complexity will be high.
Depth of Inheritance shows the number of class definitions that inherits from a root class.
Class inheritance is very helpful for developers to provide solutions to specific requirements but if you inherit a lot, it adds an additional code complexity to the software project.
Class Coupling shows the complexity added by couplings to classes.
Class couplings are created by using additional and optional parameters, return types, etc.
As a code metric for better software architecture, your project should have low coupling.
Lines of Code gives an approximate number of code lines within the namespace, project, class or method, etc.
Number of lines of code should be small in an ideal case. If you see very high numbers, developers can split the code blocks into smaller but functional pieces.
High number of codes generally indicate that the code is doing more work and hard to maintain.
How to Calculate Code Metrics using Visual Studio
In Visual Studio 2013 main menu, you will see the Analyze title.
Analyze menu group provides Visual Studio Diagnostic Tools for improving the data quality and assurance for developers.
As seen in below screenshot, some of the diagnostic tools shipped with Visual Studio 2013 are Code Analysis tool, Code Clones tool and Code Metrics Calculation tool as well as Profiler and Performance and Diagnostic tools.
In order to calculate code metrics for all projects within the active solution, choose "Calculate Code Metrics for Solution".
If you deal with single project, you can run "Calculate Code Metrics for Selected Project(s)" options.
When the calculated code metrics are displayed in the Code Metrics Result window, you can export the list to Microsoft Excel using the "Open List in Microsoft Excel" icon.
The result window also has a filter screen to concentrate on specific figures and a add/remove columns option to add or remove desired code metrics from the list.