Duplicate Real-time detection in Visual Studio
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:
Hover over this icon to see a message hint with additional information:
If the analyzed solution does not have duplicate code, the icon state changes:
If there is duplicate code in the solution, the following state of the icon is shown:
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:
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:
Hovering over the code issue will show you a Code Fix hint with the Duplicate Code item:
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:
Navigation 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:
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:
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
Clicking this link will bring up the dialog for sending feedback to help DevExpress improve Code Consolidation when it is not available:
The report will contain the duplicate code and a few other details that may help us better understand the challenge at hand.
Consolidation
If the duplicated code can be consolidated, you will see one or more target destinations where the code can be consolidated:
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:
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:
Want to learn more?
- Duplicate Detection and Consolidation Overview
- Duplicate Detection Visual Studio tool window
- Duplicate Detection Using the Stand-alone Application
- Duplicate Consolidation inside Visual Studio
- Duplicate Detection analysis options
- Duplicate Detection in CodeRush versus Code Clone Analysis in Visual Studio 2011