Why does StyleCop recommend prefixing method or property calls with “this”?

前端 未结 9 2094
孤街浪徒
孤街浪徒 2020-11-27 04:15

I have been trying to follow StyleCop\'s guidelines on a project, to see if the resulting code was better in the end. Most rules are reasonable or a matter of opinion on cod

相关标签:
9条回答
  • 2020-11-27 04:24

    It can make code clearer at a glance. When you use this, it's easier to:

    • Tell static and instance members apart. (And distinguish instance methods from delegates.)
    • Distinguish instance members from local variables and parameters (without using a naming convention).
    0 讨论(0)
  • 2020-11-27 04:26

    I do follow it, because I think it's really convenient to be able to tell apart access to static and instance members at first glance.

    And of course I have to use it in my constructors, because I normally give the constructor parameters the same names as the field their values get assigned to. So I need "this" to access the fields.

    0 讨论(0)
  • 2020-11-27 04:29

    I don't really follow this guidance unless I'm in the scenarios you need it:

    • there is an actual ambiguity - mainly this impacts either constructors (this.name = name;) or things like Equals (return this.id == other.id;)
    • you want to pass a reference to the current instance
    • you want to call an extension method on the current instance

    Other than that I consider this clutter. So I turn the rule off.

    0 讨论(0)
  • 2020-11-27 04:29

    I think this article explains it a little

    http://blogs.msdn.microsoft.com/sourceanalysis/archive/2008/05/25/a-difference-of-style.aspx

    ...a brilliant young developer at Microsoft (ok, it was me) decided to take it upon himself to write a little tool which could detect variances from the C# style used within his team. StyleCop was born. Over the next few years, we gathered up all of the C# style guidelines we could find from the various teams within Microsoft, and picked out all of best practices which were common to these styles. These formed the first set of StyleCop rules. One of the earliest rules that came out of this effort was the use of the this prefix to call out class members, and the removal of any underscore prefixes from field names. C# style had officially grown apart from its old C++ tribe.

    0 讨论(0)
  • 2020-11-27 04:34

    A few basic reasons for using this (and I coincidentally always prefix class values with the name of the class of which they are a part as well - even within the class itself).

    1) Clarity. You know right this instant which variables you declared in the class definition and which you declared as locals, parameters and whatnot. In two years, you won't know that and you'll go on a wondrous voyage of re-discovery that is absolutely pointless and not required if you specifically state the parent up front. Somebody else working on your code has no idea from the get-go and thus benefits instantly.

    2) Intellisense. If you type 'this.' you get all instance-specific members and properties in the help. It makes finding things a lot easier, especially if you're maintaining somebody else's code or code you haven't looked at in a couple of years. It also helps you avoid errors caused by misconceptions of what variables and methods are declared where and how. It can help you discover errors that otherwise wouldn't show up until the compiler choked on your code.

    3) Granted you can achieve the same effect by using prefixes and other techniques, but this begs the question of why you would invent a mechanism to handle a problem when there is a mechanism to do so built into the language that is actually supported by the IDE? If you touch-type, even in part, it will ultimately reduce your error rate, too, by not forcing you to take your fingers out of the home position to get to the underscore key.

    I see lots of young programmers who make a big deal out of the time they will save by not typing a character or two. Most of your time will be spent debugging, not coding. Don't worry so much about your typing speed. Worry more about how quickly you can understand what is going on in the code. If you save a total of five minutes coding and win up spending an extra ten minutes debugging, you've slowed yourself down, no matter how fast you look like you're going.

    0 讨论(0)
  • 2020-11-27 04:37

    Note that the compiler doesn't care whether you prefix references with this or not (unless there's a name collision with a local variable and a field or you want to call an extension method on the current instance.)

    It's up to your style. Personally I remove this. from code as I think it decreases the signal to noise ratio.

    Just because Microsoft uses this style internally doesn't mean you have to. StyleCop seems to be a MS-internal tool gone public. I'm all for adhering to the Microsoft conventions around public things, such as:

    • type names are in PascalCase
    • parameter names are in camelCase
    • interfaces should be prefixed with the letter I
    • use singular names for enums, except for when they're [Flags]

    ...but what happens in the private realms of your code is, well, private. Do whatever your team agrees upon.

    Consistency is also important. It reduces cognitive load when reading code, especially if the code style is as you expect it. But even when dealing with a foreign coding style, if it's consistent then it won't take long to become used to it. Use tools like ReSharper and StyleCop to ensure consistency where you think it's important.

    Using .NET Reflector suggests that Microsoft isn't that great at adhering to the StyleCop coding standards in the BCL anyway.

    0 讨论(0)
提交回复
热议问题