OfficeClip Forums

OfficeClip Blog
Discussions on OfficeClip Products and Services

StyleFix - Automatically fix StyleCop violations

by Admin 16. February 2009 14:02

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: http://www.codeplex.com/StyleFix

The source code is at: http://www.codeplex.com/StyleFix/SourceControl/ListDownloadableCommits.aspx

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 ***

Tags:

Developer Resources | OfficeClip Products

Comments (9) -

Howard van Rooijen
2/23/2009 10:02:15 PM #

Hi,

You could also use StyleCop for ReSharper (you'll need ReSharper installed tho) - we went feature complete last week and implement Quick Fixes and Code Clean Up Modules to fix the majority of the StyleCop violations.

See http://www.codeplex.com/StyleCopForReSharper for more details.

Regards,

Howard

admin
2/24/2009 4:02:55 AM #

Thanks, I looked at this a while back. It is a good commercial tool that does some stylecop fixes. I will probably need to look at it again.

We wanted something simple which would just fix the stylecop errors without doing anything more.

Howard van Rooijen
2/26/2009 4:02:17 PM #

If you haven't tried ReSharper I really urge you to - it's the single biggest productivity gain I've seen - we've rolled it out for all our developers and it's gone down very well. Although it's commercial it's really worth the value (I've had licenses since V2.0 and have always paid for them myself because I personally see the benefit). The cheapest option is the C# only version.

Again I urge you to give it a go - StyleCop for ReSharper is just one of the *free* plugins that are available - there are also Agent Smith and Agent Johnson which also have some very useful features. We also make a lot of use of the Gallio plugin which gives some neat TDD features.

The one thing I'd say about StyleFix is there are only a small number of StyleCop violations that can be fixed by string replacement and RegExp - some of the other violations are very tricky to solve and we had to use a lot of ReSharper's inbuilt features to fix them.

The other thing that's good about R# is that you can wrap all the fixes in transactions, which means they will roll back if they fail and also allows you to Undo your changes.

Regards,

Howard

admin
2/27/2009 5:02:12 AM #

Howard, Thanks for your comments

Luke
3/23/2009 5:03:34 AM #

Hi,

is there any documentation available for StyleFix?

It seems to run ok, but does not change anything in the code.

admin
3/23/2009 3:03:27 PM #

Hi!

Can you try this:

Select the files and then click on Enable button at the bottom and then click on "Run stylefix" button.

I think there is a bug which I need to fix.

There is no other documentation but the source code is available in codeplex.

Mitesh
3/30/2009 12:03:18 PM #

HI,

I also tried the above mentioned steps i.e. Click on Enable button and then Click on Run StyleFix button. But the tool is not working Frown
I am using VSTS 2008 and StyleCop 4.3.

Regards,
Mitesh

admin
3/30/2009 10:03:22 PM #

Can you post a sample project (zip file) in the codeplex issue tracker so that I can take a look

Thanks

Nandita
6/18/2009 11:06:34 PM #

Hi,

I have the same problem. It seems to run but doesnot change the file. I have notice the same with the Ghostdoc.

Any idea?

Thanks,
Nandita

Comments are closed

Powered by BlogEngine.NET