• Share this article:

He who lives by the quick fix…

Thursday, November 10, 2005 - 10:48 by Wayne Beaton

I write software interatively and almost always top-down. As such , I tend to rely heavily on the refactoring tools provided in the Eclipse JDT. But I’ve really started to lean on the Quick Fix feature.

When you write code from the top-down, you always start with a lot of errors. Ideally, I start by building JUnit unit tests and then spend the rest of my time making them work. I’ve spoken with a lot of folks who try very hard to avoid ever having errors show up when they hit ‘save’; I don’t understand these folks because the errors save you time.

When a red line appears under some part of your Java code, that means there’s an error. If you put the cursor in the code with an error and hit CTRL-1 (on Windows and Linux) to get a context menu with all the possible ways to fix the error. I most commonly use the Quick Fixes to create a new class or new method (since I build things top-down, I tend to use these things before I actually build them). The Quick Fix mechanism generally does a pretty good job of determining things like an appropriate super class or interface for a class, return values and parameters for methods, and such. It builds a pretty good skeleton of the thing you need to make.

There are also Quick Fixes that import missing classes, add exception handling code, or help you fix typing mistakes in class names. It’s all quite wonderful for a lazy person such as myself.

A few days ago, I was building an anonymous class and discovered a new quick fix. I built the class based on an interface that I hadn’t yet added and added a method that doesn’t exist (as is my normal approach). I used the quick fix to build the interface. Building the interface made both errors go away. On a whim, I put the cursor over the method I had added to my anonymous class (which hadn’t yet been added to my new interface) and hit the CTRL-1 not really expecting much to happen. However, the menu did contain something useful: a quick fix to create my method in the super type (the interface).

So you can even quick fix things that don’t even appear to be broken. Very cool.