Home > Code Analysis > Duplicate Real-time detection in Visual Studio

Duplicate Real-time detection in Visual Studio

December 20th, 2011

CodeRush Duplicate detection runs automatically inside Visual Studio IDE in a background thread. To enable it, go to the Duplicate Code options page in the Options Dialog and toggle its availability. Note that you should also have the Code Issues Analysis feature enabled. Once you enable detection, it starts scanning your solution for code duplication. You will see an animated icon in the lower right corner visually indicating the progress of duplicate code analysis:

CodeRush Duplicate Code editor icon

Hover over this icon to see a message hint with additional information:

CodeRush Searching For Duplicates Hint

If the analyzed solution does not have duplicate code, the icon state changes:

CodeRush No Duplicates Found Hint

If there is duplicate code in the solution, the following state of the icon is shown:

CodeRush Duplicates Found Hint

When you make changes to your source code, the analysis is suspended and it will be automatically resumed later, according to your analysis settings. If you want to manually resume the analysis, click the Scan Now link in the Waiting to Check hint:

CodeRush Waiting To Check Hint

Clicking the Options link will bring up the Duplicate Code options page.

Once the duplicate code is found, a code issue of a special type highlights the code duplication:

CodeRush Duplicate Code editor code issue

Hovering over the code issue will show you a Code Fix hint with the Duplicate Code item:

CodeRush Duplicate Detection and Consolidation Code Fix Hint

There are several interesting things in this hint related to the Duplicate Code code issue:

  • Duplicate code label
  • Navigation buttons
  • The “Why?” link
  • The “Report issue” link
  • Consolidation fixes

Duplicate code label

Hover over this label to see the duplicated code highlighted in the Visual Studio code editor:

CodeRush - Highlighting Duplicate Code Range

Navigation buttons

CodeRush Code Fix Hint Dup nav buttons

The “Next duplicate block” and “Previous duplicate block” buttons allow you to navigate between duplicate code blocks of a single cluster, which represent two or more duplicate code blocks of a single piece of code that is duplicated. Clicking one of these two buttons will navigate to another duplicate code block, open a file if necessary and show the Code Fix hint for this block with a mouse cursor positioned on the “Next duplicate block” button for further navigation.

The center “See all duplicates” button opens the Duplicate Code tool window, where you can observe all duplicate code found in the entire solution:

CodeRush Code Fix Hint See All Duplicates button

The “Why?” link

This link is shown when CodeRush is unable to offer an automated consolidation option. Consolidation of duplicate code is very challenging, and not all duplicates can be easily unified. Hover over this link to see why consolidation is not available, for example:

CodeRush Code Fix Hint Why Not Available link

If you think CodeRush should be smart enough to consolidate this block of code and its clones automatically, click the “Report issue” link and provide your feedback.

The “Report issue” link

CodeRush Code Fix Hint Report Issue link

Clicking this link will bring up the dialog for sending feedback to help DevExpress improve Code Consolidation when it is not available:

CodeRush Help Us Improve Duplicate Consolidation Dialog

The report will contain the duplicate code and a few other details that may help us better understand the challenge at hand.


If the duplicated code can be consolidated, you will see one or more target destinations where the code can be consolidated:

CodeRush Code Fix Hint consolidation fixes

Hover over each target consolidation link to see the preview hint of code editor changes and a visual diagram to review the duplicate code changes being made. In the consolidation hint, you can see how many duplicate code blocks exist and their structure, and how many classes and projects are involved in the duplication. The hint also shows the resulting target destination of the consolidated code. For example, consolidating to a new ancestor class in a new project:

CodeRush Consolidation Hint preview (New Ancestor target)

Click the link to apply the code consolidation at the selected location.

Hovering over the document bar will show you a similar hint with Duplicate Code issues:

CodeRush Document Bar Code Issue Hint

Want to learn more?

Products: CodeRush Pro
Versions: 11.2 and up
VS IDEs: any
Updated: Dec/20/2011
ID: C148

Similar Posts: