teichgraf's picture

FxCop

I recently discovered a great tool by Microsoft which is called FxCop.

What is FxCop
From Wikipedia:
FxCop is a free static code analysis tool from Microsoft that checks .NET managed code assemblies for conformance to Microsoft's .NET Framework Design Guidelines.

What it did for me
I used it in my own project. The first time I analysed it with FxCop, a lot of messages were printed (over 100). But not all rules which FxCop checks were really necessary for my project. So I disabled them global in the FxCop project or local in my code using the System.Diagnostics.CodeAnalysis.SuppressMessageAttribute.
For example I disabled "AssembliesShouldHaveValidStrongNames" and "DoNotDeclareVisibleInstanceFields", ... in the FxCop project and locally I supressed the message "IdentifiersShouldBeCasedCorrectly" with the attribute [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "OBB")] at the OBB class. But of course the most rule violations were fixed changing the code. Otherwise it would have been senseless to use FxCop.
FxCop doesn't only find design issues, it also finds real coding failures and possible performance problems. For example FxCop found a wrong used ArgumentException in my project.

So I can highly recommend to use FxCop with OpenTK or any other .Net project.
I tested OpenTK with the FxCop rule settings for my project and it printed over 11334 messages. But most of them could be disabled for the OpenTK project. For example "AvoidUncalledPrivateCode". But I also saw a wrong used ArgumentException in OpenTK.Utilities.

It was a real pain in the ass when I first started the FxCop analysis and I saw the bunch of rule violation messages. But soon it felt better and better after I have fixed the first problems and the rule violations stated by FxCop became less.

How to use

  1. You can download the latest version here.
  2. Start the program and a add a target (Dll, Exe) with "Menu" -> "Project" or context menu, ...
  3. Press the "Analyse" button.
  4. When the analyse is finished, you will see a list of messages with rule violations. Click on a list item to see the details in the "Properties" tab.
  5. (In the "Rules" tab you can disable certain rules.)

Note that FxCop could copy the right SuppressMessageAttribute string to the clipboard. So you won't have to write the attributes yourself to suppress a certain message. Just paste them at the right place. To do so, open the context menu for a message in the list and click "Copy As" -> "SuppressMessage".
Don't forget to compile your project with the symbol "CODE_ANALYSIS" to suppress messages.

I've added the FxCop project to the sources of my project. Feel free to use it as a starter or copy some parts from the FxCop Xml-Project-File.

Discussion
So what do you thing about FxCop?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

Yeah it's a great polishing tool; I wish I would use it from the beginning of my projects instead of as a polish-step afterwards :)

Also the code coverage feature is really nice to find unused code in applications (not so useful in API development).

the Fiddler's picture

I do run it on OpenTK to keep an eye on code status. 11K warnings may look bad, but most stem from the OpenGL bindings (e.g. missing doc strings) and can be safely ignored. Once you disable these the rest provide useful information, like the wrong ArgumentException.

I wish I could use the same program at my job... there is a staggering amount of bad (hand-crafted) code.

georgwaechter's picture

there is also an open source alternative of fxcop namely Gendarme ... http://www.mono-project.com/Gendarme

it is not finished, but yet a very cool tool

teichgraf's picture

@georg:
Thanks for the info. I'll give it a try.