44

Every now and again, I make use of the Eclipse refactoring feature. Some techniques are more obvious then others and some I never tried.

What refactoring is most useful for you and why?

Note: I find this presentation very useful, perhaps because it is example driven therefore easy to understand:

"Refactoring for everyone - How and why to use Eclipse's automated refactoring features"

Edit: This article is useful as well (Thanks jitter)

Explore refactoring functions in Eclipse JDT

Adrian
  • 6,013
  • 10
  • 47
  • 68
  • http://www.ibm.com/developerworks/opensource/library/os-eclipse-refactoring/index.html – jitter Nov 26 '09 at 17:13
  • 2
    The reference for refactoring is Martin Fowlers book "Refactoring: Improving the Design of Existing Code". Eclipse implements a lot of these - as other IDE (netbeans, IntelliJ) - to me it's on of THE strengths of an IDE and a reason to not start programming Java without one. – Andreas Dolk Nov 26 '09 at 17:15
  • 5
    Subjective, doesn't have a single answer, topic should be marked CW. – BalusC Nov 26 '09 at 17:18
  • 7
    There's no right answer to this, I suggest to put it as a Community wiki – Nettogrof Nov 26 '09 at 17:19
  • I would rephrase the question - is there any plugin for Eclipse better or as good as Eclipse's "native" refactoring? – Trick Dec 01 '09 at 15:35

13 Answers13

52

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

zvikico
  • 9,765
  • 4
  • 38
  • 49
30

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)

abyx
  • 69,862
  • 18
  • 95
  • 117
20

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.
Hardcoded
  • 6,476
  • 2
  • 22
  • 20
  • +1 for a few refactorings I don't know but like. – Spina Dec 02 '09 at 21:55
  • Ctrl 1 does not work for me. – Koray Tugay Nov 13 '13 at 13:27
  • 1
    for splitting the variable declaration, could you please mention that for moving the declaration outside of a containing code block (e.g. outside of a try-catch block) one can use the Alt+Up-Arrow combination? I was looking for this, did not find it here, but found elsewhere I thought would be good if you included that in your answer. – Peter Perháč May 18 '15 at 13:52
7

My favorites:

  1. Renaming
  2. Pull up/Push Down
  3. Extract Method
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
5

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.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • I definitively use most CTRL+SHIFT+7 (or /) for (un)commenting lines. – Trick Dec 01 '09 at 15:37
  • 1
    FYI - (and maybe you know or can't use it for whatever reason)...you can setup 'Save Actions' to auto-format for you (amongst other things). I find this helpful insofar as not having to manually format via ctrl+shift+f – javamonkey79 Dec 07 '09 at 23:58
3

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

My favorites are:

1) Rename - It works on method names, variable names, class names, fields-- really anything with a name.
2) Convert Anonymous Class to Nested - Helps with debugging, lets you reuse logic (such as a comparator) that you only thought that you'd use in one place.
3) Convert Member Type to Top Level - Frequently after making an anonymous class into a nested class I discover that the class is useful elsewhere. This refactoring is perfect then.

Spina
  • 8,986
  • 7
  • 37
  • 36
1

What I use the most is Rename, Extract Method and Change Method Signature, in that order.

Yishai
  • 90,445
  • 31
  • 189
  • 263
1

CTRL + 1 on a red lined section, i.e. quick fix.

jeff porter
  • 6,560
  • 13
  • 65
  • 123
1

I like the Extract Method (Alt+Shift+M), and since 3.6M1, it now handles selections that contain continue statements.

To preserve the semantics of the existing code, the selection needs to include the last statement of the loop. In the extracted method, continue statements are changed to return:

Extract method refactoring with continue http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-continue.png

For a selection that would need multiple return values in the extracted method, Eclipse now lists the conflicting variables in the error message:

Extract method refactoring with an ambiguous return value error http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-multiple-return-values.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I use :

1- Rename - to correct better method name

2- Move - to organize my package in better ways, like when I start my project, it was so small so no need of a io package, but now yes.

3- Generate Comments -when I create a .class avoid me to re-copy the GPL license, etc

4- Correct Indentation - to keep my code readable.

Nettogrof
  • 2,116
  • 2
  • 15
  • 22
0

It's also worth reading this research: How do API evolve? A story of refactoring. by D. Dig and R. Johnson.

The authors noticed that 80% of the changes were refactorings and classified them. Here is the abstract:

Frameworks and libraries change their APIs. Migrating an application to the new API is tedious and disrupts the development process. Although some tools and ideas have been proposed to solve the evolution of APIs, most updates are done manually. To better understand the requirements for migration tools, we studied the API changes of four frameworks and one library. We discovered that the changes that break existing applications are not random, but tend to fall into particular categories. Over 80% of these changes are refactorings. This suggests that refactoring-based migration tools should be used to update applications.

ewernli
  • 38,045
  • 5
  • 92
  • 123
-3

"It's also worth reading this research: How do API evolve? A story of refactoring. by D. Dig and R. Johnson.

The authors noticed that 80% of the changes were refactorings and classified them. Here is the abstract ... "

80% of the BREAKING changes were observed to be refactorings. The refactorings themselves formed only 20 - 30% of the API changes..

Raja
  • 2,846
  • 5
  • 19
  • 28