4

We have started to implement checkstyle as a way to provide a consistent code style across developers. One of the checks requires that all instance variables be references using "this.". I have not been in the habit of using "this.", and so have thousands of instances that need to be fixed.

Is there a tool or IntelliJ plugin that can just run through and add the appropriate code?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Phyxx
  • 15,730
  • 13
  • 73
  • 112
  • 2
    Why? It's redundant. Don't perform redundant work. Nobody wants to pay for it, and why should they? Modify your checkstyle configuration so as not to require it. Don't let tools tell you how to spend money, and don't be fooled that a 'consistent code style across developers' is essential to delivering a working product. It isn't, as long as the code is legible and maintainable. I have seen more time and money wasted on this issue than I care to think about. – user207421 Feb 04 '13 at 08:55
  • 1
    That's one of the more inane checks in checkstyle. Everyone uses an IDE with syntax highlighting. – millimoose Feb 04 '13 at 08:57
  • 1
    This answer explains exactly what you need: http://stackoverflow.com/a/19865423/1149414 – bluesman80 Dec 08 '15 at 11:06

3 Answers3

5

In eclipse: Window - Preferences - Java - Editor - Save actions - Configure... - Member access -

Here you check the "Use this" checkboxes.

On every save on code it will make the correction.

I just found this page.

It is a never too late to read for those who want to use eclipse efficiently. I guess there will be something similar to Netbeans and others as well.

CsBalazsHungary
  • 803
  • 14
  • 30
5

One of the checks requires that all instance variables be referenced using "this."

So 'checkstyle' is mistaken.

I have not been in the habit of using "this."

Quite right. It's redundant. Good for you.

so have thousands of instances that need to be fixed.

So you are mistaken.

You don't have any instances that 'need to be fixed'.

Don't do it. It's redundant. Don't perform redundant work. Nobody wants to pay for it, and why should they? Modify your checkstyle configuration so as not to require it. Don't let tools tell you how to spend money, and don't be fooled that a 'consistent code style across developers' is essential to delivering a working product. It isn't, as long as the code is legible and maintainable. I have seen more time and money wasted on this issue than I care to think about.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    +1 for voice of sanity. Pedantic crap that can be detected automatically pretty much never significantly affects how easy or not a codebase is to comprehend. Any gains to be made will be dwarfed by that one class that an intern once wrote that has loops nested seven levels deep that nobody dares touch, or JSF. If you do automatic code quality checks, focus on things like cyclomatic / dataflow complexity. Test coverage is probably a better proxy for "readability" as well. – millimoose Feb 04 '13 at 09:24
  • 1
    I will add that I started programmning in 1971 when there were all sorts of coding styles in existence, most of them personal, and most of them illegible to anyone except the author. Fortran programs without spaces; programs written all on one line ... the move towards coding standards was aimed at fixing *this,* and it did. I consider it a completely solved problem since about 1982, and I never let a software tool dictate how I spend my time or my money. – user207421 Feb 23 '15 at 23:41
1

One of the checks requires that all instance variables be references using "this."

IMO, the best solution is to raise this with the rest of the development team, and get agreement to turn that stupid check off. If adding redundant this keywords improves readability then you have to doubt the Java skills of the people reading the code.

If they pushed back, I'd be tempted to register my distaste thusly:

public class SomeClass {
  private String thisName;

  public String getName() {
      return this.thisName;
  }

  public void setName(String notThisName) {
      this.thisName = notThisName;
  }

  // and so on
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216