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, resurfacing 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 allows you consolidate many forms of code duplication automatically.

The ci_DuplicateCode 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.

DuplicatesFountHint

Click this icon to open the Duplicate Code tool window to see all duplicates found in the entire solution.

DuplicateCodeWindow

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 and consolidate duplications. In addition to synchronized scrolling through duplicate blocks, you can hover the mouse over the identifiers and see corresponding identifiers highlighted synchronously in adjacent code windows.

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.

DuplicateCodeIssueWindow

From this window, you can navigate among duplicates using left and right duplicate code navigation buttons. You can also bring up the Duplicate Code tool window using the ci_DuplicateCode 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. For example, if all the duplicate code is in a single class, it can be consolidated into a single method in the same class.

The to another position item click calls the Code Consolidation wizard, which allows you to specify the target position for consolidated code.

CodeConsolidationWizard

You can also call the Duplicate Code Issue window hovering the mouse over the duplicate code issue bar that appears to the right of the source code.

DuplicateCodeIssueDocumentBar