16

I like StyleCop's static code analysis and rules enforcement. However, it is severely lacking in several key departments.

  • Adding new rules isn't officially supported and from what I hear pretty difficult.
  • Automatic "fixing" of trivial rules violations would be nice! Perhaps not with variable naming but with method ordering (static, etc) this would be a huge time saver.
  • Microsofts "one-size-fits-all" approach to StyleCop is kind of restrictive. I would like to have a custom set of rules for our in-house standards.

Is there such a commercial product out there?

alt text http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/dominicz/WindowsLiveWriter/MicrosoftStyleCopSourcecodeanalysisforfo_D8EF/styleCopErrors_6.gif

mmcdole
  • 91,488
  • 60
  • 186
  • 222
  • There's a project called StyleFix that will attempt to fix some of the errors automatically. It's poorly documented and only works on VS2008. I've taken the source and modified it to integrate StyleCop directly into the program and do the fixes. (And compiled it in VS2005) – Brad Bruce Jul 02 '09 at 02:45
  • CodeIt.Right, CodeMaid to name two. – nawfal Jun 06 '15 at 09:38

7 Answers7

12

Adding rules is, or is going to be, officially supported:

As promised, we will also be releasing SDK documentation for StyleCop explaining how to author custom rules and how to integrate the tool into custom build environments. The SDK documentation is currently under final review and we hope to release it very soon. -- JasonAll

In terms of our "in-house" style, I got pretty close by disabling a handful of StyleCop rules:

  • File Headers (SA1633-SA-1640)
  • Code ordering (SA1200-SA1202)
  • Requiring "this" (SA1101)

You can do this globally by modifying the Settings.StyleCop file in the installation directory, though I've taken the approach of putting one at the root of our source tree in each project.

The end effect is much what we want. There are a handful of "in-house" choices that would be nice to flag, but even without them StyleCop is delivering a lot of value for us.

Bevan
  • 43,618
  • 10
  • 81
  • 133
  • Those are the same rules that I currently have disabled as well. Overall I really like it. It would just be nice if Microsoft made StyleCop a little more polished. Permanent integration into Visual Studio makes sense to me with something like this. – mmcdole Nov 14 '08 at 09:23
  • The one thing that really frustrates me is the lack of commandline support, making it hard to integrate StyleCop into my builds. I ended up writing my own front end (http://www.nichesoftware.co.nz/content/stylecop-cmd) – Bevan Nov 16 '08 at 05:46
6

StyleCop for ReSharper could help (you'd need to purchase ReSharper, but the plug-in is free):

StyleCop for ReSharper is now feature complete in that is has reached feature parity with StyleCop 4.3.

There are 148 StyleCop rules.

  • 38 of these must be fixed manually (normally because you have to type descriptive text or rename variables).
  • Of the remaining 110 rules 58 are fixed by R# Code Cleanup (silent mode).
  • Of the 52 now remaining we have Code Cleanup rules that fix all of them automatically.

We also provide 106 Quick Fixes that provide in place context menu fixes for violations for the 110 rules that can be fixed automatically

We also ship a "StyleCop friendly ReSharper Code Style Sharing Settings file" which configures ReSharper to automatically format code in a StyleCop friendly manner.

5

There's Gendarme from Mono, although it's Open Source, not commercial.

skolima
  • 31,963
  • 27
  • 115
  • 151
3

Agent Smith is very nice. It requires Resharper.

dav_i
  • 27,509
  • 17
  • 104
  • 136
orip
  • 73,323
  • 21
  • 116
  • 148
3

An alternative or a good complement to StyleCop would be to use the commercial tool NDepend. With this tool one can write Code Rule over LINQ Queries (namely CQLinq). Disclaimer: I am one of the developers of the tool

More than 200 code rules are proposed by default, these include design, architecture, code quality, code evolution, naming conventions, dead code, .NET Fx usage...

CQLinq is dedicated to write code rules that can be verified live in Visual Studio, or that can be verified during build process and reported in an HTML/javascript report.

The strength of CQLinq over StyleCop or even FxCop, is that it is straightforward to write a code rule, and get immediately results. Facilities are proposed to browse matched code elements. Concretely this looks like that:

CQLinq code rule

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
2

I often write unit tests to reflect over my types and check for violations of my custom rules.

Here's an example for verifying that certain types are immutable: http://blogs.msdn.com/kevinpilchbisson/archive/2007/11/20/enforcing-immutability-in-code.aspx

Here's another, for rules about unit test naming: http://jbazuzicode.blogspot.com/2008/11/keeping-test-fixture-and-class-names.html

EDIT: The 2nd link seems to be lost to the sands of time.

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
2

Take a look at CodeIt.Right from Submain.

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110