Duplicate Consolidation inside Visual Studio
The process of removing duplicate code is called code consolidation. The result of code consolidation is a single code block that replaces a specific duplicated code in several locations (different files or projects). The resulting consolidated code block is functionally absolutely the same as all duplicated code blocks it replaces.
To apply the code consolidation, you can use the Consolidate Code provider. It is available when the editor caret is positioned at a Duplicate Code issue via the CodeRush popup menu:
or through the Code Issues visual hints, such as Code Fix hint:
and the document bar hint:
If the Consolidate Code provider is not available, you can see why it is not available in the Code Fix hint by hovering over the “Why?” link:
If you think that duplicated code can be consolidated, click the “Report Issue” link to provide feedback about your expectations:
Once clicked, a special dialog appear for submitting feedback:
Consolidation locations
Code consolidation allows you to consolidate code at different locations. The code consolidation will suggest only an appropriate code location, such as:
- the current class – if duplicate code blocks are residing in a single type
- the base class – if duplicate code blocks are residing in a single class hierarchy
- a new ancestor class – if duplicate code blocks are residing in types that can be inherited from a new ancestor
- the helper class – if duplicate code blocks are residing in different classes that do not have a common ancestor and a new ancestor cannot be created
- the existing class, where previous consolidation has been performed
You may also have different project location options: for the ‘helper class’ and ‘new ancestor class’ locations, it is possible to choose whether to use the current project (if duplicate code blocks are residing in a single project) or create a new one (if duplicate code blocks are residing in different projects), so source projects will reference a newly created project:
If duplicate code blocks are residing in different projects and those projects reference other common projects, you can choose one of the referenced projects to create a helper class in it and move the consolidated code there. Once you have consolidated any duplicated code in a particular class (inside the current, new, or referenced project), you can choose that class as a target location when consolidating the next duplicated code.
Preview hint
As you have already seen, hovering over the target location for consolidation in the Code Fix hint or CodeRush Popup menu, will show you a preview hint with the changes being made to the current code and the visual diagram of the duplicated and consolidated code hierarchy:
Depending on the duplicate code block count, their location (classes and projects) and target location for the consolidated code, the hint will reflect and correspond to these parameters.
Consolidation in action
For example, a duplicated code may vary by different values used. Consider the following simple example of the duplicated code – it is not real, but it may show how consolidation works here:
These two methods do the same thing: inside the ‘for’ and ‘foreach’ loops it writes several lines with certain values to the Console. However, note the differences in the values and their different positions inside the code, e.g. passing “1 + 2 + 3” and “2 + 3 + 1” as arguments. Consolidating such code will require replacing all differences in values to parameters and pass the correct values as arguments to the resulting method. Also, because the parameters of two duplicate code blocks have different types they should be consolidated to a common type, if possible. In this specific case, the common type for both parameters will be the System.IEnumerable type. Here’s the resulting consolidated code:
and the calling sides:
This is a just simple example with local differences, but there can be also other things involved in code duplicates, such as different member references (dependencies), different types of parameters and fields, different project boundaries, etc. So, the consolidation task is rather really complex, with lots of edge cases. The CodeRush Consolidation Engine can deal with all of these differences and create a single code that functionally is the same as all replaced duplicated code blocks. If the code consolidation is not yet capable of removing a specific duplicated code, you can always send feedback to DevExpress by clicking the “Report issue” link, so the Consolidation Engine will be improved in the future.
Want to learn more?
- Duplicate Detection and Consolidation Overview
- Duplicate Real-time detection in Visual Studio
- Duplicate Detection Visual Studio tool window
- Duplicate Detection Using the Stand-alone Application
- Duplicate Detection analysis options
- Duplicate Detection in CodeRush versus Code Clone Analysis in Visual Studio 2011