Introduction
Recently my superior requested to report some code metrics from my team. Unfortunately, Visual Studio professional was used by me and team members. This version does not provide ready to use tools for collecting these statistics. TFS server was not used either.
Note: The explanation for metrics are taken from Wikipedia and extension developer's blog. I am not affiliated with any of the tool authors mentioned in the article. All tools are free to use.
Background and Solution
I have reviewed a number of extensions for Visual Studio Professional, and have stopped on the following combination:
Tool to collect characteristics: Visual Studio Code Metrics PowerTool 10.0, by Microsoft Corporation
http://www.microsoft.com/en-us/download/details.aspx?id=9422
This is the command line tool, that analyzes code in your solution code and provides 5 important quantitative characteristics for Project Manager / Team leader.
Maintainability Index
This metric originally is calculated as follows:
Maintainability Index = 171 - 5.2 * ln(Halstead Volume) - 0.23 *
(Cyclomatic Complexity) - 16.2 * ln(Lines of Code)
But for this specific extension, the team decided to adjust this metric, quoting: As a result of the decreasing usefulness of the negative numbers and a desire to keep the metric as clear as possible, we decided to treat all 0 or less indexes as 0 and then re-base the 171 or less range to be from 0 to 100. Thus, the formula we use is:
Maintainability Index = MAX(0,(171 - 5.2 * ln(Halstead Volume) - 0.23 *
(Cyclomatic Complexity) - 16.2 * ln(Lines of Code))*100 / 171)
Cyclomatic Complexity
Cyclomatic complexity (or conditional complexity) is a software metric (measurement). It was developed by Thomas J. McCabe, Sr. in 1976 and is used to indicate the complexity of a program. It directly measures the number of linearly independent paths through a program's source code. The concept, although not the method, is somewhat similar to that of general text complexity measured by the Flesch-Kincaid Readability Test.
Cyclomatic complexity is computed using the control flow graph of the program: the nodes of the graph correspond to indivisible groups of commands of a program, and a directed edge connects two nodes if the second command might be executed immediately after the first command. Cyclomatic complexity may also be applied to individual functions, modules, methods or classes within a program.
Depth of Inheritance
This metrics was designed specifically for object-oriented analysis: Depth of Inheritance. Depth of inheritance, also called depth of inheritance tree (DIT), is defined as “the maximum length from the node to the root of the tree”.
Class Coupling
Basically, class coupling is a measure of how many classes a single class uses. A high number is bad and a low number is generally good with this metric. Class coupling has been shown to be an accurate predictor of software failure and recent studies have shown that an upper-limit value of 9 is the most efficient.
According to the MSDN documentation, 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 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 (also called LOC)
According to the documentation, 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. 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.”http://msdn.microsoft.com/en-us/library/bb385914.aspx
Presentation
Now, when we have a free tool to get the desired code metrics, I started to look for visualization tool, to access the information into easy to use form directly in Visual Studio. Fortunately, I was able to locate such a tool, and this free tool is called Code Metrics Viewer. It can be installed by NuGet package manager, and more detailed information on using the plugin can be found on its blog here.
This plugin provides a visual representation of characteristics, as well as provides additional exploration capabilities.
Points of Interest
This is a must have combination of tools for teams consisting of developers with different qualifications. This set of tools allows to organize code review process in a more efficient way.