简体   繁体   English

如何处理冲突的编码约定?

[英]How to deal with Conflicting coding conventions?

Generally we use various static code analysis tools to analyze our code for validation.一般我们使用各种static代码分析工具来分析我们的代码进行验证。 But I've seen some conflicting scenarios.但是我看到了一些相互矛盾的场景。

As an example if we use class variables, the StyleCop will suggest us to use例如,如果我们使用 class 变量,StyleCop 会建议我们使用

this.Name = myName

instead of,代替,

Name = myName

But this will pop up a Resharper error, "Redundant qualifier" and will suggest to not to use "this."但这会弹出一个 Resharper 错误,“冗余限定符”,并建议不要使用“this”。 notation.符号。

So in such scenarios I need to check a more consistent reference to choose what is correct/Best.因此,在这种情况下,我需要检查更一致的参考来选择正确/最佳的参考。 Is there any such resource that "defines" the correct conventions?有没有这样的资源可以“定义”正确的约定?

There is no correct convention, you adopt the one you prefer and that is your baseline/reference.没有正确的约定,你采用你喜欢的那个,那就是你的基线/参考。

if you use both ReSharper and StyleCop you should set them up to work together meaning to accept and validate code in the same way.如果您同时使用 ReSharper 和 StyleCop,您应该将它们设置为一起工作,这意味着以相同的方式接受和验证代码。

That's a subjective question, so here's my subjective answer: I agree with Resharper and think that this is redundant.这是一个主观问题,所以这是我的主观回答:我同意 Resharper 并认为this是多余的。 Personally I prefix field names with underscore:我个人在字段名称前加上下划线:

public class Foo
{
    private readonly string _name;

    public Foo(string name)
    {
        _name = name;
    }
}

Then I configure the static analysis tools to obey the conventions I use.然后我配置 static 分析工具以遵守我使用的约定。

Different tools suggest different things.不同的工具建议不同的东西。 I would suggest making your own coding guideline document and share that with your team (do start from an existing coding convention).我建议您制作自己的编码指南文档并与您的团队分享(从现有的编码约定开始)。 A good starting point however is the book "Framework design guidelines" ISBN: 978-0321545619然而,一个很好的起点是“框架设计指南”一书 ISBN:978-0321545619

You could also configure resharper to give the warnings/errors you want and have all team members use the same resharper config so they will get the same errors/warnings.您还可以配置 resharper 以提供所需的警告/错误,并让所有团队成员使用相同的 resharper 配置,以便他们获得相同的错误/警告。

Don't do just everything a coding guideline wants, based on the situation it's sometimes better to not stick with it, but if you don't follow the guideline make sure to comment why.不要只做编码指南想要的所有事情,根据情况,有时最好不要坚持它,但如果你不遵循指南,请务必评论原因。

They're more like guidelines anyway savvy;).无论如何,它们更像是指南;)。

I think using FxCop is more useful because it is being provided by Microsoft, so what can be more authentic then Microsoft.我认为使用 FxCop 更有用,因为它是由 Microsoft 提供的,所以还有什么比 Microsoft 更真实的。 http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx

I need to check a more consistent reference to choose what is correct/Best.我需要检查更一致的参考来选择正确/最佳的参考。 Is there any such resource that "defines" the correct conventions?有没有这样的资源可以“定义”正确的约定?

These questions cannot be answered.这些问题无法回答。 References cannot help you "choose what is correct".参考文献不能帮助您“选择正确的”。 And conventions by their very nature do not have a 'correctness' property - we use conventions to arbitrarily decide a consistent approach to those questions that do not have a correct answer.就其本质而言,约定并不具有“正确性”属性——我们使用约定来任意决定对那些没有正确答案的问题采用一致的方法。

If you want to follow StyleCop's guideline here, you can configure ReSharper to stop complaining about the use of this .如果您想在此处遵循 StyleCop 的指南,您可以配置 ReSharper 以停止抱怨this的使用。 This is in ReSharper | Options | Code Inspection | Inspection Severity这是在ReSharper | Options | Code Inspection | Inspection Severity ReSharper | Options | Code Inspection | Inspection Severity ReSharper | Options | Code Inspection | Inspection Severity - or, I believe, there is a specific 'StyleCop for ReSharper' plugin which will handle this for you. ReSharper | Options | Code Inspection | Inspection Severity - 或者,我相信,有一个特定的“StyleCop for ReSharper”插件可以为您处理这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM