Running FxCop and the quirks

Aug 16, 2007

Earlier today I did a code maintenance session on BlogEngine.NET using the excellent FxCop tool. It was a while since I’ve did so there were quite a few things to correct in the code. I actually like spending time with FxCop and fixing the broken rules. There’s something very relaxing about it. Even though FxCop is excellent, it is not without its quirks.

Compound words should be cased correctly

Based on this rule, FxCop told me to correct the spelling of Whitespace to WhiteSpace. I didn’t think more about it and just accepted that whitespace probably isn’t one word but two. After the change I ran FxCop again and, lo and behold, it suggested that I should change WhiteSpace back to Whitespace. Hmm, thought I just did the opposite because it broke the rule.

Avoid namespaces with few types

This rule makes sense if you only have a single class in a namespace, but it breaks if you have fewer than 5 types. Now here is the thing. I have a namespace with only four types and I want it that way. Stop telling me that I shouldn’t!

Do not pass literals as localized parameters

This rule is a globalization rule that breaks if you write the content of a string manually in the source files instead of putting them in resource files. When I throw some exceptions or construct some strings that I know will not be localized then I couldn’t care less about this rule. If I were to put every string in a resource file, it would be hard to work with. I turned the rule off.

Do not declare static members on generic types

In English: Don’t write a static method or property that uses Generics in its declaration. The rule also states that you must never exclude that rule no matter what. It states that in order to call that method you need to write bad code such as

someObject.GenericMethod<int>();

Well, that is not correct. You can have a static member using Generics without calling it with the <type> syntax if you know how to write the members correctly. Apparently it doesn’t take smart people into account for this one.

Conclusion

Even though this post sounds a bit negative I’m still a big fan of FxCop. Some of these rules are perfectly fine to break according for some scenarios. The rules state that them selves. These few ones have just annoyed me ever since I started using FxCop a couple of years ago.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (5) -

Thom
Thom United Kingdom
8/16/2007 9:04:01 PM #

Why not just tell FxCop to ignore the rules that annoy you? Smile

www.gotdotnet.com/.../FxCopApp_Rules.html

Mads Kristensen
Mads Kristensen Denmark
8/16/2007 9:19:33 PM #

That's what I end up doing, but some of the things the annoying rules find are valid enough. It's just not good enough to pick the valid from the invalid ones when it runs the rules.

Dan Atkinson
Dan Atkinson United Kingdom
8/17/2007 8:34:14 AM #

I guess that you just have to pick a happy medium between your own personal coding style and FxCop's somewhat-maddening contradictions.

I used to use FxCop about 3-4 years ago, but stopped using it very quickly after it began contradicting itself. I haven't really used it since, and ended up relying on general best practice to guide my way, rather than this.

David Kean
David Kean United States
8/18/2007 3:35:10 AM #

Mads: Thanks for the feedback. The whitespace (or should I say white space) issue has been fixed (and the rule changed so that this kind of problem can never happen again). With regards to the static members on generic types, it's basically stating that the compiler can never infer the type parameter on the class, forcing the user to always specify a type parameter.

I've added an example in the community content block that hopefully explains it a little better: msdn2.microsoft.com/.../ms182139(VS.80).aspx

Dan: I'm sorry to hear that you no longer using FxCop. Free feel to head over the FxCop forum (forums.microsoft.com/.../ShowForum.aspx) and provide us some feedback as to where you think we're going wrong.

Regards

David

Regards

David

Dan Atkinson
Dan Atkinson United Kingdom
8/19/2007 12:55:43 AM #

David,

It's great to see the developers have a look at feedback and react to it. I did leave feedback a few years ago on a forum for FxCop. However, it seems that the oldest topics are only 2005, so I can't dredge up the response I got from it (which would be typical if it was on Usenet). The help wasn't great at the time, and I wasn't a developer in any real sense, and the experience put me right off.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.