私有字段的命名约定

发布于 2024-10-09 11:43:03 字数 821 浏览 6 评论 0原文

首先,我知道这个问题之前已经被问过几次,最终,这主要是个人喜好的问题,但是阅读有关该主题的所有帖子,有些事情我不清楚。

基本上,大多数人至少同意的一点是,公共成员应该采用 PascalCased,而私有成员应该采用 lowerCamelCased。

通常引起争议的问题是是否在私有成员前面添加下划线或其他任何内容。前缀违反了几条 StyleCop 规则(但显然可以关闭)。

不添加前缀的理由是您应该使用它。改为前缀。

我的问题是我不明白它是如何产生影响的? 我的意思是,这并不是说你不能在类中的公共成员上使用它。

让我们想象一个类 Customer,如下所示:(

class Customer
{
    private int age;

    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
}

显然,在如此简单的情况下,我可以使用自动属性,但这只是一个示例)。

如果我在此类中添加第二个属性,则没有什么会阻止我使用 this.Age (公共属性)而不是 this.age (私有字段)来引用它。 有时,如果在 getter 级别应用一些验证或格式化,这甚至可能是可取的。

另外,如果我的类的某些其他属性需要修改客户的年龄,那么直接使用该属性而不是支持字段是有意义的,因为设置器还可以实现一些业务规则验证,对吧?

换句话说,我真的不明白 this 关键字如何避免私有支持成员和公共属性之间的混淆,因为这可以在两者上使用,并且 IntelliSense 显示两者?

谢谢。

First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subject, some things are not clear to me.

Basically, something that most people agree with at least is that public member should be PascalCased while private members should be lowerCamelCased.

The matter that usually brings debate is whether or not to prefix the private members by an underscore, or anything else. Prefixing violates several StyleCop rules (which can obviously be turned off though)

The rationale to not prefixing is that you should use this. to prefix instead.

The problem I have is that I don't understand how it is making a difference?
I mean, it is not like you can't use this on a public member anyway inside a class.

Let's imagine a class Customer, looking like this:

class Customer
{
    private int age;

    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
}

(Obviously, in such a simple case, I could use an autoproperty, but that's just an example).

If I added a second property inside this class, nothing would prevent me to refer to it using this.Age (the public property) rather than this.age (the private field).
Sometimes, it could even be wishable, if some validation or formatting was applied at the getter level.

Also, if some other properties of my class needed to modify the customer's Age, it would make sense to use the property rather than the backing field directly as the setter could also implement some business rules validations, right?

In other words, I really don't see how the this keyword avoids the confusion between private backing members and public properties as this can be used on both and IntelliSense shows both?

Thanks.

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

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

发布评论

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

评论(4

寂寞笑我太脆弱 2024-10-16 11:43:03

我强烈喜欢私有字段的领先“_”约定,即使它不遵循 MS 约定:

  1. 它消除了与驼峰式参数名称的冲突 - 无需使用“this”

  2. 它是一个视觉指示器,表明正在读取对象的内部持久状态,或者 - 更重要的是 - 正在写入。这是一个标志,上面写着“这在我碰巧正在查看的特定方法之外有副作用”,在查看不熟悉的代码时了解这一点非常重要。

I strongly prefer the leading "_" convention for private fields, even though it does not follow MS conventions:

  1. It eliminates conflicts with camel cased parameter names - no need to use "this"

  2. It's a visual indicator that the internal persistent state of the object is being read, or - more importantly - being written. It's a flag saying "this has side effects outside of the particular method I happen to be looking at", which is very important to know when looking at unfamiliar code.

眼眸 2024-10-16 11:43:03

你说得很对。事实并非如此。

使用 this 是一种确保您使用类成员的方法,以防命名冲突(例如参数名称与字段名称相同)。

对我来说,公共成员的 pascal 大小写和私人成员的驼峰大小写一直足以作为一种惯例,能够很好地工作。

You are quite right. It doesn't.

Using this is a way to ensure you are using the class member, in case of naming conflicts (say a parameter name that is identical to a field name).

For me, pascal casing public members and camel casing private members has always been enough of a convention to work well.

老子叫无熙 2024-10-16 11:43:03

使用 this.age 可以帮助区分 Age 属性的后备存储和对象方法的 age 参数:

public bool CheckIfOlderThan(int age)
{
   // in here, just using "age" isn't clear - is it the method parameter? 
   // The internal field?? Using this.age make that clear!
   return (this.age >= age); 
}

当然,在这种情况下,您还可以为参数指定一个不太令人困惑的名称,以避免任何冲突......

但在属性的实际定义中 - 读取其值并将其存储在后备存储中 - 添加 this. 并没有真正添加任何东西。我认识的一些人只是喜欢一直使用 this. 前缀 - 不仅仅是在需要时 - 个人喜好,真的......

Using this.age could help distinguish between the backing store of your Age property and an age parameter for a method on your object:

public bool CheckIfOlderThan(int age)
{
   // in here, just using "age" isn't clear - is it the method parameter? 
   // The internal field?? Using this.age make that clear!
   return (this.age >= age); 
}

Of course, in this case, you could also give your parameter a less confusing name to avoid any clashes....

But in the actual definition of the property - reading and storing its value in the backing store - adding the this. doesn't really add anything. Some people I know just prefer to use the this. prefix all the time - not just when it's needed - personal preference, really...

太阳公公是暖光 2024-10-16 11:43:03

使用下划线作为私有字段前缀与使用“this.”基本上是一样的。然而,下划线对我来说使用起来更快、更短、更优雅(我相信这来自 Java)。

函数参数和私有字段具有相同的名称对我来说似乎有点棘手。我不止一次忘记使用“this”,这导致了令人讨厌的 NullPointerException(是的,我有一天做了 java...:))。

据我所知,它没有违反任何 FxCop 规则,因为它不是匈牙利符号。

Prefixing private fields with underscore is basically the same thing as using "this.". However underscore is faster to use, shorter and more elegant to me (this comes from Java I believe).

Having the same names for function parameters and private fields seems a bit tricky to me. Not only once have I forgot to use "this" which resulted in nasty NullPointerException (yes, I did java someday... :) ).

As far as I know it doesn't violate any FxCop rule, as it's not a hungarian notation.

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