Mark Miller

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):

    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.

    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:

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

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

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


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.

Published May 09 2012, 12:00 AM by
Bookmark and Share


Daniel Shitrit

As always you make a great job -:)

May 30, 2012 10:44 AM

About Mark Miller (DevExpress)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2016 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners