The beauty of a Quick Fix

Eclipse has a nice feature called Quick Fix where if you type in something that doesn’t compile, it thinks of things you could do to fix it.  For instance, if you type in the name of a class it can’t find, there’s a little X with a light bulb (which I translate as “Something’s wrong but I have an idea how to fix it”).  I can then click on the X-with-the-light-bulb (or better yet, press Ctrl+1) and choose a quick fix from the list.

I had known for several weeks about being able to click on the X/light bulb icon, but having to stop typing and go click over there was enough impedance that I generally would just do the work myself.  Just in the last day or so though, I found out about the Ctrl+1 shortcut…

Let me demonstrate how this helped me today.

I had renamed my MessageSender class to MessageSenderImpl, and then performed Extract Interface to recreate MessageSender as an interface.  After I helped Subversion understand, I was then ready to create a new implementor of the interface: a UserSvcAwareMessageSenderImpl.

  1. I started UserSvcAwareMessageSenderImpl’s sendMessage() implementation with the code from the plain MessageSenderImpl:
  2. I changed the name of the class it was instantiating to UserSvcAwareMessageCreator — which doesn’t exist yet.  I got the red underscore:
  3. I press Ctrl+1 to pop up the Quick Fix list:
  4. “Create class ‘UserSvcAwareMessageCreator'” is already highlighted, so I just hit Enter.  This pops up the Create New Class dialog, where the defaults are fine, so I hit Enter again.
  5. Eclipse has put me in the new UserSvcAwareMessageCreator class.  I click on the TODO comment and press Ctrl+F1, which comes up with the single suggestion “Remove task tag”:
  6. I hit Enter and the comment is gone (Click, Ctrl+1, Enter is more efficient than my usual Click, Home (to get to beginning of line), Shift+DownArrow, Delete).
  7. I intend to pass session.createTextMessage a String that’s passed in to UserSvcAwareMessageCreator’s constructor, but I haven’t created the field nor the constructor yet.  I type the code anyway:
  8. I press Ctrl+F1…
  9. …and down-arrow to “Create field ‘messageText'”:
  10. I place the cursor on the new messageText field, press Shift+Alt+S (could instead have pulled down the Source menu but I’m partial to keyboard shortcuts), and choose Generate Constructor using Fields…
  11. And there we are, ready to fill in the rest of the createMessage() method:

Using the Quick Fix feature, I got all this set up in way less time than it took to type about it!  :)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s