Sunday, July 20, 2008

StyleCop confusion cleared

As Brian Harry and Jason Allor blog about how easy series of misunderstandings may give birth to conspiration theories, I thought I’d drive some of the points they raise home and talk about what’s common and what’s different between FxCop (and Team System Static Code Analysis at that) and StyleCop.

  1. FxCop runs on .NET binaries (thus your code must pass compilation) and does not require source code; StyleCop runs solely on C# source files. Thus while FxCop essentially works for any .NET assembly, StyleCop is limited to C# source files (and essentially, this files may not even compile)
  2. Team System Static Code Analysis is integrated into Visual Studio project properties, has standard check-in policy and analysis results can be published to TFS data warehouse and as such are reportable. StyleCop is out-of-band tool with limited integration to Visual Studio (very similar to FxCop stand-alone executable)
  3. FxCop supports analysis suppression attributes (on element or module level); StyleCop does not.
  4. Team System Static Code Analysis product is actively developed as part of next version of Visual Studio (“Rosario”); StyleCop most probably will always be unsupported tool with limited dedicated development
  5. And finally, the biggest difference – while StyleCop verifies source code and documentation formatting for C#, FxCop (and Team System Static Code Analysis) is the tool checking the binaries for conformance to design and development guidelines [while FxCop includes Naming rules, it is just part of its rules inventory].

The only thing one can expect from StyleCop analysis is more maintainable code (since source code becomes more uniform and compliant to coding standards). From using FxCop analysis one may expect actual improvement of the code by elimination of design/development flaws as well as making code compliant to common naming conventions.

The overlap between two tools is minimal – here is the table of StyleCop rules that also exist in FxCop:

Rule ID Rule Name
SA1401 FieldsMustBePrivate
SA1300 ElementMustBeginWithUpperCaseLetter
SA1301 ElementMustBeginWithUpperCaseLetter
SA1302 InterfaceNamesMustBeginWithI
SA1303 ConstFieldNamesMustBeginWithUpperCaseLetter
SA1304 NonPrivateReadOnlyFieldsMustBeginWithUpperCaseLetter
SA1305 FieldNamesMustNotUseHungarianNotation
SA1306 FieldNamesMustBeginWithLowerCaseLetter
SA1307 AccessibleFieldsMustBeginWithUpperCaseLetter
SA1308 VariableNamesMustNotBePrefixed
SA1309 FieldNameMustNotBeginWithUnderscore
SA1310 FieldNamesMustNotContainUnderscore

Only other rules that are code-related rather than formatting-related are the following rules:

Rule ID Rule Name
SA1404 CodeAnalysisSuppressionMustHaveJustification
SA1405 DebugAssertMustProvideMessageText
SA1406 DebugFailMustProvideMessageText

The rest of StyleCop rules deal with C# source code formatting or code comments.

To conclude, FxCop is more about telling you how you ought to do things, while StyleCop is all about how you should format the source code for the things, no matter what is in that source code.

Myself (while I like arguing about code formatting) FxCop is unquestionably has more value. How about you? Does your “mileage” vary?

No comments: