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

2019-01-27 11:57发布

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 parameters, but rather when it seems unnecessary.

Example:

public class Person {        
    private String name;

    public String toString() {
        return this.name;
    }
}

In toString, why not just reference name as name?

return name;

What does this.name buy?

Here's a stackoverflow question whose code has this pre-pending.

16条回答
SAY GOODBYE
2楼-- · 2019-01-27 12:48

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-27 12:48

Something else to keep in mind is the language itself. You didn't mention Java specifically (though I'm assuming you didn't really have anything else in mind, so this comment is more FYI), but as the previous posters have mentioned already it is an excellent way of making code self-documenting to prevent mix-ups down the road when someone else starts modifying your code base.

If you take PHP, though, the use of $this is typically required when referencing class variables. With differing rules between languages, it is often easiest to stick with the pattern that is common between them, a pattern which just so happens to be a very solid coding style throughout. It's easier for me to simply prepend this to everything than try to remember what language requires it and what language simply "prefers" it.

查看更多
Anthone
4楼-- · 2019-01-27 12:49

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.

查看更多
家丑人穷心不美
5楼-- · 2019-01-27 12:54

A slight aside, but it may be worth noting that the "Clean Up" tool in Eclipse can be set to automatically add/remove this. to member accesses according to preference.

"Java / Code Style / Clean Up" in the preferences dialogue.

查看更多
等我变得足够好
6楼-- · 2019-01-27 12:56
  1. Defensive programming (in case someone editing the code later adds a parameter or local with a conflicting name
  2. Make the code "self documenting," more obvious
查看更多
一纸荒年 Trace。
7楼-- · 2019-01-27 12:56

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.

查看更多
登录 后发表回答