In Java, why do people prepend fields with `this`?

前端 未结 16 2000
广开言路
广开言路 2020-12-20 11:18

When referencing class variables, why do people prepend it with this? I\'m not talking about the case when this is used to disambiguate from method

相关标签:
16条回答
  • 2020-12-20 11:35

    It does nothing at the language level. But it does give immediate indication to someone reading the code about the scope of the variable, which improves understanding of the code.

    0 讨论(0)
  • 2020-12-20 11:35

    It helps you identify member variables at a glance..
    the ToString() above is too tiny to illustrate this.
    Suppose you have a screenful size method. You calculate, assign, swap a mix of local and instance variables. this.memberVar or this.PropertyName helps you keep track of where you are modifying instance state via field assignments or property setters.

    0 讨论(0)
  • 2020-12-20 11:41

    In .NET world the Microsoft StyleCop tool also has a rule called "Prefix Local Calls With This":

    A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’. An exception to this rule occurs when there is a local override of a base class member, and the code intends to call the base class member directly, bypassing the local override. In this case the call can be prefixed with ‘base.’ rather than ‘this.’.

    By default, StyleCop disallows the use of underscores or m_ to mark local class fields, in favor of the ‘this.’ prefix. The advantage of using ‘this.’ is that it applies equally to all element types including methods, properties, etc., and not just fields, making all calls to class members instantly recognizable, regardless of which editor is being used to view the code. Another advantage is that it creates a quick, recognizable differentiation between instance members and static members, which are not be prefixed.

    A final advantage of using the ‘this.’ prefix is that typing this. will cause Visual Studio to show the IntelliSense popup, making it quick and easy for the developer to choose the class member to call.

    My suggestion is to choose a convention (use this. or not) and stick with that.

    0 讨论(0)
  • 2020-12-20 11:41

    I try to use 'this.whatever' when writing large chunks of code, because it was easier to figure out what was being referenced. When reading large chunks of code written by other people, at certain points I would often get confused as to whether they were referencing instance variables or local variables.

    0 讨论(0)
  • 2020-12-20 11:47

    Sometimes it is necessary to disambiguate:

    public void setFoo(Bar foo) {
        this.foo = foo;
    }
    

    At other times, it's just a stylistic thing. On the whole, I try to avoid this.blah wherever possible as it is more verbose. In case you're wondering, the resultant bytecode is exactly the same.

    0 讨论(0)
  • 2020-12-20 11:47

    The code is simplier reading. Another good practice, in my opinion, is calling the getXXX() in toString() too (instead then this.XXX), beacuse the getXXX() can have an important logic.

    I think that use the name of attribute without this is'nt a good idea for the maintenance of the application.

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