Main Contents

Using this Keyword to Call Private Methods

code reviews, software development

After a recent code review I noticed a lot of code similar to the following:

this.createAUser();

or

if (this.isValidProfile()) {

Our coding standards allow for using this with fields to make the distinction clear:

private int count;
pubic void setCount(int count) {
  this.count = count
}

We adopted The Elements of Java Style for our coding style guide about 2.5 years ago, but this is a case I’d never spent much time thinking about. I find it a lot clearer to write something like:

createAUser()

or

if (isValidProfile()) {

I always assumed this was a well understood convention, but after spending some Googling around I found nothing specifically on idioms with using this with methods.

I think the best evidence in favor of not using the this keyword is with Martin Fowler’s Extract Method pattern. You often create small methods by doing a simple extract of a code fragment on a method that’s getting a little long. And Martin’s example suggests calling the extracted methods with:

printBanner();
printDetails(getOutstanding())

not:

this.printBanner();
this.printDetails(this.getOutstanding())

For me it all comes down to better readability, but we’ll probably have a short meeting with some of the senior developers to toss it around and come to a final decision. If we hadn’t been doing code reviews on a regular basis we might have never addressed this issue.

Ed Gibbs @ November 1, 2006

13 Comments

  1. assaf November 1, 2006 @ 10:49 pm

    In a language that supports functions, like C or JavaScript, printBanner() and this.printBanner() are not equivalent!

    If there’s a local variable printBanner, the short form will call a function referenced by that variable.

    So if you’re coming from C, or moving to another language, adopting the this.foo() convention can be a good thing.

  2. ndh November 2, 2006 @ 4:44 am

    java’s verbose enough without adopting coding standards that make it worse.

  3. Ed Gibbs November 2, 2006 @ 6:27 am

    Most of our developers are coming out of a Coldfusion or mainframe background, so it’s not like they’re carrying in a convention from somewhere else.

    And after spending a lot of time with Ruby this year, Java’s verbosity can be pretty annoying.

  4. Kevin Klinemeier November 2, 2006 @ 10:43 am

    The “readability” argument for using this.methodName() seems odd. asaaf’s point is interesting, but Java doesn’t have functions (yet). What else could it be?

    I could see an argument for always qualifying your static calls with the classname instead of this.xxx, but again, my IDE shows them differently anyway.

  5. assaf November 2, 2006 @ 10:38 pm

    “What else could it be?”

    My guess would be simplicity. If you write member variable access and member method access the same way, the code looks more familiar.

    I know when I’m a language newbie, I make up conventions like that. I usually lose them after a while.

  6. Ed Gibbs November 7, 2006 @ 5:56 am

    After talking to the lead developer about it some more, he just invented the rule on his own following the idea of clarifying a field reference with this.

  7. assaf November 7, 2006 @ 5:47 pm

    I was curious what the real reason was. Thanks for solving the mystery :-)

  8. Lidor Wyssocky November 20, 2006 @ 5:48 am

    With all due respect, this kind of “dilemma” is a waste of energy (and time). Maybe this is why so many people don’t practice code reviews (or at least don’t like it).

    Surely you can find more meaningful and important issues to discuss in a code review. Using one convention or another is arbitrary in most cases (and can be verified automatically, if it is important to you).

    Use code reviews to discuss material issues which can help the code and the design become better, and the developer to grow as a professional.

    See: http://blog.qualityaspect.com/2006/06/27/elements-of-simplicity-working-around-workarounds/

    Lidor

  9. Lidor Wyssocky November 20, 2006 @ 5:50 am

    Oops… wrong link in the comment above.

    Sorry.

    The right one is: http://blog.qualityaspect.com/2006/04/05/are-your-code-reviews-effective/

    Lidor

  10. Ed Gibbs November 21, 2006 @ 10:42 pm

    There was more of a mentoring role to this question. Many of our developers are about 2-3 years into OO and Java and so they often aren’t sure about small things like using a this keyword. They ask for and want feedback on whether they’re doing things the ‘right’ way.

    Of course there isn’t any right way and it comes down to things like common practice, but in this case making a small change leads to more readable code.

    The final decision was to not use this keyword to call private methods going forward, but not to bother and go back and do all the busy work to take them out unless time presented itself.

  11. Tragomaskhalos December 13, 2006 @ 4:39 am

    I think a lot of this usage has to do with modern IDEs rather than a conscious stylistic choice; you type “this.” to pop up a list of available methods, then select the one you want. I tend to make the effort to go back and delete the “this.” afterwards, but many can’t be bothered …

  12. Ed Gibbs December 14, 2006 @ 10:30 pm

    Good point, I hadn’t thought about the IDE angle.

Leave a comment


Feed