There's Gendarme from Mono, although it's Open Source, not commercial.
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
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.
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.
An alternative to StyleCop would be to use NDepend and its Code Query language (CQL). CQL is dedicated to write code quality rules that can be verified live in Visual Studio, or that can be verified during build process and reported in a HTML report. Here are a few samples of CQL rules (designed to be highly customizable):
Code refactored recently should be 100% covered by test:
WARN IF Count > 0 IN SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage < 100
Complex methods should be commented:
WARN IF Count > 0 IN SELECT METHODS WHERE CyclomaticComplexity > 15 AND PercentageComment < 10
I don’t want that my User Interface layer to depend directly on the DataBase layer:
WARN IF Count > 0 IN SELECT NAMESPACES WHERE IsDirectlyUsing "DataLayer" AND NameIs "UILayer"
Static fields should not be named m_XXX (Custom naming conventions):
WARN IF Count > 0 IN SELECT FIELDS WHERE NameLike "^m_" AND IsStatic
Methods out of MyAssembly and MyAssembly2 should not have more than 30 lines of code:
WARN IF Count > 0 IN SELECT METHODS OUT OF ASSEMBLIES "MyAssembly1", "MyAssembly2" WHERE NbLinesOfCode > 30
Public methods should not be removed to avoid API breaking changes:
WARN IF Count > 0 IN SELECT METHODS WHERE IsPublic AND IsInOlderBuild AND WasRemoved
Types tagged with the attribute MyNamespace.FullCoveredAttribute must be thoroughly covered by tests:
WARN IF Count > 0 IN SELECT TYPES WHERE HasAttribute "MyNamespace.FullCoveredAttribute" AND PercentageCoverage < 100