StyleFix – Automatically fix StyleCop violations

Reading Time: 2 minutes

Microsoft StyleCop reports style violation in C# code. It helps standardizing codes throughout the enterprise and also makes reading, sharing and maintaining code much easier.

The only problem is that it reports more than 100 types of violations and so the number of violations that you see in a typical project may run into the thousands. Some of them may be easy to fix in the visual studio using macros and regular expressions but most of them take a lot of time and are hence not practical at all.

Microsoft blog suggests that the next release of the StyleCop will be automatically able to fix the violations. This is a welcome improvement! In the meantime let me give you a stop-gap approach that we are using here.

The following tools are useful:

  1. Ghostdoc – A automated documenting tool implemented as macros. Although I believe that automated (and syntactical) documenting defeats the purpose of documentation, this tool is a GEM!. You need to use it to see what I mean.
  2. NArrange – An open source project that does some things like rearranging the code to fix some stylecop violations of the code. However, it does not touch the majority of the violations.
  3. StyleFix – This is what we have written here to use it on our own code. It does a few things:
    1. Allows you to selectively choose files that will be processed by StyleCop.
    2. Automatically fixes many violations. This program is new and will be improved till anything better comes up on the horizon.

 

I am using StyleFix with some success. For example, for a small project with 8 files, the first time I ran with StyleCop, I got 1753 violations!!! After running StyleFix it reduced to 273 or so. Applying GhostDoc reduced it to 92. The rest of it, I had to do manually 🙁

The executable is at: https://www.codeplex.com/StyleFix

The source code is at: https://www.codeplex.com/StyleFix/SourceControl/ListDownloadableCommits.html

If you are using it I would like to know what you think. If you want to contribute, let me know I will gladly share the svn access. *** This program is still in beta, so please make appropriate backups before using it. I am not responsible if it screws up your code ***