Consolidating Duplicate Code in Visual Studio – Easy with CodeRush

On Twitter last week a developer mentioned a competing developer tool, inferring its clone detection and consolidation abilities were similar to what we were already shipping with CodeRush. Skeptical (I had looked into this tool once before months ago), I decided to dig deeper and found a video showing how to consolidate code with the tool. I watched a developer show step-by-step how to manually consolidate duplicate code found by the tool. Steps included determining the differences between the blocks of code and then manually removing them in each of the duplicated methods, then extracting one of the methods, then copying a method call and pasting it over each of the other duplicate methods. The entire process took nine minutes and 43 seconds. Wow.

In the video the developer only worked with two duplicates, and those duplicates were both located in the same class; you could see the proposed solution would become incredibly difficult and error-prone to perform manually for duplicates in larger numbers or spread across classes.

I consider the video a demonstration of how our ancestors used to consolidate code, back in the old days when dinosaurs roamed the earth.

Here’s how you do it today with CodeRush:

  1. Open the project inside Visual Studio. The screenshots in this post are using the same source code sample I saw in the demo.
  2. CodeRush lists duplicates on the Duplicate Code tool window. Here are the duplicates CodeRush has found (on an extremely granular setting – you can adjust settings in Options):

    DuplicatesFound
    In the screenshot above I’ve circled the important differences.
  3. Double-click one of the duplicates. This takes you to the code and brings up the Consolidate Duplicate Code UI.

    ToTheCurrentClass
    The preview hint shows you the duplication will be turned into a single method inside the class UINameHelper.

  4. Select “to the current class”. The Target Picker (with a handy preview) lets you decide where to insert the new method:

    TargetPicker
  5. The Up/Down arrows change the target location.  I press Enter to accept the position and generate the consolidated method. Here it is:

    ExtractedConsolidatedMethod
  6. Let’s rename it to “BuildUINameFrom”. CodeRush updates the calling methods automatically:

    ConsolidatedMethodCalls
  7. Then I rename the parameters in the consolidated method:

    RenameParameters

And I’m done.

Elapsed time: About two minutes (which includes two full runs of all the test cases). Kids, let me tell you why it’s important to work in the future rather than the past: Because spending ten minutes making high-risk manual changes is senseless when you can do it safely in seconds with CodeRush.

Here’s the video showing how fast it is:

Note we start the video from an empty Visual Studio (no solution loaded). As we load the solution watch how quickly the Duplicate Code window fills up. You can also see the Test Runner collect unit tests and populate itself in seconds as well. Nothing is faster than CodeRush.

CodeRush is still the only tool that can consolidate your duplicate code automatically across classes and projects. For more information on CodeRush, click here.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.