This is a useful extension for Visual Studio 2010 which will calculate code metrics for your solution and display the results. It can be downloaded here though you will first need to install the Code Metrics PowerTool 10.0 which it uses to generate the statistics.
To perform the analysis:
- Open the Code Metrics Viewer window by either right clicking on the solution in the Solution Explorer and selecting Code Metrics Viewer or selecting View > Other Windows > Code Metrics Viewer in the menus.
- Click on Analyze Solution.
If you see a message box “Unable to locate the metrics calculator module” when you click on “Analyze Solution” you may (like me) not have read all the instructions and so not installed the Power Tool first. In this case after closing the message box the Options dialog will be displayed but clicking the “Locate Metrics Power Tool” button will do nothing as the Power Tool is not present. Install the Power Tool from the download above and then go to Tools > Options > Code Metrics > Tools and click “Locate Metrics Power Tool”; the Code Metrics Power Tool Path should be populated with the correct folder (for example “C:Program Files (x86)Microsoft Visual Studio 10.0Team ToolsStatic Analysis ToolsFxCop”).
The Microsoft Code Analysis Team Blog has a useful explanation of what the results mean.
The tool does a good job of flagging, um, “opportunities to refactor” in the code. The actual statistics are generated by the Microsoft Power Tool and this does seem to have a few quirks:
- A function or property with no lines of code – for example, an abstract property or a function that implements an interface but has no functionality – will have a red icon in the Lines of Code column. This does not seem to affect the Maintainability Index.
- In Tools > Options > Code Metrics > Tools there is a checkbox for “Do not generate metrics for generated code (/igc)”, which is unchecked by default. However, this still generates statistics for the partial classes generated by LINQ to SQL even though the links to this code in the Code Metrics Viewer window do not work.
- The Depth of Inheritance includes the inheritance hierarchy of classes in the standard libraries. So if your class inherits from System.Web.UI.UserControl it will show a Depth of Inheritance of 4 with an amber warning icon.
Note that in the Code Metrics Viewer window if you enter a new figure in the Min or Max combo boxes you appear to need to press Enter before clicking on the “Goto Next” button or the new value will be lost.
Using: Visual Studio 2010 Pro SP1; Code Metrics Power Tool 10.0; Code Metrics Viewer 1.6.0.