CA1500 与 SA1309 - 哪一个获胜?

发布于 2024-09-08 12:27:19 字数 901 浏览 2 评论 0原文

我首先要说的是,我理解代码分析和 StyleCop 都是指导方针,但许多人还是选择忽略这些。但话虽如此,我想看看对于这两条规则的普遍共识是什么。

规则 CA1500 规定不要使参数名称和私有字段名称相同。

另一方面,规则 SA1309 规定不要在成员前添加下划线或“米_”。

这使得我们几乎没有选择来区分私有支持字段与其相应的参数。以这些例子为例。

SA1309 抱怨:

class SomeClass
{
    int _someField;

    public SomeClass(int someField)
    {
        this._someField = someField;
    }
}

CA1500 抱怨:

class SomeClass
{
    int someField;

    public SomeClass(int someField)
    {
        this.someField = someField;
    }
}

我有什么选择?我不想将私有支持字段设为 PascalCase,因为这是公共字段/属性的(我相信相当普遍)约定。我不想重命名其中之一,只是为了解决歧义。

因此,我只剩下上述两条规则之一,这需要我取消 SA/CA 规则之一。

你们通常做什么?更重要的是,这些规则的作者认为您应该做什么(因为他们的文档中都没有提供替代解决方案)?

I'll prefix by saying that I understand that both Code Analysis and StyleCop are meant as guidelines, and many people chose to ignore these anyway. But having said that, I'd like to see what the general consensus is with regard to these two rules.

Rule CA1500 says don't make parameter names and private field names the same.

Rule SA1309, on the other hand, says don't prefix members with underscore or "m_".

This leaves us with little options for distinguishing private backing fields from their corresponding parameters. Take these examples.

SA1309 complains:

class SomeClass
{
    int _someField;

    public SomeClass(int someField)
    {
        this._someField = someField;
    }
}

CA1500 complains:

class SomeClass
{
    int someField;

    public SomeClass(int someField)
    {
        this.someField = someField;
    }
}

What options do I have? I don't want to make the private backing field PascalCase, because this is the (I believe fairly universal) convention for public fields/properties. And I don't want to rename one or the other, just for the sake of resolving ambiguity.

So I'm left with one of the above two, which would require me to suppress one of the SA/CA rules.

What do you guys typically do? And more importantly, what do the authors of these rules think you should do (as neither provide alternative solutions in their documentation)?

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

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

发布评论

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

评论(6

咆哮 2024-09-15 12:27:19

我们关闭SA1309。其背后的推理相当薄弱。

我们的团队认为,私人成员以下划线开头的广为接受的做法远远超过了有人可能在代码上使用不同编辑器的想法,无论如何,这种情况在我们的商店中从未发生过。至于提供“立即区分”,下划线也能做到这一点。

如果您确实有开发人员仍然使用“m_”,并且您仍然需要检查这一点,您可以为此编写一个快速规则。

We turn off SA1309. The reasoning behind it is fairly weak.

Our team feels that the well-accepted practice of private members starting with underscores far outweighs the idea that someone might use a different editor on the code, which never happens in our shop anyway. As to providing an "immediate differentiation", the underscore does that as well.

If you really have developers that still use "m_" though and you still need to check for that, you could write a quick rule for just that.

漆黑的白昼 2024-09-15 12:27:19

根据我从 Microsoft 那里看到的情况,我认为 CA1500 获胜。

如果您查看 BCL,您会发现大多数代码都在本地字段前添加下划线。

Based on what I've seen from Microsoft themselves, I say CA1500 wins.

If you look at the BCL, most of the code prefixes local fields with an underscore.

浪漫之都 2024-09-15 12:27:19

这是我通常的解决方案:

class SomeClass
{
    int SomeField{get;set;}

    public SomeClass(int someField)
    {
        SomeField = someField;
    }
}

Here is my usual solution:

class SomeClass
{
    int SomeField{get;set;}

    public SomeClass(int someField)
    {
        SomeField = someField;
    }
}
伴梦长久 2024-09-15 12:27:19

我能想到的唯一替代方案似乎可以满足这两个规则,并且我实际上在任何地方都看到过它的使用,如下所示。我自己并不遵循这个惯例,因为它看起来很笨拙。

public class Class1
{
    // prefix private fields with "m"
    private int mValue1;

    public int Value1
    {
        get { return mValue1; }
        set { mValue1 = value; }
    }

    private string mValue2;

    public string Value2
    {
        get { return mValue2; }
        set { mValue2 = value; }
    }

    // prefix parameters with "p"
    public bool PerformAction(int pValue1, string pValue2)
    {
        if (pValue1 > mValue1)
        {
            mValue2 = pValue2;
            return true;
        }
        else
        {
            return (mValue2 == pValue2);
        }
    }
}

The only alternative I can think of that seems like it would satisfy both rules and that I have actually seen used anywhere is something like the following. I don't follow this convention myself, as it seems clumsy.

public class Class1
{
    // prefix private fields with "m"
    private int mValue1;

    public int Value1
    {
        get { return mValue1; }
        set { mValue1 = value; }
    }

    private string mValue2;

    public string Value2
    {
        get { return mValue2; }
        set { mValue2 = value; }
    }

    // prefix parameters with "p"
    public bool PerformAction(int pValue1, string pValue2)
    {
        if (pValue1 > mValue1)
        {
            mValue2 = pValue2;
            return true;
        }
        else
        {
            return (mValue2 == pValue2);
        }
    }
}
东京女 2024-09-15 12:27:19

很简单,当存在类时,对私有字段使用后缀“Field”:

 private Int32 counterField;

 public Int32 Counter
 {
     get
     {
          return this.counterField;
     }

     set
     {
           if (this.counterField != value)
           {
                this.counterField = value;
                this.OnPropertyChanged("Counter");
            }
      }

并且您可以满足这两个规则。用任何字符或匈牙利语前缀修饰变量都是部落式的。每个人都可以在 StyleCop 或 FXCop 中找到他们不喜欢的规则,但标准只有在每个人都使用它时才有效。自动代码清理器的好处远远超过您个人对语言的“艺术”贡献。

Simple, use the suffix 'Field' for private fields when there's a class:

 private Int32 counterField;

 public Int32 Counter
 {
     get
     {
          return this.counterField;
     }

     set
     {
           if (this.counterField != value)
           {
                this.counterField = value;
                this.OnPropertyChanged("Counter");
            }
      }

And you can satisfy both rules. Decorating your variables with any characters or hungarian prefixes is tribal. Everybody can find a rule that they don't like in StyleCop or FXCop, but a standard only works when everyone uses it. The benefits of an automated scrubber of code far outweigh your own personal 'artistic' contributions to the language.

§普罗旺斯的薰衣草 2024-09-15 12:27:19

没有冲突。更改参数名称。

public class SomeClass
{
    private int namedField { get; set; }

    public SomeClass(int differentlyNamedField)
    {
        this.namedField = differentlyNamedField;
    }
}

There's no conflict. Change the parameter name.

public class SomeClass
{
    private int namedField { get; set; }

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