Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring duplicate code into methods

Tags:

c#

resharper

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

like image 273
Shawn Mclean Avatar asked Dec 18 '09 20:12

Shawn Mclean


People also ask

What are the best practices for refactoring?

The best time for refactoring is before adding new features or updates to existing code. Doing so can help improve the product's quality. By cleaning the code base before adding new features or updates, it helps to make the product more robust and easier to use in the future.

Is duplicating code bad?

It's safe to say that duplicate code makes your code awfully hard to maintain. It makes your codebase unnecessary large and adds extra technical debt. On top of that, writing duplicate code is a waste of time that could have been better spent.

What is the purpose of refactoring code?

The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior. Computer programmers and software developers refactor code to improve the design, structure and implementation of software. Refactoring improves code readability and reduces complexities.


2 Answers

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.

like image 124
Josh Buedel Avatar answered Oct 24 '22 21:10

Josh Buedel


Extract Method.

like image 29
jason Avatar answered Oct 24 '22 20:10

jason