Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

The this keyword is primarily used in three situations.

The first and most common is in setter methods to disambiguate variable references.

The second is when there is a need to pass the current class instance as an argument to a method of another object.

The third is as a way to call alternate constructors from within a constructor.

However someone at work decided too put a PMD/Checkstyle rules at work that force us to put "this" in front of all the variable and method. Is it really viable to do that or it's just to verbose ?

Related: What is the accepted style for using the `this` keyword in Java?

share|improve this question

marked as duplicate by Dan Pichelman, unholysampler, gbjbaanb, Telastyn, gnat Sep 12 '13 at 20:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

It's a coding style that's not that uncommon. Whether it's "too verbose" is a subjective matter of opinion. Some people like it, some don't. Some people use naming conventions for their instance fields to differentiate them from local variables; this is meant to accomplish the same goal. – Servy Sep 12 '13 at 18:00
Is it overkill since modern IDEs will achieve the same purpose by using a different color for local parameters/local variables/fields which help differentiate them. – Chris Sep 12 '13 at 18:14
Again, that's a subjective matter of opinion. If you don't like it, then speak with your team and decide, as a group, to not use this particular convention. If your team disagrees with you then this is one of those times where you'll simply need to bite the bullet and use a convention you don't like for the sake of your team. That's simply life programming with other people. – Servy Sep 12 '13 at 18:17
@Servy - Programmers.SE can tolerate that level of subjectivity within a question. If you put your two comments together, I think they'd make a reasonable answer to this question. You're doing a good job at objectively calling out the pros / cons of both sides of the style issue. – GlenH7 Sep 12 '13 at 18:23
I don't understand why it is considered duplicated. I added the related question because it was related but didn't answered what I wanted to know. – Chris Sep 12 '13 at 21:19
up vote 10 down vote accepted

This question straddles the boundary of opinion-based vs. factual information; nevertheless I consider it valuable enough to leave open because I find the usual answers of "this is a style question, do what you like" too simplistic. So here's my pointed opinion:

Using this to refer to attributes of a class within the class is redundant, and so increases code verbosity with no clear benefit. Not only can you almost always look up the status of a variable via the tools you use; more importantly, if you have to look it up, then your class is too big in the first place. Just like a method that requires you to scroll up to find the declaration of a local variable is too long, if you have to look up a member field while writing code within a class, then that class does too much.

In other words, using this.fieldName except for disambiguation is unnecessary cruft, and if you need it for the reason commonly given, you have greater problems than scope issues.

share|improve this answer
I agree it's subjective. But I've seen the ridiculous extreme of having stuff like this.myLocalMethod() all over the codebase. What purpose at all does this accomplish in this case? – Andres F. Sep 12 '13 at 19:06
@AndresF. - it provides a clear, unambiguous signal at the call site what variables are instance vs local vs static. Sometimes that's worth the cruft, sometimes it's a smell that your types are too big or your methods too complex. I personally prefer this. to mFooBar or _fooBar – Telastyn Sep 12 '13 at 19:11
@Telastyn Agreed, but note I mentioned a local method, not a local variable. Adding this to a local method is always cruft in Java. Plus it's against all common practices (including the official Java style guide from Sunacle), but I still I see it at my workplace, enforced by automatic code formatting :( – Andres F. Sep 12 '13 at 19:32
this also indicates that it's an instance member that is being called as opposed to a static, member - at a glance. In the case of a method call, the former indicates that the method can possibly modify the state of the object, the latter can not (unless this is passed as a parameter, or some type of reflection is being used based on the caller). – Steve Evers Sep 12 '13 at 19:33
@SteveEvers Note however than when there is name clashing it may be useful, as when there is both a local variable and a method argument with the same name (typical of setters). But what name clash does this.localMethod() solve? Visually, local and static methods are styled differently by the IDE, so a person will never be confused; and the compiler is never confused about the two, so... what's the purpose again? – Andres F. Sep 12 '13 at 19:35

Just considering Java: too verbose IHMO.

However, if you're in an environment where you are switching frequently between Java and Javascript, having consistency in the usage of 'this.' might have benefits. (I'd still consider it too verbose, but I can see the case being made).

share|improve this answer
You had me at "Java: too verbose". – Andres F. Sep 12 '13 at 20:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.