Thursday, April 9, 2009

FXCop experiences

What is FxCop? FxCop is a static analysis tool, written by Microsoft to inspect if some classes of mistakes may behave wrong in your application. They use IL intermediate assembly code representation and report regardless of .NET language.

Most of principles are really right to follow like: if you setup a default to a variable to zero in one class, you will get notified by FxCop that is not needed.

What went wrong:
Polluting the warning box with a lot of errors that do not really fit, like:
- a namespace has too few classes in it, so unify it with other namespaces. Namespace is an important issue of C# as it cleans the language and naming. C# IDE-s, like Visual C#, SharpDevelop and MonoDevelop, creates namespaces based on the project root namespace and on your folders. So for cleaning reasons you may keep some classes out of context in other folders. I had no idea if I had a class namely Feature which creates some OpenCascade shapes (like Fillet and Extrude) to unify namespace with other classes
- using NaroCAD complains about CAD that is not properly spelled. If you look to XmlDocument is clear why: CAD contains only capitals . Changing in NaroCad makes it complain that you probably have hungarian notation. I had changed to Naro, and I remain with this error: Correct the spelling of 'Naro' in namespace name 'Naro.PartModeling.Modifiers'. (CA1704). My question is: how should everything be spelled?

- Circular errors: Set methods are purposed to be made as properties (seems to fit natural as properties have get and set "methods"). Making a write-only property it will complain that you did not make it a read property

This makes the whole replacement and warning to be a hunt around errors you want to fix and try to ignore the errors which are really not there.

Anyway, the most important small design problems are now fixed. It touches almost every file of the project, excluding to make NaroCad build on signed assemblies, which imparts pretty random behavior and not using internationalization (a stronger complaint of FxCop), which in case of using it makes it impossible to find resources and to complain that all other assemblies are impossible to be loaded cause they are not marked with NeutralCulture attribute. (see here some details)


This was reported by Gendarme (I had used it for the most important errors at least) and the message was: Parent use the old not notation, try to change in Parenot.

Gendarme reports more errors, but the good thing is: every error has a confidence degree. Another good thing is that it found similar methods and warns you against copy/paste programming.
The bad thing is the same of FxCop problem: it really throws you a lot of problems (with a good degree with Confidence: Low) that are not there.

Which should you use? If you use Visual C# Express, use Gendarme, because FxCop is not integrated with C# Express, and Gendarme's output is good enough (a web page made by categories). If you use SharpDevelop, use FxCop as is an integrated part of error reporting, so most warnings can be located with one click. Visual Studio 2008 professional has FxCop included in it, so you will have to check your project "metrics" to see its problems.

Considering that most (important) small issues are fixed, I will go back to Naro... CAD to fix other logic issues and bug fixes.

No comments: