Skip to main content

Duplicate Code

  • 3 minutes to read

The Duplicate Code tool window lists all duplicate code clusters found in the current solution. You can view duplicate code blocks, open them in the code editor, or consolidate them directly from the tool window.

DuplicateCodeWindow

#Duplicate Code Window Toolbar

DuplicateCodeWindowToolbar

The toolbar allows you to start a search for duplicates, check consolidation availability, adjust the window elements appearance etc. It includes the following items:

Button Description Screenshot
ddcToolbarRunAnalysis Analyzes the current solution for duplicate code.
ddcToolbarCheckForConsolidation Checks all found clusters for consolidation availability. If consolidation is available, the cluster icon becomes green, otherwise the icon remains gray. ddcConsolidationAvailability
ddcToolbarOpenResult Loads duplicate code analysis data from an .xml file.
ddcToolbarSaveResult Saves duplicate code analysis data to an .xml file.
ddcToolbarGroupByProjects Groups files containing duplicate code by project within a cluster. ddcGroupByProject
ddcToolBarGroupByFiles Groups clusters by file. ddcGroupByFile
ddcToolbarUseExtendedView Shows/hides additional information on each cluster. Note that this option affects only grouped clusters. If clusters are shown without grouping, the tool window always shows the additional information on each cluster.
ddcToolbarShowVertically Shows duplicate code preview fields one under another.
ddcToolbarShowHorizontally Shows duplicate code preview fields one beside another.
ddcToolbarToggleLog Shows/hides the duplicate code analysis log.
ddcToolbarSynchronizeScrollbars Synchronizes vertical scrollbars of all shown duplicate code preview fields.
ddcToolbarOptions Opens the Duplicate Code options page.

#Duplicate Code Clusters

A duplicate code cluster is a set of code sections that have similar structure and probably can be consolidated to a single method.

The found clusters are listed at the left part of the Duplicate Code tool window.

DuplicateCodeClusters

The square to the left of a cluster indicates whether CodeRush Classic can consolidate the cluster or not.

ddcConsolidationAvailability

Note

Gray square color may also mean that the consolidation availability has not been checked for this cluster. Place the mouse pointer over the Consolidate item of the context menu to check the consolidation availability for a certain cluster, or press the ddcToolbarCheckForConsolidation toolbar button to check the consolidation availability for all clusters.

The cluster list also allows you to view all files containing the code related to a cluster,

ddcGroupByProject

or group clusters by files.

ddcGroupByFile

Use the ddcToolbarGroupByProjects and ddcToolBarGroupByFiles toolbar buttons to specify the way CodeRush Classic shows the clusters.

CodeRush Classic allows you to easily process duplicate code directly from the tool window using the Clusters context menu described below.

#Clusters Context Menu

DuplicateCodeContextMenu

The context menu called for a duplicate code cluster includes the following items:

Item Description
Copy Copies the selected cluster information to the clipboard as text.
Consolidate Opens the submenu allowing you to consolidate the selected cluster to the specified position. Note that the submenu items may appear after several seconds, because CodeRush Classic should check the selected cluster for consolidation availability.
Expand All Expands all nodes of the Clusters tree list
Collapse All Collapses all nodes of the Clusters tree list.
Import Loads duplicate code analysis data from an .xml file.
Export Saves the results to an .xml file.
Clear Clears the duplicate code analysis results.
Reset State Resets the duplicate code analysis engine.
Options Opens the Duplicate Code option page.

#Duplicate Code Preview

The code sections related to the selected Clusters tree list item are shown on the right side of the Duplicate Code tool window.

DuplicateCodePreview

CodeRush Classic 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.