Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: Most useful refactorings [closed]

People also ask

How do I use refractor in Eclipse?

Refactoring using EclipseRight clicking on a Java element in the Package Explorer view and selecting Refactor menu item. Right clicking on a Java element in the Java editor and selecting Refactor menu item. Selecting a Java element in either the Package Explorer view or Java Editor and clicking Shift + Alt + T.

What is the use of refactor in Eclipse?

AutoRefactor is an Eclipse plugin to automatically refactor Java code bases. The aim is to fix language/API usage in order to deliver smaller, more maintainable and more expressive code bases.

How do I undo refactoring in Eclipse?

Normally you can achieve this by just pressing undo. If this is not possible for any reason Eclipse will warn you. In case of emergency: Revert by using your VCS :-) D.R.


It is an interesting question. I know what works for me and it is interesting to see what others use.

I decided to take a more scientific approach to determine the most commonly used refactoring commands. Eclipse has a Usage Data Collector (UDC) feature built in. The data is publicly available. I took the data and extracted the following graph which shows the most commonly used editing commands (without navigation commands).

alt text http://img.skitch.com/20091207-bmcng36rjy837sqmcx58b85age.gif

However, I am a strong believer in "Save Actions" for formatting and organizing imports (read my article about it), so I wouldn't count those. I would also remove the commenting actions. The picture looks like this: alt text http://img.skitch.com/20091207-ieas1mk5114fwitucqkqxyw6t.gif


Rename - because giving things meaningful names is the best way to write self-documenting code. Shift+Alt+R

Extract method - whenever a method gets too long. Shift+Alt+M

Extract constant - because magic numbers are bad. Shift+Alt+T (refactoring menu, there's no direct shortcut).

Inline/introduce variable - to remove clutter from methods. Shift+Alt+I (inline), Shift+Alt+L (introduce)


My favourites (in order of using):

  1. Rename (Alt-Shift-R, or Ctrl-1 for faster in-file renaming)
    Good renaming variables,methods,etc. without side effects.
  2. Extract Variable (Ctrl-1, Alt-Shift-L)
    Good for splitting a quick-made-100-character-line to separate steps.
  3. Extract Method (Alt-Shift-M)
    Create a method out of some code without any side effects.
  4. Split Variable Declaration (Ctrl-1)
    Good when you initialize a variable at the declaration and now find out, that the initialisation needs to be in a try- or if-block.
  5. Change Method Signature (Alt-Shift-C)
    The handy Swiss Army Knife of method signature manipulation, including default values for new parameters.
  6. Pull Up/Push Down Pull methods and variables to a generic interface or superclass or push it down to a subclass
  7. Extract Interface/Superclass
    Extract an interface or a superclass out of the current class. Very handy.

My favorites:

  1. Renaming
  2. Pull up/Push Down
  3. Extract Method

The most popular refactorings have been stated, and I entirely agree with them.

Code formatter (Source, Format or Ctrl Shift F) is one of the features of the IDE I use very often. True, it is not refactoring, but it improves code readability while maintaining your coding style: simply head to Preferences, Java, Code Style, Formatter and tell Eclipse how you want your code to look!

Generate Getters and Setters is also a feature I find to save time when writing Java beans.


Eclipse has perhaps the least refactorings for all the popular IDEs. You might consider Netbeans or IntelliJ (Community edition is free). Conversely Eclipse has possibly the best debugger. ;)

I use refactoring as I write the code (which I have found speeds up writing by about 15%) so IntelliJ's ability to refactor code which does not compile very useful for me. The other IDEs may support this now (does any one know?) I find IntelliJ's smart complete is a fair bit smarter as well.

I have tested retyping a files from printout (originally written in eclipse) and found I used 30% less keys and 50% less mouse movements when typing the file with IntelliJ (compared with Eclipse) I would estimate Netbeans to be somewhere in the middle.