Categories
java

Eclipse Tips and Tricks, #1…

Yes, I’m finally using Eclipse. I tried to use it about 6 months when 3.0 came out, but just couldn’t do it. At my old job nobody used it. Hardly anybody even used an IDE. It was all cmd-line and vim. I was very productive in that environment. Now, at my new job, Eclipse is a requirement.

Therefore, I’m starting this new category, “Eclipse Tips and Tricks”. Mostly, it will be asking how do I do such and such. So I’ll be looking for feedback on the tips and tricks I want to use. I’ll also update each entry when I figure out how to do the various things I’m asking about. That way, I’ll have something to go back and look at. I guess I should move this stuff to a wiki, but that can be a project for another day.

Drum roll, please……..

First tip I need.

How can I quickly remove a method from a class? Or, what is the best way to remove a method from a class? The very generic gui way would be to simply high-light the method in my editor and click my Delete key. Surely, there is a quicker way……..

UPDATE:
The best way that I can tell is to use the Outline View. A simple right-click of the method name and then selecting ‘Delete’ from the context menu does the trick. Thanks to Chad Bakerfor that advice and also to Joakim Erdfelt for his tip. It will show up shortly in a keyboard shortcut Tip and Trick.

8 replies on “Eclipse Tips and Tricks, #1…”

Hmm… there are only 3 methods that I can think of, and two of them are pretty much the same.

1. Highlight method in body of code and press delete (not sophisticated at all).
2. Left-Click on method name in Outline view and press delete.
3. Right-Click on method name in Outline view and select delete.

I’m not deleting methods that often, maybe I should start and then I’d find out if there’s an easier way.

Good thing about Eclipse is that if you delete the wrong method (and don’t realize it right away), you can Right-Click on the class name and use the “Restore from local history…” option to get it back. Very nice.

If you are a keyboarder then you can use the “Select Enclosing Element” command (Alt-Shift-Up) and highlight a code block. Repeat for more block levels. (When you have a suitable block highlighted, delete it.)

Also, if you want to remove the method but not the logic (example: A simple 1 liner method body which is lightly used) you could Inline the method (Alt-Shift-I) into where it was used. (NOTE: If the method is not being used, it’s simply removed)

I am going to save everyone’s trouble by avoiding the topic of “how one can be efficient without an IDE”, even though I am really tempted to.

Another point is that you should look into either IntelliJ, or JBuilder as a better IDE. Eclipse can give people false idea of what an IDE can do (or not trying too hard in some cases).

I believe the way you delete a method in vim is “dd” command to delete line? If that is correct, ctrl+D in Eclipse does the same for you.

A more interesting question would be that why do you delete the methods, because that can decide what you really want Eclipse to do for you:
1. Unused method: If you turn on the “unused code” warning in Eclipse (as useful as this feature is, it is turned off by default, go figure). A light bulb will popup on the method name. You can click on it (or put your cursor on that line and press ctrl+1), then it will delete the whole method for you.

2. Inline method: As posted by Chad.
3. Move methdo: that is another refactoring.

I think the IDE nowadays focus more on intentions, rather than actions. And that is what is different of them from the traditional editors

Sorry I have got enough spam so I don’t put my real email here.

Cheers.

Thanks for the input Shane. And no problem with not leaving a “real” email address.

So you’re saying Ctrl+1 in the editor will remove a method if I’m on the line that the method was declared on?

Actually, this is one of the few features that Eclipse has done right on basic Java developement.

Ctrl+1 is something called “Quick Fix”. Whenever there is an error or a warning, and your caret is on that error (which has squiggley underneath it), you can press Ctrl+1, which will prommpt you suggestions for fixing the error or warning.

For example, if you type in ArrayList list = null, and it shows ArrayList is not imported, you can just press Ctrl+1, and it will import it automatically for you. If there are more than one match (for the case of List class), it will ask you which one you want.

Of course, that is not a good example because in Eclipse, you can just type ArrayLi then press Ctrl+space, it will give you a list of classes that starts with “ArrayLi”, no matter if it is being imported or not, and it will add the import automatically for you.

Ctrl+, and Ctrl+. will let you go through all your errors and warnings in the current file. So you can fix all of them very quickly with the help of Ctrl+1.

Whenever you see an error, try this action, and it can become very handy.

Cheers.

Folks, save yourself the trouble of having to discover all the tips and tricks. Just get a subscription of “Essential Eclipse – The Visual Tutorial” and you’ll get them delivered to you on a monthly basis and in animated form.

Carlos

Leave a Reply

Your email address will not be published. Required fields are marked *