StyleCop - Microsoft Source Code Analysis for CSharp

by kevin 5/23/2008 11:07:00 PM

Microsoft released their source code analysis tool for C# today. It's called StyleCop, sort of like FxCop except for source code. This tool is available for download here. I've been getting some coding standards in place at SnagAJob.com and this tool comes at the perfect time. StyleCop works with both Visual Studio 2005 and 2008. After you install it, right-clicking on a file in the Solution Explorer or in the code editor will present a new option on the context menu entitled Run Source Analysis like this:

 

Clicking on the new option will run the source code analysis tool on the selected file(s) and produce a report in a new source analysis window that looks like this:

Notice that each StyleCop violation shown has a code beginning with the letters SA and followed by a numeric identifier. These codes defined by StyleCop rules are grouped in the following way:

  • Spacing rules - 1000 series
  • Readability rules - 1100 series 
  • Ordering rules - 1200 series 
  • Naming rules - 1300 series
  • Maintainability rules - 1400 series 
  • Layout rules - 1500 series 
  • Documentation rules - 1600 series

In the Source Analysis output window above, notice the error SA1027. That rule is not valid for me because, for my projects, I want to promote the use of tabs in source code whenever possible. So how can I turn this source code analysis error off? Well, in the installation folder for StyleCop, there is an XML file called Settings.SourceAnalysis. On my Longhorn installation, this can be found in D:\Program Files\Microsoft Source Analysis Tool for C#. On your system, that folder may be elsewhere. There is also an executable program named SourceAnalysisSettingsEditor.exe in the installation folder. If you run the settings editor, passing the name of the settings file as the first parameter, you'll see something like this:

Notice the seven items under C# in the tree on the left? Those are the seven rule groups I mentioned earlier. Since it's the rule for SA1027 that I want to disable, I simply open up the Spacing Rules node and locate that rule by its code. I need to uncheck the checkbox for that rule like this:

Clicking on the Apply button, the change is made to the XML settings file. Opening the XML file in Notepad, we can see the change documented this way:

From the XML, you can see that the Boolean property called Enabled has been set to False for the rule named TabsMustNotBeUsed within the SpacingRules. And running the source code analysis on my file(s) from Visual Studio again shows that the SA1027 error related to the use of tabs in the source code has been eliminated from the output. Nice. You can tune the master settings file for all the other rules to suit your needs in the same manner. I'll be blogging more about MSBuild integration and other cool stuff that StyleCop can do in the near future.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

C# | Software Development | StyleCop

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen


Kevin's on Twitter / FriendFeed

W. Kevin Hazzard Welcome to Kevin Hazzard's Blog. Kevin is a Software Architect, Professor and Microsoft MVP specializing in C#, WCF, Silverlight and IronPython.

View Kevin Hazzard's profile on LinkedIn
Microsoft MVP Award Foolish robot!

Calendar

<<  October 2008  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in