I'm always looking for new tools to add to my developer's toolbox. One such tool I recently came across is StyleCop for ReSharper.
StyleCop for ReSharper is a ReSharper add-on that enables real-time syntax highlighting of StyleCop rules. Normally, you run StyleCop by selecting "Run StyleCop" from the Tools menu:
The StyleCop for ReSharper takes it one step further by providing the same analysis, but in real-time and with squigglies:
The first thing you'll notice is that every single line has been flagged for violations! That's kind of ridiculous. Fortunately, there's a quick way to reduce that count by loosening up the rules a bit.
Prerequisites
Of course, to use the StyleCop for ReSharper add-on you'll need to have ReSharper. Also, if you attempt to install the add-on without first having installed StyleCop, you'll see this dialog:
The latest version of StyleCop can be downloaded here.
Loosening up StyleCop's Rules
Before we start modifying the default rules, let's learn how to share a single StyleCop settings file across multiple solutions or projects.
StyleCop starts looking for a Settings.StyleCop rules or settings file in the project's local folder first. Regardless of whether it finds one or not, it works up the folder tree, looking for other copies of the file. Once it reaches the top of the chain, it goes back to the default install folder where there should always be a the global version of the file. It is possible to override the global settings by placing a copy of Settings.StyleCop in the local solution or project file. Also, you can have global settings which are overridden by solution-level settings which in turn might be overridden by project-level settings. All depends on how granular you want to get with your rules.
Personally, I only maintain a single Settings.StyleCop file for all of my projects and solutions by putting the file at the development root (d:\dev).
You'll find the global copy of Settings.StyleCop here:
Copy that into your development tree, then double-click on it. That will bring up the StyleCop Project Settings editor:
If you expand the "Enabled rules" nodes you'll see all sorts of rules that you can turn off.
Rules I Can Live Without
Here is the list of rules that I operate without. I'll add to this list as I get further into StyleCop's rule set. In general, I'm trying to adhere to the rules as given, but some of them are just a bit too nitpicky for my tastes.
| StyleCop Rule |
Definition |
Why I Turned It Off |
| SA1000 |
KeywordsMustBeSpacedCorrectly |
This rule is telling me my choice of spacing around, for example, the keyword 'typeof' is not right. "Not right", in this case, is in conflict to my standard formatting rules, so this one is out. |
| SA1008 |
OpeningParenthesisMustBeSpacedCorrectly |
This rule seems to dictate no spaces between, say, a method name and an opening parenthesis. I like have a single space between such things, so this one stays off. |
| SA1027 |
TabsMustNotBeUsed |
Ah, yeah. I don't like spaces. I use tabs. |
| SA1101 |
PrefixLocalCallsWithThis |
In my mind, the "this" keyword is implied by its absence, and I generally do not use it unless there is some naming conflict present otherwise. |
| SA1117 |
ParametersMustBeOnSameLineOrSeparateLines |
|
| SA1309 |
FieldNamesMustNotBeginWithUnderscore |
This is in exact conflict with the style guide I use, which advocates using an underscore as the prefix on private variables. |
| SA1502 |
ElementMustNotBeOnSingleLine |
There are times, for example for a getter/setter property definition, where you might want everyting on one line, including the curly braces. This ones gets turned off.
|
| SA1503 |
CurlyBracketsMustNotBeOmitted |
I think this goes a bit too far. There are times where, for readability if nothing else, it's OK to omit the curly braces. |
| SA1512 |
SingleLineCommentMustNotBeFollowedByBlankLine |
Another one that is a bit too nitpicky. I'm happy just having comments in the code. Who cares about blank lines? |
| SA1515 |
SingleLineCommentMustBePrecededByBlankLine |
Same reasoning as SA1512. |
Rules Off By Default That I Use
The other side of turning off default rules is turning on some that are off by default. Here are those:
| StyleCop Rule |
Definition |
Why I Turned It Off |
| SA1124 |
DoNotUseRegions |
I hate regions. Nuff' said. |
Conclusion
If we take a look at the code snippet from above we'll see that the squiggly-indicated violations have gone way down:
Still have some work to do, but at least it's a bit more manageable and realistic.