Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What is considered more readable, "this" or no "this"

You don't really need to write the "this" keyword in Java. But is it better to do so anyway? Does it make sense to homogenise your style, i.e. if you use "this" once, you use it every time it's implied? Or is there a place where you would always use it and others where you never use it?

like image 843
Soyuz Avatar asked Feb 18 '13 09:02

Soyuz


People also ask

Should we always use this in Java?

In answer to "Are there any cases where you should always use this ?" You should use it when it is needed to avoid ambiguity, for example if there is another variable with the same name in scope.

Can I omit this in Java?

Yes, that can happen.

Why do we use this in Java?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this .


3 Answers

The general consensus is that you should use this only when it is necessary and not at any other time.

private String param;
public Construct(String param) {
  // Usually the only place you need to use this.
  this.param = param;
}

// A less common use of this
public Function() {
  synchronized(this) {
    // Some synchronized stuff.
  }
}
like image 145
OldCurmudgeon Avatar answered Oct 11 '22 08:10

OldCurmudgeon


As a rule I tend not to use it - if you can reduce redundant code then all the better.

There are however three places that I can think of where the this keyword cant be avoided:

  • Constructors (delegating to another constructor in the same class)

    public MyClass() {
        this("Default Parameter");
    
  • Synchronizing on the current object

    synchronized(this) {

  • Passing the current object to another class

    public void methodOne() {
        anotherClass.doSomething(this);
    

You sometimes need it in constructors where the field name is the same as the parameter, but this isnt really mandatory as you could simply rename the paramter:

public MyClass(String data) {
    this.data = data

Other than these I cant think of too many other scenarios where I'd use the this keyword. I have seen it overused (on every method and field reference) and this can make the code very hard to read.

Use it only when you have to, or when you believe that it enhances code readability.

like image 35
Sean Landsman Avatar answered Oct 11 '22 08:10

Sean Landsman


As a general rule you should avoid redundant syntax wherever it may arise. You will read lots of opinion to the contrary, mostly referring to a thoroughly mythical programmer who doesn't know about member variables, doesn't know what parentheses are for, and doesn't remember the rules of operator precedence he was taught in third grade. I've never met him in 40 years. That isn't enough to justify disfiguring your code for him on the assumption that he will (a) not understand it and (b) therefore break it. I've never seen that happen at all.

What I have seen is code produced by such a person. That's not a reason to dumb down your own code for him. The occasions on which someone has actually gone as far as to incorrectly rewrite a piece of code of mine are exactly two: once in 1979, where someone refactored a grammar to remove the operator precedence, which was dumb, and he shouldn't have done it, and another time in about 1992, but in both cases there is no way I could have written the grammar or the code that would have prevented it.

like image 41
user207421 Avatar answered Oct 11 '22 07:10

user207421