views:

660

answers:

6

Is there any Visual Studio Add-In that can do the remove method refactoring?
Suppose you have the following method:

Result DoSomething(parameters)  
{  
    return ComputeResult(parameters);  
}

Or the variant where Result is void.

The purpose of the refactoring is to replace all the calls to DoSomething with calls to ComputeResult or the expression that uses the parameters if ComputeResult is not a method call.

+1  A: 

When it comes to refactoring like that, try out ReSharper.

Just right click on the method name, click "Find usages", and refactor until it cannot find any references.

And as dlamblin mentioned, the newest version of ReSharper has the possibility to inline a method. That should do just what you need.

Lars Mæhlum
+1  A: 

I would do it the simpliest way:

  1. rename ComputeResult method to ComputeResultX
  2. rename DoSomething method to ComputeResult
  3. remove DoSomething method (which is now ComputeResult)
  4. rename ComputeResultX method back to ComputeResult

Maybe VS will show some conflict because of the last rename, but ignore it.

By "rename" I mean: overwrite the name of the method and after it use the dropdown (Shift+Alt+F10) and select "rename". It will replace all occurences with the new name.

Biri
A: 

You can also right click the method name and click "Find all References" in Visual Studio.

I personally would just do a CTRL + SHIFT + H to Find & Replace.. or am I missing something here? :D

Rob Cooper
A: 

ReSharper is definitely the VS 2008 plug in to have for refactoring. However it does not do this form of refactoring in one step; you will have to Refactor->rename DoSomething to ComputeResult and ignore the conflict with the real ComputeResult. Then delete the definition which was DoSomething. It's almost one step.

However maybe it can do it one step. If I read that correctly.

dlamblin
+1  A: 

There are a few products available to add extra refactoring options to Visual Studio 2005 & 2008, a few of the better ones are Refactor! Pro and Resharper.

As far as remove method, there is a description in the canonical Refactoring book about how to do this incrementally.

Personally, I follow a pattern something along these lines (assume that compiling and running unit tests occurs between each step):

  1. Create the new method
  2. Remove the body of the old method, change it to call the new method
  3. Search for all references to the old method (right click the method name and select "Find all Reference"), change them to calls to the new method
  4. Mark the old method as [Obsolete] (calls to it will now show up as warnings during the build)
  5. Delete the old method
Wedge
+5  A: 

If I understand the question, then Resharper calls this 'inline method' - Ctrl-R + I

Will Dean