stylistic use ofexplicit this.

Discussion in 'Java' started by P.Hill, Apr 21, 2004.

  1. P.Hill

    P.Hill Guest

    This is a syntax style question, but hopefully not the usually silly
    personal choice issues. I find myself use lots of explicit "this."
    while most Java code has few to none explicit "this." uses.

    Over the last couple of years I have spent some time programming in
    languages other than Java. Some other languages do not have implicit
    this, they always require explicit this.somevirtualMethod( ... )
    Another influence upon my style is the commonality of code completion in modern
    IDEs, so I find myself typing this. then looking for the needed completion.
    Between these two influences I find myself writing "this." more if not
    very often in my Java classes. I learned long ago that it is just simplier
    to stick with the defacto standard, so everyones code reads the same.
    Years ago in C I was a fan of aligned braces, these days I have no
    problem running with the K&R brace style that came into Java, because
    that is the Java-style.

    I realize lots of "this." is not the typical Java style, but didn't think
    anything of it until a co-worker said that when he sees an explicit "this."
    it is stylistically saying to him that the writer wants to bring attention
    to which method or member the code is using, otherwise
    if it just another call, he doesn't expect to see "this.". I didn't get
    into any great discussion with him about it, so I'm not sure even where
    he might expect to use explicit "this.".

    I checked into various style guides, but could find none that mention
    stylistic use of explicit "this." for certain cases.

    Does anyone have any comments on such a stylistic usage of explicit
    "this.", such that you tend to insert it under certain conditions to help
    to make the code clearer, or expect that a certain pattern is being used
    when you see "this." or find it actually be confusing to see an explicit
    "this."

    I'd be interested in your comments.

    -Paul
     
    P.Hill, Apr 21, 2004
    #1
    1. Advertising

  2. P.Hill

    Chris Smith Guest

    P.Hill wrote:
    > Does anyone have any comments on such a stylistic usage of explicit
    > "this.", such that you tend to insert it under certain conditions to help
    > to make the code clearer, or expect that a certain pattern is being used
    > when you see "this." or find it actually be confusing to see an explicit
    > "this."


    Well, it's a simple fact that 'this.method()' is more complex code than
    'method()'. As such, I'd favor the latter unless there are definite
    reasons otherwise. I wouldn't consider your experience with other
    languages or quirks about how you use tools to be a definite reason,
    since both are irrelevant to anyone maintaining your code.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 21, 2004
    #2
    1. Advertising

  3. P.Hill

    P.Hill Guest

    Chris Smith wrote:
    > P.Hill wrote:
    >
    > Well, it's a simple fact that 'this.method()' is more complex code than
    > 'method()'. As such, I'd favor the latter unless there are definite
    > reasons otherwise.


    Okay, so Chris, you weigh in with a 'let's just keep it short as possible'.
    This seems a fair enough choice. So given you don't use it in general,
    do you ever? Are there any "this." constructs in your code?
    What would be any of those 'reasons otherwise' which
    would lead to specific coding patterns as eluded to by my co-worker and
    suggested by you.

    You are right in that how I got there is of no concern to the next programmer.
    I only explained the reasons I was doing it to explain that I had
    walked (wandered?) into the style not because I was so drawn by an clarity or
    lack thereof, but that I had simply grown into it, one for it usefulness in
    IDEs, the other because it is the expected form in other languages. Another way
    to put it is that it wouldn't take much convincing to switch me back. While
    starting down that path I had taken it as a benign choice -- that is until a
    co-worker expressed his dislike of the style.

    -Paul
     
    P.Hill, Apr 21, 2004
    #3
  4. P.Hill

    Chris Smith Guest

    P.Hill wrote:
    > Okay, so Chris, you weigh in with a 'let's just keep it short as possible'.
    > This seems a fair enough choice. So given you don't use it in general,
    > do you ever? Are there any "this." constructs in your code?


    I tend to use 'this.' to disambiguate as part of an idiomatic form in
    mutator expressions:

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

    This isn't really good practice; just a habit of mine. It's
    particularly important to avoid this form when the identifier gets
    longer and thus more easily mistyped, because it can lead to silent
    bugs. One of those differences between real life and best practice, I
    guess.

    Nevertheless, I still tend to use the basic form even when avoiding
    duplicating the identifier, as in:

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

    Again, there's not a good reason to do this; but you asked what I do,
    not what I think is good to do.

    I don't think I ever use 'this.' in method invocation expressions. I
    could imagine using 'OuterClass.this.someMethod()', though, to avoid
    ambiguous method dispatch when programming with inner classes.

    > What would be any of those 'reasons otherwise' which
    > would lead to specific coding patterns as eluded to by my co-worker and
    > suggested by you.


    I don't have specific examples. That's just a way of hedging, because I
    know that I can't possibly predict every possible readability concern in
    everyone else's code.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 21, 2004
    #4
  5. P.Hill

    P.Hill Guest

    Chris Smith wrote:

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


    I tend toward this also but not particularly for the reasons I gave for general
    introduction of this. The modern IDEs (Eclipse and IntelliJ maybe others?)
    all will tell me if I stupidly do:

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

    > I don't think I ever use 'this.' in method invocation expressions. I
    > could imagine using 'OuterClass.this.someMethod()', though, to avoid
    > ambiguous method dispatch when programming with inner classes.


    Hmm, I guess I wasn't even aware that I could skip the OuterClass.this
    when working with inner classes and still expect it to find the outer
    method.

    -Paul
     
    P.Hill, Apr 21, 2004
    #5
  6. P.Hill

    Roedy Green Guest

    On Wed, 21 Apr 2004 09:44:48 -0600, "P.Hill" <>
    wrote or quoted :

    >This is a syntax style question, but hopefully not the usually silly
    >personal choice issues. I find myself use lots of explicit "this."
    >while most Java code has few to none explicit "this." uses.


    I'm with you. If in doubt put in the explicit this, especially when
    you are dealing with two different objects at once this one and some
    other.

    Another time you must use it is in constructors in them:

    this.port = port;

    Other times you might use it just to emphasise this a per object
    variable where you might have expected it to be a common static per
    class variable.


    However, using it all the time is excessive. Most of the time it would
    just be clutter.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 21, 2004
    #6
  7. P.Hill

    Roedy Green Guest

    On Wed, 21 Apr 2004 13:09:32 -0600, Chris Smith <>
    wrote or quoted :

    > public void setFoo(String newVal)
    > {
    > this.foo = newVal;
    > }

    that screws your JavaDoc.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 21, 2004
    #7
  8. P.Hill

    Dale King Guest

    "P.Hill" <> wrote in message
    news:c664ta$bgr$...
    > This is a syntax style question, but hopefully not the usually silly
    > personal choice issues. I find myself use lots of explicit "this."
    > while most Java code has few to none explicit "this." uses.


    I don't really have a problem with it if it is used sparingly. It is much
    better than using prefixes on member variables like some people do to
    differentiate local variables from member variables. (e.g. m_variable to
    indicate a member variable).

    I agree with Chris that it should be used infrequently when it makes the
    code clearer. I would not like to see it used on every variable or method
    access.


    --
    Dale King
    Blog: http://daleking.homedns.org/Blog
     
    Dale King, Apr 21, 2004
    #8
  9. P.Hill

    Mohun Biswas Guest

    Roedy Green wrote:
    > I'm with you. If in doubt put in the explicit this, especially when
    > you are dealing with two different objects at once this one and some
    > other.


    Let's take it a little farther to a related style issue I've never seen
    treated: let's say your class has private members foo and bar, and
    therefore accessors getFoo() and getBar(). Now in some third method I
    need the value of foo. My options include:

    this.getFoo()
    getFoo()
    foo

    In other words should a class access its own members via accessors or
    directly? Directly makes debugging a little easier because most IDE's
    let you click on the variable to see its value; running an accessor can
    be done also but may take a couple more steps. But I can see where it
    would make refactoring tougher.

    MB
     
    Mohun Biswas, Apr 22, 2004
    #9
  10. P.Hill

    P.Hill Guest

    Mohun Biswas wrote:
    > In other words should a class access its own members via accessors or
    > directly?


    It can be better for future modification if even in the class it self
    you use the accessor, but there is more than a few lines of my code that
    accesses a classes members of itself directly.

    But the debug question seems spurious, every debugger I've used shows
    all members of an object in whatever is the variable window/view, but
    I've never spent a lot of time with such new fangled features as hover
    or click to see something. I usually drill to whatever I need in
    the variable display -- the one that shows this, locals and other
    variables.

    -Paul
     
    P.Hill, Apr 22, 2004
    #10
  11. P.Hill

    Adam Maass Guest

    "P.Hill" <> wrote in message
    news:c664ta$bgr$...
    > This is a syntax style question, but hopefully not the usually silly
    > personal choice issues. I find myself use lots of explicit "this."
    > while most Java code has few to none explicit "this." uses.
    >


    In general: I use an explicit "this." when it will help disambiguate my
    code.

    Writing .equals methods is one place where the explicit "this" helps a lot.


    -- Adam Maass
     
    Adam Maass, Apr 22, 2004
    #11
  12. On Wed, 21 Apr 2004 23:52:04 GMT, Mohun Biswas wrote:

    > Roedy Green wrote:
    >> I'm with you. If in doubt put in the explicit this, especially when
    >> you are dealing with two different objects at once this one and some
    >> other.

    >
    > Let's take it a little farther to a related style issue I've never seen
    > treated: let's say your class has private members foo and bar, and
    > therefore accessors getFoo() and getBar(). Now in some third method I
    > need the value of foo. My options include:
    >
    > this.getFoo()
    > getFoo()
    > foo


    I use the last option normally. If I can't use an IDE with debugging
    support and I need to see the changes, I'll convert my code to use
    accessors. For this I introduce new private accessors.

    private int foo;

    becomes

    private void foo(int value) { this.foo0 = value; }
    private int foo() { return foo0; }

    private int foo0;

    For me, this affects readability the least. The change in the name of the
    variable ensures that I won't miss any callsites.
     
    Timo Kinnunen, Apr 22, 2004
    #12
  13. P.Hill

    P.Hill Guest

    Adam Maass wrote:

    > In general: I use an explicit "this." when it will help disambiguate my
    > code.
    >
    > Writing .equals methods is one place where the explicit "this" helps a lot.


    Oh definitely! I had forgotten about this one.

    if ( this.equals(that) ) {
    ...
    }
    is definitely the right construct.

    if ( equals(that) ) {
    ....
    }

    is just stupid.

    -Paul
     
    P.Hill, Apr 22, 2004
    #13
  14. P.Hill

    Adam Maass Guest

    "P.Hill" <> wrote in message
    news:c68pfk$6ic$...
    > Adam Maass wrote:
    >
    > > In general: I use an explicit "this." when it will help disambiguate my
    > > code.
    > >
    > > Writing .equals methods is one place where the explicit "this" helps a

    lot.
    >
    > Oh definitely! I had forgotten about this one.
    >
    > if ( this.equals(that) ) {
    > ...
    > }
    > is definitely the right construct.
    >
    > if ( equals(that) ) {
    > ...
    > }
    >
    > is just stupid.
    >
    > -Paul
    >


    I was thinking more like

    class Foo {
    int a, b, c;
    public boolean equals(Object o){
    Foo that = (Foo)o;

    return this.a == that.a && this.b == that.b && this.c == that.c;
    }
    }
     
    Adam Maass, Apr 23, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov
    Replies:
    4
    Views:
    490
    Siemel Naran
    Apr 30, 2004
  2. Mantorok Redgormor

    stylistic prototype question

    Mantorok Redgormor, Oct 29, 2003, in forum: C Programming
    Replies:
    5
    Views:
    371
    Mark A. Odell
    Oct 30, 2003
  3. Andrew Koenig

    Stylistic question about inheritance

    Andrew Koenig, Mar 31, 2005, in forum: Python
    Replies:
    17
    Views:
    503
    Guy Bolton King
    Apr 1, 2005
  4. Mark Healey

    A stylistic question.

    Mark Healey, May 14, 2006, in forum: C Programming
    Replies:
    60
    Views:
    1,268
    Jordan Abel
    May 28, 2006
  5. Replies:
    7
    Views:
    305
    Jerry Coffin
    Jul 27, 2007
Loading...

Share This Page