I’ve been thinking about the best way to start getting a consistent coding style with our team. Mostly I’ve worked on new projects and this is a relatively trivial thing to do by setting up StyleCop with an agreed set of rules, and hooking it up to analyse all source files as part of the Continuous Build. Along with a culture based on sarcasm and peer abuse, this usually works out fine.
However in this instance I’m working with a large legacy codebase. Even with the best intentions people won’t stick to a convention unless it gets enforced, and based on the amount of code that doesn’t comply it isn’t very simple to start enforcing the standard.
The only solution I could really see was to swallow the pain and go and reformat all of the existing code, then start enforcing analysis on the projects that have been updated.
I don’t think this would have even been possible if it hadn’t been for Resharper 5 and the StyleCop for Resharper plugin. I’ve become massively more productive since using Resharper a few weeks back and in this instance it’s been simply awesome.
Code Cleanup
Once Resharper has been configured with the StyleCop plugin so all coding style conventions are set up, you can run the code cleanup feature against a file or folder (Ctrl-Shift-Alt-F) which will reformat all existing code based on the configured rules. In my case this will typically reduce a file from around 300 violations to about 8. You can set up a profile to control exactly what does or does not get changed as part of this process.
Cycle through code issues (F12)
The next most useful feature is that StyleCop violations are treated as code issues by ReSharper. Hitting F12 repeatedly within a code file will jump to the next issue making it very simple to navigate around all violations and clean them up.
Violation highlighting within editor
The plugin will also highlight any style violation as you code, and provide tooltips and messages in the status bar to indicate what the violation was.
It will still take a while to finish updating the existing codebase, but I’m not even sure it would be feasible to attempt this without these tools.
