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

大城市里の小女人 提交于 2019-12-07 03:39:56

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/1499446/best-practice-for-c-sharp-auto-implemented-property-and-local-variable-that-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!