There is a class of refactorings that I am using very often in Eclipse and
since I recently learned a bit more about it, I thought I would share with the
rest of the community.

The refactorings are Extract Method (Alt-Shift-m) and Extract Local Variable

Their use is fairly straightforward, but I discovered by accident recently
that Eclipse is actually more clever than I thought:  when the code segment
you are trying to replace with a method is found at several locations within
your class, the refactoring is automatically applied to all of them.

Here is an example where the three highlighted lines are also repeated in the
method above:

Before After

you can do the same by introducing a local variable to capture an expression
repeated several times:


Before After

Finally, as a curiosity, notice that you can do the opposite as well: 
remove a local variable and replace it with the expression it was assigned to:

Before After

Admittedly, this refactoring is fairly rare but I have used it a few times
already for J2ME programming, where every byte counts.  If you see a local
variable that is only being used once in your code, you should probably consider inlining it.

These refactorings in themselves are pretty benign until you learn their
keyboard shortcuts:

  • Alt-Shift-m for "Extract Method"
  • Alt-Shift-l for "Extract Local Variable"
  • Alt-Shift-i for "Inline Local Variable"

Memorize these shortcuts and very soon, you will be using them everywhere.