Visual Studio Refactoring: Removal Method

Is there any Visual Studio add-in that can refactor the delete method?
Suppose you have the following method:

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

Or an option in which Result is not valid.

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

+4
source share
6 answers

If I understand the question, then Resharper calls this "built-in method" - Ctrl - R + I

+5
source

When it comes to refactoring, try ReSharper .

Just right-click on the name of the method, click on "Find usages" and refactor it until it finds the links.

And as mentioned above, the new version of ReSharper has the ability to embed a method. This should do exactly what you need.

+1
source

I would do it in the easiest way:

  • rename the ComputeResult method to ComputeResultX
  • rename the DoSomething method to ComputeResult
  • remove the DoSomething method (which is now ComputeResult)
  • rename the ComputeResultX method back to ComputeResult

Perhaps VS will show some conflict due to the last renaming, but will ignore it.

By "renaming" I mean: overwrite the name of the method and after it use the drop-down menu (Shift + Alt + F10) and select "rename". It will replace all occurrences with a new name.

+1
source

There are several products available to add additional refactoring options in Visual Studio 2005 and 2008, some of the best are Refactor! Pro and Resharper .

Regarding the removal method, the canonical book of Refactoring contains a description of how to do this gradually.

Personally, I follow a pattern of something along these lines (suppose that compilation and launch of unit tests occurs between each step):

  • Create a new method
  • Delete the body of the old method, change it to call the new method
  • Find all the references to the old method (right-click the name of the method and select "Find all references"), replace them with calls to the new method.
  • Mark the old method as [Deprecated] (calls to it now display as warnings during build)
  • Delete old method
+1
source

You can also right-click the name of the method and click Find All References in Visual Studio.

I personally would just do CTRL + SHIFT + H to Find & Replace

0
source

ReSharper is certainly an embedded VS 2008 for refactoring. However, he does not perform 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 remove the definition that was DoSomething. This is almost one step.

However, perhaps this can do it in one step . If I read it correctly.

0
source

Source: https://habr.com/ru/post/1276321/


All Articles