Santosh Benjamin's Weblog

Adventures with AppFabric, BizTalk & Clouds

StyleCop – first impressions

leave a comment »


A friend alerted me to the fact that StyleCop (formally named Microsoft Source Analysis for C#) is now available in the public domain and can be downloaded from the MSDN Code Gallery . Its been used as an internal tool in Microsoft for a long time.

It differs from FxCop which is targeted at compiled assemblies while this looks at source code and is only aimed at C#. Information on the tool can be found here and the comments are quite interesting. It looks like a controversial tool (for example, the preference for spaces over tabs) so now we are going to have more layout wars. 

This is delivered as a VS Package so when you install it, it automatically gets loaded into the VS 2005 Tools Menu under the caption – Run Source Analysis. There is a SourceAnalysisSettingsEditor application in the Program Files directory. No shortcuts are available on the desktop.

There is a gotcha when you try to launch the editor application from that folder directly. It will immediately throw up an error message saying “The command line arguments are incorrect. Include a path to the Source Analysis Settings file.

To overcome this simply drag the “Settings.SourceAnalysis” file onto the application and it will open up correctly.  Alternatively, you can open up a command prompt at the folder and enter “SourceSettingsAnalysisEditor.exe Settings.SourceAnalysis” to launch the application. The third method, which i guess is the way it was intended to be used (as it is a VSPackage) is to simply right click on the Project and choose the Source Analysis Settings option.

Ultimately, however you choose to launch it, the main screen looks like this

StyleCop_UI

I ran it on a small application and it immediately threw up at least a hundred errors – a bit like FxCop – very daunting. The interesting thing about the settings file is that there is hardly anything there. Its an XML file (arent they all?) and i was expecting to see all the options that had been selected in the application showing up there. Actually the application launches with all the default settings and once you unselect or select one of them , the settings file immediately gets updated. However we are not expected to play around with this that much. According to the blurb on the MSDN post

The ultimate goal of Source Analysis is to allow you to produce elegant, consistent code that your team members and others who view your code will find highly readable. In order to accomplish this, Source Analysis does not allow its rules to be very configurable. Source Analysis takes a one-size-fits-all approach to code style, layout, and readability rules. 

Lone Ranger developers with strong opinions on code layout (esp if they differ from these) will most probably be up in arms at some of this, but the tool is aimed at team use and when you need consistency in a big code base, tools such as these are a definite asset (of course, in some teams you gotta be prepared to start arguing about the number of spaces /tabs etc 🙂 .. sigh!! you cant win ’em all )

It is also not an extensible tool and so you cannot add your own rules to it. FxCop does allow that, but then you need a stout heart to even attempt writing custom rules for FxCop so i dont think the absence of a facility for defining custom rules is that much of a downside (in a free tool , that is.. commercial tools are a different story).

There’s also a couple of good posts on the MSDN blog documenting the following items in more detail.

There is also an MSBuild task available for this in the Microsoft.Sdc.Tasks library (Microsoft.Sdc.Tasks.Tools namespace) and the CHM file for the library indicates that the task has the following parameters

<Tools.StyleCop FullAnalysis=”fullAnalysis”
                            PathToAnalyze=”pathToAnalyze”
                            ViolationsFile=”violationsFile”
                            UseVSBuildFiles=”useVSBuildFiles”
                            FileToAnalyze=”fileToAnalyze”
                            OptionsFile=”optionsFile”
                            UseCoreXTFiles=”useCoreXTFiles”>
                <Output TaskParameter=”TotalViolations” ItemName=”itemName” />
            </Tools.StyleCop>

So, its nice to have this and i’m sure that now its in the open, there will be some enhancements made in due course. Perhaps someone will work out a way to plugin new rules (maybe using extension methods!!) into the existing code base or maybe we’ll get a new version if it starts getting popular. Check it out and see if it helps your team. 

 

 

 

Advertisements

Written by santoshbenjamin

May 31, 2008 at 2:20 PM

Posted in Coding, General

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: