简体   繁体   English

C#自动实现的属性和局部变量的最佳实践,仅根据具体情况而有所不同?

[英]Best practice for C# Auto-Implemented Property and local variable that differ only by case?

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. 我已经开始在这种情况下使用this ,因为我有轻微的偏执,单独依赖案例可能会令人困惑,或者更糟糕的可能实际上会导致错误。

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. 我认为人们会在一方或另一方强烈下来。

Using " this. " is redundant in any class. 使用“ this. ”在任何课程中都是多余的。 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. 使用“ this. ”的优点是,一些开发人员发现在阅读代码时,更容易将它们与类实例相关联,正如您所提到的,在处理类似命名的项目时更清楚。

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. 缺点是有些人认为它使你的代码文件变得混乱,如果你使用像ReSharper这样的工具,它们默认将它标记为冗余代码。

As womp said. 正如womp所说。 "this" is redundant but it makes the code easier to read. “这”是多余的,但它使代码更容易阅读。 Or rather harder to misread. 或者更难以误读。

C# is definately case sensitive so there is no risk in using... C#绝对区分大小写,因此使用时没有风险......

MyProperty = myProperty; 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. 事实是,这不是必需的,极简主义者可能会说不管它。

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. 点击使用'this'对我来说超出了规范。 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. 我知道变量'_anotherProperty'因为下划线而具有类范围。 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. 当下划线存在时,我更容易阅读代码因为我立即知道范围而不必将鼠标悬停在变量上以查看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. 下划线的另一个好处是您可以键入下划线并按下ctrl +空格,并且所有类范围的变量都会被分组。

At my workplace, coding standards dictate that properties be written LikeThis while local variables be written likeThis. 在我的工作场所,编码标准要求写出属性LikeThis,而局部变量写成像这样。 As C# is case sensitive, this is a good tool to utilize to distinguish your variables apart. 由于C#区分大小写,因此这是一个很好的工具,用于区分变量。 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. 但是,如果您发现自己的属性和局部变量具有完全相同的名称,则使用this关键字肯定会消除使用歧义。

你的两个选择都只依赖于案例......两者之间没有区别。

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 . 对于维护程序员来说,几个月或几年之后很容易过来,并且无意中对myThing而不是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 : 假设您有一个只有一个属性的简单Book类: 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 . 或者,如果你想确保没有歧义: 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. 但我的经验是,当构造函数超出几行时, Titletitle之间的区别就会丢失。 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. 我在参数中使用了各种各样的缩写:例如ttl Or something like bookTitle , which is more descriptive when using Intellisense. 或类似bookTitle东西,在使用Intellisense时更具描述性。 In my opinion, either is preferable to the convention of using a name that differs only by case. 在我看来,要么优于使用仅因情况而异的名称的惯例。

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

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