Do you use 'this' in front of instance variables?

前端 未结 17 2095
猫巷女王i
猫巷女王i 2020-12-19 01:08

When accessing instance variables or properties of a class from within the class itself, do you prepend them with \"this.\"?

相关标签:
17条回答
  • 2020-12-19 02:04

    Anytime I have a method parameter whose name is identical to an instance variable (rarely) I do, to avoid confusion.

    0 讨论(0)
  • 2020-12-19 02:05

    In C#, I absolutely do. The primary reasons are:

    1. Whether to do it or not is a stylistic issue. In spite of all the fighting that goes on, I don't believe there's an objectively better approach.

    2. My source analysis tool (StyleCop) defaults to requiring this. in front of instance accesses. My first point implies that I shouldn't care much about whether I always do or always don't, and because the default StyleCop setting is to always require it, I take the path of least resistance/greatest consistency and I follow the default setting.

    I follow this philosophy for most stylistic issues. I'm a huge fan of not changing default formatting options in an auto-formatting IDE. It just makes everyone's life harder over something that's really just not that important.

    0 讨论(0)
  • 2020-12-19 02:05

    Only when required to distinguish from parameters, as in a setter or constructor. I consider its use in unnecessary cases to be "codejunk", analogous to Edward Tufte's chartjunk. Noise, instead of signal.

    0 讨论(0)
  • 2020-12-19 02:07

    No. I consider it visual noise. I think the this variable is a crutch to bad naming styles. Within my type I should be able to manage the naming of the fields, properties and methods.

    There is absolutely no good reason to name your backing field "myfield", the parameter to the constructor as "myField" and the property to be "myField".

     public class TestClass
     {
        private string myField;
        public TestClass(string myField)
        {
          this.myField = myField;
        }
        public string MyField {get { return myField;} set {myField = value}}
     }
    

    Personally, I always add a prefix of _ to all my private backing fields.

     public class TestClass
     {
        private string _myField;
        public TestClass(string myField)
        {
          _myField = myField;
        }
        public string MyField {get { return _myField;} set {_myField = value}}
     }
    

    and now with automatic properties in C#

     public class TestClass
     {
        private string MyField {get; set;}
        public TestClass(string myField)
        {
          MyField = myField;
        }
     }
    

    Other than the above maybe the only other time you type this. is because you want to see the intellisense for your current type. If you need to do this then I submit that your type is too big and probably not following the Single Responsibility Principle. And lets say you are. Why keep the this. around after you actually make the call. Refactor it out.

    0 讨论(0)
  • 2020-12-19 02:07

    Nope! I can certainly see that it might be beneficial, but nothing I work on is sufficiently complex to need another level of clarification.

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