I've decided to start running StyleCop on a medium sized project and am getting over 1000 warnings.
Is there a quick easy way to fix most of these warnings? Most of the warnings could easily have been automated to resolve and would save me a lot of time.
This doesn't exactly "automatically fix anything" but there is a StyleCop plug-in for Resharper on Codeplex that will automatically highlight the style errors in your code.
Update: According to the StyleCop for ReSharper website, ReSharper now provides an increasing number of Quick-Fixes for StyleCop errors. You select a quick-fix from the list and the problem will be resolved automatically.
No, frustratingly. That's why Style Cop is counter-productive. Consistent formatting looks nice, but there's little productivity benefit to it (what really makes code hard to read is length and indirection). Perfect spacing is not worth spending hours adjusting lines by hand.
Of course, if there were an automated tool to achieve consistent formatting, I'd hook it up post-check-in, and let it help me. But Style Cop is made by sadists. Presumably their conversation went like this:
Bob: Using pattern matching, the software identifies ugly code
Charlie: —and fixes it!
Bob: No, it prints a message about each transgression
Charlie: —and then the user right-clicks to fix them all?
Bob: No, it scolds them 'invalid spacing around the comma'
Charlie: —and then explains how to fix it?
Bob: No, that's in a separate document
Charlie: —on Google?
Bob: No.
I hate Style Cop. It's below a minimum viable product.
StyleCopFixer automatically fixes StyleCop exceptions within Visual Studio IDE for C# Only.
Errors currently catered for are:
- SA1001
- SA1516
- SA1513
- SA1508
- SA1507
- SA1505
- SA1309
- SA1210
- SA1202
- SA1200
- SA1106
- SA1101
- SA1025
- SA1013
- SA1012
- SA1009
- SA1008
- SA1633
- SA1003
- SA1005
- SA1600 (for properties only)
- SA1121
There is a project in codeplex which does some automatic fix based on stylecop warning. It is not complete however...
see: http://stylefix.codeplex.com/
Code Maid does some Stylecop fixes.
Unfortunately, right now, I don't know of any automatic tools to fix things entirely.
However, if you setup Visual Studio's formatting options (and Resharper's, if you have that) for code formatting to match the style cop guidelines, cleanups get a bit easier.
Once you've done that, you can go into a file and do Edit->Advanced->Format Document (or the keyboard shortcut, which varies with your shortcut settings - mine is Ctrl+K, Ctrl+D). This will fix quite a few of the style cop warnings, especially if you have resharper + the stylecop plugin. It won't fix all of them, but in many cases, it's taken the number of warnings in a file from hundreds down to tens.
If you have Resharper you can download a StyleCop plugin for Resharper which features some automatic fixes.
StyleCop for Resharper on codeplex.
- Turn off the warnings! Not recommended, but you could use this in the short term until you get a bulk of the issues resolved.
- Write a program to fix some of the simpler warnings, or use find/replace
- Use code snippets to help with things like file header documentation
There was mention on the StyleCop blog regarding implementing auto fixing for rules
Also check out the entry on the blog regarding integrating in to an existing large project.
Note: It seems that StyleCop only returns around 1000 warnings, so you will continue to get this number even after fixing a heap.