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

前端 未结 17 2096
猫巷女王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 01:55

    Generally yes, I do. Communicating scope is an important aspect of readability. It distinguishes it from local variables, static methods, etc. It also communicates that the definition is "nearby".

    Oh, and I only do it for public methods/properties. Those tend to be capitalized so this.Thing looks right. The internal view looks like the external view (myInstance.Thing). Private properties are often lowercase and so it's a less attractive idea for those.

    It's not strictly necessary of course, and some people prefer it more terse. But it provides hints to me and other devs who might look at the code.

    0 讨论(0)
  • 2020-12-19 01:56

    I only ever use a this. prefix in constructors or setters, primarily in the case where the passed parameters have the same name as the relevant member variables.

    0 讨论(0)
  • 2020-12-19 01:58

    We're using ReSharper which manages all of that very well. Most of the time we remove 'this' unless keeping it in a constructor since we typically use constructor parameters with the same name.

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

    I think this practice improves legibility most of the time, so yes.

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

    Absolutely. 'this' avoids the need for any prefixes, such as m_. More importantly, it quickly improves the performance of my code, and this is why:

    I've really embraced the Microsoft Cops (FxCop, StyleCop). They've really helped me to catch things that normally I wouldn't even think about. For example, if a method does not reference any member variables, one suggestion from FxCop is to mark the method as static, so the method doesn't have to be allocated to every instance of the class. From MSDN:

    Methods that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit non-virtual call sites to these members. Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null. This can result in a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

    Prefixing my member variables with 'this.' does two things for me. First, it satisfies StyleCop. Secondly, and more importantly, it helps me to quickly identify if a method needs to be marked static.

    Of course, running FxCop will tell me if I need to mark a method as static. However, using 'this.' helps me spend more time writing new code and less time remedying FxCop violations.

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

    I do a lot simply because it makes the autocompletion pop up.

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