One of the built-in Checkstyle checks is RequireThis, which will go off whenever you don t prepend this.
to local field or method invocations. For example,
public final class ExampleClass {
public String getMeSomething() {
return "Something";
}
public String getMeSomethingElse() {
//will violate Checkstyle; should be this.getMeSomething()
return getMeSomething() + " else";
}
}
I m struggling with whether this check is justified. In the above example, the ExampleClass
is final, which should guarantee that the "right" version of getMeSomething
should be invoked. Additionally, there seem to be instances where you might want subclasses to override default behavior, in which case requiring "this" is the wrong behavior.
Finally, it seems like overly defensive coding behavior that only clutters up the source and makes it more difficult to see what is actually going on.
So before I suggest to my architect that this is a bad check to enable, I m wondering if anyone else has enabled this check? Have you caught a critical bug as a result of a missing this
?