FxCop, oh where have you been all my life?

So recently I’ve come across a new extension for Visual Studio 2010 called Visual Studio Achievements, which is a fun little extension that just made me chuckle a little, especially considering it isn’t anything that will make me more productive.  Or did it? Discovering this little gem did introduce me to FxCop! Can’t believe that I’ve been tooling around in the .NET world and never heard of this little tool.  Because I’m feeling particularly lazy today, here’s a “what this is about” snip right from the msdn page.

“FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements. Many of the issues concern violations of the programming and design rules set forth in the Design Guidelines, which are the Microsoft guidelines for writing robust and easily maintainable code by using the .NET Framework.”

I’ve already taken a spin with it in Visual Studio 2010 on a couple of my projects and found little things that I never considered when writing my code.  Little things that I’ve just always done a certain way and never really had the time to understand the improvements which have come over the years.

One of them is the time old tradition of checking if a string contains a value other than blank or null.  Back since my college days of learning C++, I’ve always tackled this one by using the following if statement or something similar.

if(name == null || name == ""){
    //do something

Well FxCop will let you know of a more performant way of performing that check.  Many of you probably have seen this hundreds of times.  And when I saw it I immediately had one of those “Oh yeah! Now I remember!” moments.

if (String.IsNullOrEmpty(name)){
    //do something

For me this was one of those little gems which I just never got into the habit of doing. I’d always fall back on my “tried and true” methods.

Of course, FxCop will catch more than just little things like above. It also aids you in being more mindful of Globalization, design standards, and even potential insecure parts of your code. I’m already thankful for the Globalization information I received.  Globalization was always one of those things added when it was requested.  It was never applied until we physically go in to add it.  Or is it?  The .NET framework takes Globalization into consideration by default.  So methods like toString(), SubString(), IndexOf() do consider the local culture info when performing their tasks.  Which is nifty for display, but might break your application if something like IndexOf() on internally stored data suddenly is working with a view to a different cultural setting in the operating system.  We all know what troubles case sensitivity can introduce.

I strongly suggest giving it a look for your .NET projects. Oh and one final note. Don’t forget, FxCop works right off of your compiled code, so after you make changes, be sure to recompile for those FxCop suggestions to disappear.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.