7

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

1
  • It's been 13 years ... and resharper still does not provide class context duplicate code refactoring ... Commented Mar 15, 2023 at 9:31

3 Answers 3

14

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
(source: landofjosh.com)

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Extract Method.

5 Comments

how do i get all of them into the method at once? The same code is found in multiple events
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.
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.
@jason, I don't think this is a good way. The advantage of the "extract method" discover what code is duplicated and extract the method by itself is not delegate this discover for the developer. This need to be done by the tool, like Eclipse do.
this does not identify duplicated occurrences in the whole Class unfortunately. Only on the same method (same context?). Pitty. :)
1

See our 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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.