Thursday, September 25, 2008

Code Metrics color vs. Code Analysis warnings – what’s the difference?

Recently I have read an interesting question on MSDN forums:
“Why the color indications in Code Metrics Results tool window (red/yellow/green) are not in sync with Code Analysis warnings generated (when corresponding CA rules such as AvoidUnmaintainableCode are enabled)?”

It appears, that when Code Analysis warnings are fired on certain methods, the expectation might be that those methods will be marked as red/yellow in Code Metrics Results window.

The expectation is incorrect, since the color indication in Code Metrics Results window relates only to maintainability index (as described on FxCop blog) and therefore corresponds only to AvoidUnmaintainableCode warnings. Other CA warnings (such as AvoidExcessiveComplexity) may or may not cause the maintainability index to go into red/yellow range; each rule warning is fired when individual metric value falls into rule-specific range (as described in this helpful post). Thus while there is some correlation between Maintainability rules and Code Metrics Results tool window color coding it is not one to one.

Monday, June 16, 2008

Review Code Metrics your way – in Excel

In addition to filtering function, there is another (almost magical) function in Code Metrics tool window – “Export to Excel”. If you feel more comfortable reviewing numeric data in Excel, or the number of results in code metric calculation is very large (which easily happens even for middle sized projects), you just click on magic button on the Code Metrics window toolbar, and voila! Your results are available in Excel spreadsheet, with advanced sorting and filtering functionality, and graphs, and statistics …

And if you are Excel whiz, you can save historical results over the time and perform historical code metrics results comparison.

Enjoy!

Code Metrics Filter gotcha

A neat way to review Visual Studio 2008 code metrics results (and for large projects the list may become pretty large) is to use built-in filter functionality:

One may select any available column (Maintainability Index etc.) in Filter combo box, and specify Min/Max range. Additional neat feature of Code Metrics window is that all filter ranges applied are added to Filter combo box list, and thus can be easily reused:

The downside of this feature is that once you have applied several filters the combo box list can become pretty long. To remove the custom filters from the list one may clear certain registry entry value, viz. HCU\Software\Microsoft\VisualStudio\9.0\EnterpriseTools\CodeMetrics key, value MRUList) (sorry for not finding more elegant supported way of doing it).