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

前端 未结 9 2095
孤街浪徒
孤街浪徒 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:37

    I follow it mainly for intellisense reasons. It is so nice typing this. and getting a consise list of properties, methods, etc.

    0 讨论(0)
  • 2020-11-27 04:43
    this.This 
    this.Does 
    this.Not 
    this.Add 
    this.Clarity 
    this.Nor 
    this.Does 
    this.This 
    this.Add 
    this.Maintainability 
    this.To 
    this.Code
    

    The usage of "this.", when used excessively or a forced style requirement, is nothing more then a contrivance used under the guise that there is < 1% of developers that really do not understand code or what they are doing, and makes it painful for 99% who want to write easily readable and maintainable code.

    As soon as you start typing, Intellisence will list the content available in the scope of where you are typing, "this." is not necessary to expose class members, and unless you are completely clueless to what you are coding for you should be able to easily find the item you need.

    Even if you are completely clueless, use "this." to hint what is available, but don't leave it in code. There are also a slew of add-ons like Resharper that help to bring clarity to the scope and expose the contents of objects more efficiently. It is better to learn how to use the tools provided to you then to develop a bad habit that is hated by a large number of your co-workers.

    Any developer that does not inherently understand the scope of static, local, class or global content should not rely on "hints" to indicate the scope. "this." is worse then Hungarian notation as at least Hungarian notation provided an idea about the type the variable is referencing and serves some benefit. I would rather see "_" or "m" used to denote class field members then to see "this." everywhere.

    I have never had an issue, nor seen an issue with a fellow developer that repeatedly fights with code scope or writes code that is always buggy because of not using "this." explicitly. It is an unwarranted fear that "this." prevents future code bugs and is often the argument used where ignorance is valued.

    Coders grow with experience, "this." is like asking someone to put training wheels on their bike as an adult because it is what they first had to use to learn how to ride a bike. And adult might fall off a bike 1 in 1,000 times they get on it, but that is no reason to force them to use training wheels.

    "this." should be banned from the language definition for C#, unfortunately there is only one reason for using it, and that is to resolve ambiguity, which could also be easily resolved through better code practices.

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

    In addition it is possible to duplicate variable names in a function so using 'this' can make it clearer.

    class foo {
      private string aString;
    
      public void SetString(string aString){
        //this.aString refers to the class field
        //aString refers to the method parameter        
        this.aString = aString; 
      }
    }
    
    0 讨论(0)
提交回复
热议问题