如何解决“StyleCop C# 风格”与“StyleCop C# 风格”之间的差异? 和“框架设计指南 C# 风格”?

发布于 2024-07-11 16:07:50 字数 934 浏览 9 评论 0原文

在阅读了《框架设计指南》(2008 年 11 月第 2 版)这本书的附录 A“C# 编码风格约定”后,我对 Microsoft 内部使用/推荐的编码风格感到非常困惑。

博客文章 C# 风格简史声明:

事实上,“StyleCop风格”和“Framework DesignGuidelines风格”之间的差异相对较小

在我看来,差异非常明显。 StyleCop 说左大括号应该在单独的一行上,框架设计指南说它应该在左括号之后。 StyleCop 说所有关键字后面都要跟一个空格,框架设计指南说“摆脱所有空格”(甚至在二元运算符周围)。

我发现《框架设计指南》书中的这条规则特别讽刺(第 366 页,从上往下数第 6 条规则):

不要在流程控制语句之前使用空格

右:while(x==y) 
  错误: while (x == y) 
  

这明确表明StyleCop风格是错误(while关键字之后的空格,相等二元运算符前后的空格)。

最后,使用 StyleCop 风格格式化的代码与使用框架设计指南风格格式化的代码具有完全不同的“感觉”。 通过遵循框架设计指南风格,人们将不得不禁用一堆规则(并且没有规则检查是否遵守框架设计指南风格......)。

有人(也许是微软内部人士?)能否解释一下这种分歧?

您的团队如何处理这个问题? 关注 StyleCop? 框架设计指南? 完全忽略风格? 烘焙自己的风格?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在夏天 2024-07-18 16:07:50

stylecop 团队的这篇文章准确地解释了我认为您所问的问题。
http://blogs.msdn.com /sourceanalysis/archive/2008/05/25/a-difference-of-style.aspx

为了回答您问题的第二部分,我们的团队刚刚开始使用 StyleCop 使用所有规则(有些人挑选哪个)的使用)。 我唯一不喜欢的是它需要额外的时间,但是使用像 StyleCopForResharper 这样的工具可以让它运行得更快。 以前,当人们编写的代码与我编写的代码看起来不同时,我会感到非常恼火,但现在我们使用 StyleCop,每个人的代码看起来都一样。 不再对人们所做的烦人的事情咬牙切齿

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

雪若未夕 2024-07-18 16:07:50

我在博客上读到了这一点(我似乎找不到网址),其中写道:
框架指南基于 C++ 指南并由其演变而来(他们都是经验丰富的 C++ 开发人员),而 stylecop 提供的指南是更现代的新 C# 指南...
两个都可以,自己决定吧……我个人用的是StyleCop的

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

花海 2024-07-18 16:07:50

我们所有的代码都使用 StyleCop,除了一些小问题之外,我认为它的大部分标准都导致了最具可读性的代码。 它的许多标准已经在 Microsoft 内部进行了深入讨论,并得到了社区的反馈,虽然并不期望每个人都会同意所有内容,但它可能是最好的“标准”(特别是因为它允许自动验证,以及使用 StyleCop for ReSharper 插件自动更正)。

如果有任何您强烈不同意的事情,维护该工具的 Jason Allor 对某些事情的建议非常开放,例如 StyleCop 最初坚持的自动属性......

public int Prop
{
    get;
    set;
}

但我们提出了更改请求以允许单个行属性(即一行上的所有内容),因为它的可读性并没有降低,并且占用的空间也更少。 他在几天内做出了这一改变。

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.

风向决定发型 2024-07-18 16:07:50

你做出决定。 如果您喜欢其中一个的某些部分和另一个的某些部分,请编写您自己的风格指南。 如果您比另一种更喜欢其中一种,请选择它。

最重要的是选择一种风格; 没有办法以任何严格的定量方式对彼此进行评估。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文