tags:

views:

164

answers:

3

I am using Resharper for refactoring. What is this type of feature called when you have the same code in around 5 different events being called but you want to replace them with a method call? And place that code in that method.

All done automatically.

Thanks

+4  A: 

Extract Method.

Jason
how do i get all of them into the method at once? The same code is found in multiple events
Shawn Mclean
I don't think that can be done. But you can still achieve it easily. Extract the method once, copy and paste the method call from that extraction in the other places and rename the parameters as necessary. Or, use Resharper to extract one method for each time the code is duplicated and then delete all but one of the methods and replace the calls to the method that remains.
Jason
The canonical use of Extract Method is extract two chunks of near-duplicate code each into its own method; use other refactorings to make the two methods identical; replace a call to Method2 with a call to Method1; delete Method2. Of course, run your unit tests between each refactoring. Repeat until all duplications are gone.
John Saunders
+2  A: 

I've been working on a Resharper plugin that does what you are asking. That is, it scans your code, searching for sections that can be replaced by an existing method call. A section can be a whole method or just a part of a method. When it finds one, the lightbulb pops up and offers to replace said section with a call to the existing method.

alt text

I call it AgentRalph. At this point it's not ready for production use, but I've been making a lot of progress and hope to make a release soon.

Josh Buedel
this is very nice man.
Shawn Mclean
A: 

See the C# CloneDR. While it doesn't replace redundant code with function calls, it does tell you where they are across very large system, and forms the essential abstraction (procedure body and parameters). The web link has example clone analyses for the C# equivalent of Hibernate (NHibernate).

Ira Baxter