I used to be in the habit prefixing object field references with “this”. I’d write code like this:

class Person {
    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }
}

It seems harmless enough. But things get out of hand rather quickly:

class Person {
    private String firstName;
    private String lastName;
    private String middleName;

    public String getFullName() {
        return this.firstName + " "
                + this.middleName + " "
                + this.lastName;
    }
}

As you sprinkle in more and more “this” references, the code becomes increasingly cluttered. I found myself asking:

Does this really help?

I think the code is more readable when written like this:

class Person {
    private String firstName;
    private String lastName;
    private String middleName;

    public String getFullName() {
        return firstName + " "
                + middleName + " "
                + lastName;
    }
}

Just sit back and look at the two code examples. Which is more visually appealing? Which one looks cleaner? Do the “this” keywords add clarity, or make the screen busier?

Yeah, But…

Some people will disagree with me on “this” topic. They will argue that any field adorned with “this” is obviously an object field, while other fields are obviously local variables. I used to fall into this camp. I got into the habit of writing code in this style and prefixed all fields with “this” even when the code was painfully obvious without the extra clutter.

Can anybody honestly argue that this method is confusing?

public int getAge() {
    return age;
}

age is obviously not a local variable. Adding the “this.” prefix only adds clutter, making your code less readable. Heck, good IDEs like IntelliJ IDEA even highlight object fields a different color, further removing the need for “this”.

DRY

Don’t Repeat Yourself. If you really insist upon adorning your object fields with some token, I will argue that “this.” is a horrible way of doing so. Instead, consider prefixing your field with a consistent name or character. I’m not crazy about prefixes, but they beat “this” because of the DRY principle. For example:

public class Person {
    // show both ways...
    private String _firstName;
    private String lastName;

    public String getFirstName() {
        return _firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public String getFullName() {
        // oops...forgot to prefix with "this" on our lastName
        return _firstName + lastName;
    }
}

Did you see what happened? In the getFullName() method, I forgot to prefix the lastName with “this”. The code works fine, but this violates the DRY principle. If you adopt the “I shall prefix all fields with ‘this’” mantra, you are advocating reliance upon humans to manually remember to prefix all object fields with “this” over and over again. Relying upon humans to manually copy a prefix over and over is a recipe for inconsistency because you are repeating yourself all over the place.

Adopting a prefix like “m_” or “_” is preferable to “this” because the compiler ensures consistency. It does not rely on human programmers to remember to repeat the “this” prefix over and over.

Let me repeat: Do not Repeat Yourself.

Like I said a few paragraphs back, I don’t like this approach, however it is better than the “this” convention. I’m just presenting it as an option.

Your Code is Too Complicated

What if you have some really complicated method that uses a combination of local variables and object fields? Don’t you need to prefix object fields with “this” to avoid ambiguity?

no.

I don’t buy the argument. To me, it sounds like this:

“This method is too complicated to understand. I’m looking at the code and I cannot tell which field is a local variable and which field is an object field. My solution is to add MORE code by prefixing all object fields with ‘this.’, making the code even more cluttered.”

I propose another solution: refactor the code until it is simple enough to understand without the “this” adornments.

On a Related Topic…

Study up on XP and see what the experts have to say about comments. If code is too hard to understand, adding more comments should not be your first reaction. Instead, think really hard about finding ways to simplify your code so the comments are not necessary.

Be Open Minded

Coding conventions are a religious issue, to be sure. I myself used to believe in the “this” convention as you’ll see if you read any of my books. I must confess that it was difficult to give up the habit, and I still find myself habitually typing “this” occasionally.

My advice to anyone is to be open minded about your coding habits. It is really easy to fall into the trap of immediately disliking new ideas because they are foreign to you. If you are in the habit of prefixing your code with “this”, code that does not follow the convention will look ugly to you at first glance. It did to me. All I can say is that I have changed my outlook and I like the simpler style better. You might also if you give it a chance.

Conclusion

  1. Absolutely trivial methods do not need the “this” prefix. Surely you will agree that this is simple:
    public int getAge() {
        return age;
    }
  2. The “this” convention violates the DRY principle. It forces you to manually replicate “this” all over the place. If you forget to follow the convention, the code works the same and the compiler issues no warning.
  3. If your code is too hard to understand, try to simplify it rather than to adorn it with “this” prefixes all over the place.
  4. If you really need to differentiate local variables from object fields, consider prefixing your object fields with “_” or some other character. You can always refactor your code later - using IDEA or a similar refactoring tool of course - if you decide you don’t like the convention!
  5. Don’t blindly follow conventions for the sake of following conventions. Follow conventions because they improve your code. If the coding convention encourages repetition, increases clutter, and relies upon human consistency to be effective, maybe the convention is flawed.

Do “this” prefixes improve clarity or make code more complicated?