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.
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.
Figure: 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.
Figure: 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).
Figure: 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.
No comments:
Post a Comment