Good cop, Bad cop, Stylecop?

So what do I mean with this title?

….I have no clue at all  ;-)

Anyway, a few days ago Andreas Håkansson told me to check out MS StyleCop.
StyleCop is a tool that ensures that you follow a certain code standard, such as naming conventions and general code structure.

Andreas also directed me to a nice Resharper plugin on CodePlex: http://www.codeplex.com/StyleCopForReSharper

This plug in allows you to see the StyleCop warnings directly in your code.

Confident that I was writing at least somewhat decent code, I thought; “OK, I wonder if StyleCop will find any problems with my code?”

And yes, apparently it could do that..

horriblecode

It was a bit depressing to see how badly my code sucked, but I still decided to make my code conform to the standard that MS uses.
Most of the problems in there are quite easy to fix, but even if they are easy to fix, its a pain to add things like “this.” in front of all class members in an entire code base.

Luckily, I found this blog post today: http://blogs.telerik.com/blogs/08-12-05/How_to_minimize_StyleCop_violations_with_ReSharper_reformat_code_feature.aspx 
Jordan Dimitrov at Telerik shows how to make a Resharper settings file that enables Resharper to format code the way StyleCop wants.

So by using these tools together, you can now use the Resharper “Cleanup code” and format the entire code base to be almost StyleCop compatible.
There are a few things that you still need to do manually, e.g. add comments and add parenthesis for operator priority inside expressions.

So if you own a Resharper 4.1 license, I think you should try these tools together, it’s an awesome way to get a code base that completely follows a style guideline.
Both StyleCop and the add in are free.

//Roger

10 thoughts on “Good cop, Bad cop, Stylecop?”

  1. You can also disable some rules in Stylecop (or FxCop), as most of them are really retarded.

    E.g. using ‘this’, it’s not Stylecop who decides that for me. :) Or ‘_’ prefixes for member variables. I WANT them to have that prefix.

    In the old days, we used ‘lint’ for C, which managed to spit out hundreds of errors of similar flavor as stylecop, e.g. that one didn’t store the # of characters returned from printf etc.

    There are a few good rules in stylecop, but most of them are really not that useful. Like ‘placing the usings inside the namespace’. Why?

  2. I could live with any sort of standard, as long as they are standard :-)

    I do think the StyleCop looks really funky in some aspects, but knowing that the code will be conforming to the same stlye as the MS code base and alot of other companies makes up for this IMO.

  3. There are a few exceptions that we simply made to the style cop settings. I think we put the using statements outside the namespace, we like _ on member variables, and I use ALL_CAPS for constants still, which is not legit by default in style cop.

    I think it increases code quality simply by achieving consistency among all developers, etc.

    I do wish I could get resharper to put the “Gets a value indicating whether…” in the comments for me. I guess a snippet will have to do for now.

    Jesse Foster | jf26028

  4. If you’re using StyleCop for ReSharper – there is a configuration file available with the download that configures ReSharper to be as compliant to StyleCop as possible. The you can just use the R# Quick Fixes and Code Clean-Up options to fix about 90% of the violations automatically.

    /H

  5. Not a choice for me. To have a tool that tells you what is is wrong with your code, but at same time forces you to introduce some illegibility (verbose XML Comments for self documented things, unnecessary curly brackets, augmented code density by use of “this” prefix). The majority of StyleCop rules are not driven by good practices and, by experience, programmers tend to lose a valuable “self defensive” attribute when use this tool.
    Then, over this tool (StyleCop), to put another (the valuable R#) to auto-fix what you are doing supposedly bad, is to finally lose all your programmer capacity to judge your (and others) code… OMG. Please RPG, return!!!
    Yes, I don’t like StyleCop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s