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

前端 未结 17 2094
猫巷女王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:46

    No, but then I write a lot of VB ;)

    About the only time I'll check this/Me is when I don't remember the exact name of the member or when I need to distinguish it with a function parameter of the same name.

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

    It adds clutter. So no.

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

    I do, for me it adds a bit of clarity to the code, is it in the current procedure or the class?

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

    I do. I got in the habit back when Intellisense in Visual Studio wasn't as clever as it is these days.

    I don't find it distracting, I suppose because I write a lot of Python and am used to seeing self everywhere.

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

    Yes, if I see this. I'm sure it is local and I do not need to look any further. If it isn't prefixed with this. (or maybe '_') I have to check if it is declared locally or in an ancestor or if it's a parameter or ...

    All these checks take a little more time during debugging...

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

    If your instance variable name is same as method argument- it is no longer "simply a reason to clarify". If you don't do prepend- it can lead to defects.

    I think that's what Ben S meant- but just wanted to stress- that its not a matter of best practice any more.

    I often do- increases clarity. I don't think it adds to clutter- because our brains quickly reads past it, but registers that it is an instance variable

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