Using this when referencing a member variable

Originally I wrote this whole article using the words “global scrote” instead of “global scope”. My intention was to include a picture of the world inside a scrotum. It was going to be hilarious. Then I had to switch it all back after I started searching for a picture of a scrotum that would be suitable to Photoshop the earth into. I just couldn’t any more. Urgh!

Anyhow, back in 1999 I started learning Object Orientation and Java. I learned that you used the keyword this to denote that you are talking about the class you are currently in. So you could have something like this:

public class bar{
    private Int abc;
    public function foo()
    {
        this.abc = 21;
    }

    public function foobar()
    {
        this.abc = 92;
        this.foo();
    }
 }

It made sense.

Recently I have been playing around with Adobe Flash and Java. I’ve noticed is that there’s no requirement to use the keyword this. I really want to say how this is wrong, and how the language designers have made a poor decision. I have no idea how I can say those things and be taken seriously. Taking into consideration the small fact that I have a total of maybe 6 months experience in both of those languages combined, I can’t help but feel the language designers have a bit more knowledge on the subject than I do. So it probably makes sense in the larger context.

Having said that, I have no idea why this was allowed. I think Java and code written OO in general have large increases in verbosity compared to non-OO. The verbosity allows for readability and maintainability.

Coming from a background where my professional coding has largely been PHP, where PHP allows for (but frowned upon) functions and variables to be defined in a global scope I have learned that you ALWAYS ALWAYS use this. You have no idea whose code you have inherited and you have no idea who will inherit your code. Using this does two things:

  1. In the case of the function, this.foo(), tells you that the code to be executed is the code defined in the function just in this class. No matter if someone defines a foo() in the global scope outside of the file and includes it into the local scope the function in the class will always be run.
  2. In the case of the variable, it says we are using the variable defined inside the class we are currently in.

In the case of point 2 above you may think: “ok… yeah so?”. Well take a look at the code below. This is an example of the code I have been seeing in the Java examples and tutorials:

public class bar extends kablah{
    private Int abc;
    public function foo()
    {
        abc = 21;
    }

    public function foobar()
    {
        abc = 92;
        foo();
    }
 }

That just seems confusing to me. Maybe it’s because I come from an environment where global scope was allowed (but frowned upon as the language and its developers matured). If I am looking at function foobar(), I think ok fuck where does this variable come from? What was it’s previous value, should that really get changed. Do I need to worry about it. Let me try find out. Then (in larger classes) you have to go searching to find out it’s a member variable. Same with the function foo(), I spend some mental energy thinking where is that function defined what does it do (this is even more of a problem if the functions don’t have descriptive names)?

Not entirely sure if I should stop here. Basically I answered myself. It is my PHP background that is causing confusion. With Java everything is an object, so if you started on Java and you see foo() you know that it refers to the current object. Where as if you come from maintaining really old PHP4 code as I have, you have seen some really horrible bugs from people using calling functions and variables defined in the global scope. “How the fuck has this variable changed it’s value?!!? This code isn’t even executing!!?!?”

It’s probably just a choice of style and makes no difference (I am not sure if this is always the case and there may be some special cases in more advanced or fringe OO techniques) but I think using this makes it implicit and adds to the readability. So, I’ve come to the conclusion that I will use this and attempt to incorporate what I have just learned into my frame of reference.

Edit: Turns out it does make a difference if you use this or not in java. Which is what I think Jon was trying to tell me in the comments. If you look the example code below

 

public class bar extends kablah{
    private Int abc;
    public function foo()
    {
        abc = 21;
        Object anObject = new Object();
        anObject.setSomething(new Asdf{
            public void barfunc(){
                foobar();
            }
        });
    }

    public function foobar()
    {
        abc = 92;
        foo();
    }
 }

In the anonymous inner class defined by new Asdf we call¬†foobar(). If you invoked the function using this.foobar() it only looks for the class inside the anonymous inner class. If you don’t use ‘this’ the scope will start locally and then be expanded until the function is found.

3 thoughts on “Using this when referencing a member variable

  1. The nice thing about what you describe above is that references are resolved in increasing scope. If ‘abc’ isn’t found in local scope, it’s looked for at the level of the class, if it isn’t found in the class it’s looked for in the superclass.

    It’s probably an anti-pattern at this point but you’ll probably notice that constructors tend to take parameters with the same name as member variables, and assign them using ‘this.abc = abc’. I believe this happened initially because it was an easy way for an IDE to generate a constructor based on the member variables…

  2. @Jon

    I’m familiar with the increasing levels of scope, PHP4 back then implemented rudimentary OO – but only if you used ‘this’. If you didn’t use ‘this’, (I believe) global scope was automatically used in the case of a function.

    Ok… that’s interesting. So you’re saying that Java DOES allow you to have a parameter the same as a member variable. I’ve not seen that in any of the code I have been looking at recently but I remember seeing it back at university. It’s interesting because flash does not allow that to compile, saying that the reference is ambiguous. I hadn’t seen it on my recent run so I just assumed it had been evolved out of the language.

Comments are closed.