Tuesday, April 5, 2011

VS 2010 Code Analysis – Code Metrics

One of the most important aspects of software is it’s quality. In fact, improving the quality is an integral part of any process improvement initiative. However, as with many other things, quality is better understood and effective if we can express that in terms of quantitative metrics as appose to qualitative measures. If you have followed even a basic computer science program, you may have learnt an array of such metrics. How ever, accurately calculating these metrics was not possible without using specially designed tools. Fortunately, Visual Studio had this capability for some time, but with Visual Studio 2010, the metrics calculation has greatly improved. For instance, the VS2010 even consider complex code blocks such as try..catch and switch when calculating the code metrics.

Calculating Code Metrics

Calculating code metrics in VS2010 is pretty straightforward. If you want to calculate code metrics for the entire solution, you may right-click the solution and select “Calculate Code Metrics” from the context menu. If you just need to calculate metrics for a given project, just right-click that project and select Calculate Code Metrics.

Figure: Solution Context Menu

Depending on your selection (Solution or selected projects), you may see the calculated metrics on the Code Metrics Results window. Check the following figure taken from an on-going project.

imageFigure: Code Metrics Results window

Analyzing Code Metrics Results

As you can see from the above figure, VS2010 calculates number of useful metrics, how ever the most important one among them is the Maintainability Index. This index is calculated based on few intermediate metrics such as Cyclomatic complexity and number of code lines. If you carefully analyze the above figure, you might see some small color coded squares on left-side of the metric cell. Basically, these colors are to indicate the overall health of the project in terms of maintainability.  At the time of this writing, the Maintainability Index is divided into 3 regions and each region is assigned a color.

Figure: Maintainability Index Regions

After analyzing the project level metrics, you may drill down each project up to method level to identify low maintainable code and hopefully improve them.

imageFigure: Metrics up to method level

While the Code Metrics Results windows provides powerful filtering and selection capabilities, for a large project it is bit difficult to extract the useful information. Fortunately, VS2010 has a nice feature to open the complete metrics data using Microsoft Excel (please note that you have to have Excel install in your machine). You just need to click the Excel button found at the top bar of the Code Metrics Result window (see the figure bellow).

imageFigure: Open Code Metrics in Excel

Figure: Code Metrics  opened in Excel

In Excel, you are not only able to analyze the data more effectively, but also can generate very useful graphs which you can even show to the business users.

Figure: Visualizing Code Metrics using Excel

No comments:

Post a Comment