DDC - Duplicate Detection and Consolidation
Duplicate code can be problematic in solutions because:
- A bug could exist in each of the duplicate code blocks, making it harder to fix and increasing the chances that one of the bugs escapes correction and resurfaces on your customer’s machines after the corresponding bug is fixed;
- Duplicate code is less flexible and harder to extend;
- More code takes more time to read and understand, making it harder to bring new devs up to speed.
CodeRush finds duplicate code quickly (either in a low-priority background thread while you work, or in a high-priority foreground thread while you wait), and also lets you consolidate many forms of code duplication automatically.
The icon at the bottom-right of the text editor indicates your solution contains two or more blocks of duplicate code that can be
consolidated into a single block.
Click this icon to open the Duplicate Code tool window to see all duplicates found in the entire solution.
You can consolidate found duplicates directly from the tool window. Right click the desired cluster and choose the target location for the consolidated code from the context menu.
You can also start a search from the Duplicate Code tool window. Just click the Refresh icon in the upper left to refresh the results. The progress bar at the
top of the window reports on search progress.
When the search is finished CodeRush immediately displays the results. The list on the left shows all found duplicates.
Click an entry in this list to see the corresponding duplicate code shown on the right side of the tool window.
The Duplicate Code tool window can be used to explore the connections between the detected duplicate code.
CodeRush visually shows the connections between all corresponding identifiers in the found duplicate blocks. Each identifier has the same color as all corresponding identifiers in all code blocks.
To navigate to a particular block of code (and consolidate starting with that block), just double-click the window showing the code block of interest.
Hover the mouse over the duplicate code issue bar appearing to the left of the source code. This will bring up the Duplicate Code Issue window:
From this window, you can navigate among duplicates using the left and right duplicate code navigation buttons. You can also bring up the Duplicate Code tool window using the
button in the middle. Code consolidation can also be invoked, if available, by clicking the desired consolidation location. Duplicate code can be consolidated into the current class,
an existing helper class, a new helper class, or a new ancestor class. The locations available depend upon the location of the duplicate code. You can either choose the target position from the
duplicate code issue hint or via the Code Consolidation wizard. Choose "to another position" from the duplicate code issue hint to call the wizard.
You can also call the Duplicate Code Issue window hovering the mouse over the duplicate code issue bar appearing to the right of the source code.