views:

683

answers:

4

After going through the Appendix A, "C# Coding Style Conventions" of the great book "Framework Design Guidelines" (2nd edition from November 2008), I am quite confused as to what coding style is Microsoft using internally / recommending.

The blog entry A Brief History Of C# Style claims:

In fact, the differences between the "StyleCop style" and the "Framework Design Guidelines style" are relatively minor

As I see it, the differences are quite pronounced. StyleCop says opening brace should be on a separate line, Framework Design Guidelines say it should be after the opening statement. StyleCop says all keywords are to be followed by a space, Framework Design Guidelines say 'get rid of all spaces' (even around binary operators).

I find this rule from the Framework Design Guidelines book especially ironic (page 366, 6th rule from the top):

Do not use spaces before flow control statements

Right: while(x==y)
Wrong: while (x == y)

This is explicitely stating that the StyleCop style is wrong (space after the while keyword, spaces before and after the equality binary operator).

In the end, code formatted using the StyleCop style has quite a different "feel" from the one formatted using the Framework Design Guidelines style. By following the Framework Design Guidelines style, one would have to disable a bunch of the rules (AND there are no rules that check adherence to the Framework Design Guidelines style...).

Could somebody (MSFT insiders perhaps?) shed some light on this divergence?

How is your team dealing with this? Following StyleCop? Framework Design Guidelines? Ignoring style altogether? Baking your own style?

+1  A: 

You make a decision. If you like some parts of one and some parts of the other, write your own style guide. If you like one better than the other one, pick it.

The essential thing to do is to pick a style; there's no way to evaluate one over another in any rigorous quantitative way.

Charlie Martin
+1  A: 

We use StyleCop for all our code, and apart from a few minor niggles, most of its standards I feel lead to the most readable code. A lot of its standards have been heavily discussed within Microsoft, and have had feedback from the community, and while it isn't expected that everyone will agree with everything, it's probably about the best 'standard' there is (particularly as it allows automatic validation, and automatic correction with the StyleCop for ReSharper plugin).

If there are any things you strongly disagree with, Jason Allor who maintains the tool is quite open to suggestions around certain things, for example with auto-properties StyleCop originally insisted on...

public int Prop
{
    get;
    set;
}

...but we raised a change request to permit single line properties (i.e. everything on one line) as it's no less readable and takes up less space. He made this change within a few days.

Greg Beech
+4  A: 

On a blog I read about this (I can't seem to find the url) it was stated: the framework guidelines are based and evolved from C++ guideliness (they are all seasoned C++ developers) while the guidelines stylecop provides are more modern new C# only guideliness... Both are fine, make a decision yourself... I personally use the StyleCop ones

Cohen
+3  A: 

This article by the stylecop team explains exactly what you're asking I think. http://blogs.msdn.com/sourceanalysis/archive/2008/05/25/a-difference-of-style.aspx

And to answer the second part of your question, our team just started using StyleCop using all rules (some people pick and choose which ones to use). The only thing I don't like is the extra time it takes, but using a tool like StyleCopForResharper makes it go a lot faster. I used to get really annoyed when people wrote code that looked differently than I would have written it, but now that we use StyleCop, everyone's code looks identical. No more biting your lip about annoying things people do

jayrdub