Fog Creek Software
Discussion Board

Refactoring support in IDEs

Someone mentioned refactoring support in IDEs recently.  I haven't looked into it yet but I know Eclipse makes a big deal out of it.  I have always been able to refactor code using standard IDE or editor.

Can anyone summarize the functions of these new IDEs that facilitate refactoring?

name withheld out of cowardice
Wednesday, February 4, 2004

(these actions are paraphrased, as I don't have Eclipse in front of me)

Highlight a section of code, choose "create new method from selected", and it creates a new method for you that contains that code.  It also creates the needed arguments, and calls that method.  One click, and you can move from working ugly code to working not-so-ugly code.

Andrew Hurst
Wednesday, February 4, 2004

You alter the name of a public method in one place and everywhere where this method is called is altered automatically for you, with your consent, and at the same time the changes are saved in a history for you, so if you regret it, you can undo the changes later :P

Basically it is that for me :)

Wednesday, February 4, 2004

So, why not simply use search and replace in all files?

Wednesday, February 4, 2004


That sounds useful.

name withheld out of cowardice
Wednesday, February 4, 2004

One of the tenets of refactoring is that the code must change without making any changes to the behavior of the system.  Simply doing a global search and replace is rather error-prone in this regard.  Consider a situation in which two classes have a method with the same name.  You want to change one of them for clarity.  You could search and replace, and you would end up changing all of them.  You could search and manually check each instance, giving you a pretty good chance of only changing the methods that break, but that would take time, and is still error-prone, though it would likely produce a compile-time error.  Likewise, you could just change the declaration and then follow the compile-time errors and make the appropriate changes, which would pretty much eliminate the possibility of error, but be time consuming.  *OR* you can use a refactoring IDE that does all of the lexical analysis and type checking for you, and have it done in a split second.

Now, given that I write one of these refactoring tools, I'm a little biased.  I agree completely that a refactoring such as "Rename Method" is pretty much a smart search-and-replace, but it's that smartness that makes it worthwhile.  It really does save you time and energy.

It's also odd that most people associate refactoring with renaming things.  If you read Fowler's book, he concentrates a lot more on moving things around to conform with best-practices design patterns than on simply renaming things.  Anyway, there's my two cents...

Wednesday, February 4, 2004


Now that you explain it that way it also sounds useful.  I like this idea even without refactoring.  As you point out renaming methods isn't refactoring but I am always for changing method names to reflect, more accurately, their purpose.

Anything else?

name withheld out of cowardice
Wednesday, February 4, 2004

IntelliJ Idea supports "real" refeactorings in addtion to the simple, but immensly useful things, like extract method, move class, and rename class/method/variable.

Here are some examples:

- Replace contructor with a factory method. This will go through all the places where you new an object and replace with a call to the factory method.

- Pull member up/push down - moves a member method within class hierarchy

- Extract superclass - creates a base class with selected methods and makes current class inherit from it.

- Extract inteface - creates an interface based on selected methods in the current class. After creating the interface, it can go and replace all usages of the class with interface. (I want to do this in a C# project and it's going to be a huge pain to do manually)

- Replace inheritance with delegation - just what it says

There are a few others. In addtion, Idea has notion of inspections and intentions. Inspections analyze your code for conformance to rules, and intentions suggest ways to fix the errors. There's a plug-in to Idea that adds a bunch of these inspections so you can customize the checks to your coding best practices (e.g. style, naming, Java idiom usage, etc...).

Wednesday, February 4, 2004

"refactoring" is a whole group of stuff.

Basically, I see "refactoring support" as anything that allows the developer to take existing code and safely change it to a better design that still works as it did before.

Extract Base Class:
You have a big heavy class A and you need another class B that is similar, but different.  You use a refactoring tool to pull all the common fields and methods in ClassA into a new base class BaseAB, ClassA is changed to inherit from BaseAB.  Now you can make your ClassB inherit from BaseAB as well, and you only have to implement the features unique to ClassB.  All your old code still compiles and works, because ClassA still has all the same methods it used to through inheritance.  You may want to manually change some functions that take a ClassA parameter to a BaseAB parameter if they do not require features specific to ClassA.

A lot of refactoring is aimed at reducing redundant code that does the same thing.  For example...

Simplify Conditional:
You have the conditional (FILE_EXISTS && FILE_READABLE && !FILE_IS_LOCKED) in lots of different places.  A good refactoring tool will let you highlight one instance of that conditional and pull it out into a FileIsReadyToRead() function.  It may even change all other instances of that same conditional to use the function.  If it's really smart, it may even offer to change all boolean-equivalent instances to use the function.

All of this can currently be done with search and replace, but it is prone to error.  Search/Replace is based solely on regular expressions.  Refactoring is context-sensitive.

Richard P
Wednesday, February 4, 2004

Igor- IDEA sounds pretty good.  I might look into it.

Richard- You are saying what a good refactoring tool should do.  DO you know of any tools that do this?  BY saying "tool" do you imply that these are not IDEs but tools which I might be able to use with my favorite editor?

name withheld out of cowardice
Wednesday, February 4, 2004

Just to clarify, for the people who don't use them, the IDEs that we're talking about don't have some sort of generalized ability to do any refactoring you can think of.

What they do have is automatic functions to do *some specific refactorings*. Generally the most common ones.

So it's not like you can just pull out Fowler, turn to page 160 and have your IDE "Remove Middle Man". Either the IDE will support the "Remove Middle Man" refactoring, or it won't.

When evaluating the refactoring capabilities of these IDEs you need to get a complete list of which refactorings the IDE supports and map it against the ones that you commonly use.

Bill Tomlinson
Wednesday, February 4, 2004

"Someone mentioned refactoring support in IDEs recently.  I haven't looked into it yet but I know Eclipse makes a big deal out of it."

Eclipse's implementation is to provide what IDEA did several years before, but not to do as good a job.

Try renaming a class in Eclipse via the refactoring menu. "Argh, but if I rename the class then the class and filename don't match" it whines. Then change the freakin' filename too you dumbass!

Walter Rumsby
Wednesday, February 4, 2004

...and remove the old file from and add the new one to CVS...

Chris Winters
Wednesday, February 4, 2004

Refactoring tools can either be the IDE themselves or add-ons to the IDE.  In order for a refactoring tool to be any good, it must either have its own parser for the language or hook into the IDE's parser, so you generally don't find them as standalone products.

Richard P
Wednesday, February 4, 2004

When Fowler et. al. did the smalltalk refactoring browser, they discovered that no one used it.... Then they took the same logic and made it a plugin to a popular smalltalk editor, and everyone loved it.  Economically, "standalone" refactoring tools don't make any sense, because no one would use it.  Integrate refactoring into an IDE though, and people love it.

This principle pays my bills. :)

Wednesday, February 4, 2004

The original refactoring PhD thesis was by  John Brant:

The original refactoring tool was:

If you want to know about refactoring, a good place to start is:

Having automated support for refactoring makes a world of difference.

Dafydd Rees
Thursday, February 5, 2004

Aren't there any refactoring plugins for Visual C++ ?

Marco Bertini
Thursday, February 5, 2004

Let me rephrase what I said here:

The original refactoring PhD thesis was by  Bill Opdyke:

John Brant did the first refactoring tool.

Dafydd Rees
Thursday, February 5, 2004

*  Recent Topics

*  Fog Creek Home