仅大小写不同的 C# 自动实现属性和局部变量的最佳实践?

发布于 2024-08-06 21:42:45 字数 429 浏览 6 评论 0原文

让我举个例子:

public class MyClass
{
    public string MyProperty { get; set; }

    public MyClass(string myProperty)
    {
        MyProperty = myProperty; // bad?
        this.MyProperty = myProperty; // good?
    }
}

我在这种情况下开始使用 this ,因为我有轻微的偏执,认为单独依赖大小写可能会令人困惑,或者更糟糕的是可能会导致错误。

这里的“最佳实践”是什么?

编辑:

到目前为止,听起来这比我想象的要主观得多。我认为人们会强烈地站在一边或另一边。

Let me give you an example:

public class MyClass
{
    public string MyProperty { get; set; }

    public MyClass(string myProperty)
    {
        MyProperty = myProperty; // bad?
        this.MyProperty = myProperty; // good?
    }
}

I've taken to using this in this scenario, because I have minor paranoia that relying on case alone might be confusing or worse might actually lead to bugs.

What is the "best practice" here?

EDIT:

So far, it sounds like this is a lot more subjective than I thought. I figured people would come down strongly on one side or the other.

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

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

发布评论

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

评论(7

云雾 2024-08-13 21:42:45

在任何类中使用“this.”都是多余的。完全由您的开发团队来设定使用它的标准。

使用“this.”的优点是,一些开发人员发现在阅读代码时更容易将其与类实例联系起来,并且正如您提到的,在处理类似名称的项目。

缺点是有些人认为它会使您的代码文件变得混乱,如果您使用 ReSharper 等工具,他们会默认将其标记为冗余代码。

Using "this." is redundant in any class. It's totally up to your development shop to set a standard for using it.

The pros of using "this." are that some developers find it easier to associate it in their mind with the class instance when they are reading the code, and as you mention, make it clearer when dealing with similarly named items.

The cons are that some people view it as cluttering up your code file and if you use tools like ReSharper, they mark it as redundant code by default.

硬不硬你别怂 2024-08-13 21:42:45

正如旺普所说。 “this”是多余的,但它使代码更易于阅读。或者说更难误读。

As womp said. "this" is redundant but it makes the code easier to read. Or rather harder to misread.

ペ泪落弦音 2024-08-13 21:42:45

没有风险。

C# 绝对区分大小写,因此使用... MyProperty = myProperty;

因此,我会寻找其他最佳实践,例如编写实现目标所需的最少代码(同时进行自我记录)。事实是,这不是必需的,极简主义者可能会说忽略它。

C# is definately case sensitive so there is no risk in using...

MyProperty = myProperty;

So then I would look to other best practices like writing the least amount of code needed to achieve your goal (while being self documenting). The truth is, it's not required, minimalists might say leave it out.

最佳男配角 2024-08-13 21:42:45

以下是我目前如何使用您的示例初始化属性(自动实现和非自动实现),

        public class MyClass
    {
        public string MyProperty { get; set; }

        public string AnotherProperty
        {
            get { return _anotherProperty; }
            set { _anotherProperty = value; }
        }
        private string _anotherProperty;

        public MyClass(string myProperty, string anotherProperty)
        {
            MyProperty = myProperty; // auto-implemented property initialization
            _anotherProperty = anotherProperty; //property with member variable initialization                
        }
    }

使用“this”的点对我来说是过度规范。我知道它是本地财产,因为它是大写的。所有财产均应大写。由于下划线,我知道变量“_anotherProperty”具有类范围。我曾经省略类级变量中的下划线。当下划线存在时,代码对我来说更容易阅读,因为我立即知道范围,而无需将鼠标悬停在变量上以查看 VS 工具提示中的声明。另外,我还可以通过省略下划线来对局部变量使用相同的名称。这使得你的初始化看起来很干净。下划线的另一个好处是,您可以键入下划线并按 ctrl+space,所有类范围的变量都会被分组。

Here's how I currently initialize properties using your example (both auto-implemented and not)

        public class MyClass
    {
        public string MyProperty { get; set; }

        public string AnotherProperty
        {
            get { return _anotherProperty; }
            set { _anotherProperty = value; }
        }
        private string _anotherProperty;

        public MyClass(string myProperty, string anotherProperty)
        {
            MyProperty = myProperty; // auto-implemented property initialization
            _anotherProperty = anotherProperty; //property with member variable initialization                
        }
    }

Dotting in using 'this' is over specification to me. I know that it's a local property because it is capitalized. All properties should be capialized. I know that the variable '_anotherProperty' has class scope because of the underscore. I used to omit the underscore from class-level variables. Code is easier for me to read when the underscore is there because I immediately know the scope without having to mouse over the variable to see the declaration in the tooltip from VS. Also, I get the benefit of using the same name for local variables by just omitting the underscore. This makes your initializations look clean. Another benefit of the underscore is that you can type an underscore and press ctrl+space and all of your class-scoped variables are grouped.

白云不回头 2024-08-13 21:42:45

在我的工作场所,编码标准规定属性应这样编写,而局部变量应这样编写。由于 C# 区分大小写,因此这是一个很好的工具来区分变量。但是,如果您发现自己的属性和局部变量具有完全相同的名称,那么使用 this 关键字肯定会消除用法的歧义。

At my workplace, coding standards dictate that properties be written LikeThis while local variables be written likeThis. As C# is case sensitive, this is a good tool to utilize to distinguish your variables apart. If, however, you find yourself with a property and local variable with the exact same name, using the this keyword will definitely disambiguate the usage.

迷途知返 2024-08-13 21:42:45

您的两个选择都仅依赖于大小写......两者之间没有区别。

Both of your options rely on case alone.... There is no difference between either.

昵称有卵用 2024-08-13 21:42:45

在我看来,这里的“最佳实践”是“不要这样做”。如果我在正在审查的代码中遇到这个问题,我会立即标记它。两个仅因情况不同的变量是一种即将发生的误解。对于维护程序员来说,几个月或几年后,很容易无意中分配给 myThing 而不是 MyThing

稍后添加:

一位评论者询问我的建议以替换大写/小写命名约定。为此,我需要一个具体的例子。假设您有一个简单的 Book 类,它只有一个属性: Title

public class Book
{
    public string Title { get; private set; }
}

现在您需要一个构造函数。常见的约定是使用属性的小写版本:

public Book(string title)
{
    Title = title;
}

或者,如果您想确保没有歧义:this.Title = title

人们可以说这在构造函数中是可以的。如果所有的构造函数都这么简单的话,情况可能会是这样。但我的经验是,当构造函数超出几行时,Titletitle 之间的区别就会消失。当您谈论构造函数以外的方法时,问题会变得更糟。无论哪种方式,您都需要不同的约定。

用什么?我在参数中使用过并看到过各种缩写:例如 ttl。或者像 bookTitle 这样的内容,在使用 Intellisense 时更具描述性。在我看来,这两种方法都比使用仅大小写不同的名称的惯例更好。

In my opinion, the "best practice" here is, "don't do that." If I run across that in code I'm reviewing, I immediately flag it. Having two variables that differ only by case is a misunderstanding just waiting to happen. It's just too easy for a maintenance programmer to come along months or years later and inadvertently make an assigment to myThing instead of MyThing.

Added later:

A commenter asked for my suggestion to replace the upper/lower case naming convention. For that I need a concrete example. Say you have a simple Book class that has only one property: Title:

public class Book
{
    public string Title { get; private set; }
}

Now you need a constructor. A common convention is to use a lowercase version of the property:

public Book(string title)
{
    Title = title;
}

Or, if you want to make sure there's no ambiguity: this.Title = title.

One can make the argument that this is okay in constructors. And it might be, if all constructors were so simple. But my experience has been that when a constructor goes beyond just a few lines, the distinction between Title and title gets lost. The problem becomes worse when you're talking about methods other than constructors. Either way, you need a different convention.

What to use? I've variously used and seen used abbreviations in the parameters: ttl, for example. Or something like bookTitle, which is more descriptive when using Intellisense. In my opinion, either is preferable to the convention of using a name that differs only by case.

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