Visual Studio 11 Developer Preview: Code Clone Analysis
Learn how Visual Studio 11 helps developers find duplicate code throughout their projects and solutions.
Code clone analysis is a new feature available in Visual Studio 11 (and currently available in the Visual Studio 11 Developer Preview) that allows for identification of similar code sections. These sections, or "code clones", can occur for a variety of reasons. The number one reason is copying and pasting code. Unfortunately, this type of code duplication can make changing the code difficult, as the same change must be made in multiple places in the code base. Code clone analysis, by identifying these similar sections of code, makes it easier to refactor them.
While code clone analysis will only work over a single solution, it will search through all the projects contained in that solution. In addition, code clone analysis uses heuristic search techniques to discover the duplicate code. This means not only will exact code matches be found, but also sections of code that are similar, but not exact, will also be flagged.
There are two main ways to use code clone analysis: analyze the entire solution for all potential code clones, or select a section of code and search for that code fragment throughout the solution. Please note: This column was written using the Visual Studio 11 Developer Preview, and as such, certain functionality may be subject to change before the final release.
Analyzing The Entire Solution
Analyzing the entire solution will search through all of its projects, looking for instances of code clones. This can be useful for code reviews. One caveat: code clones less than 10 statements long won't be discovered when analyzing the entire solution. To analyze the entire solution, open the solution in Visual Studio 11, then select Analyze | Analyze Solution For Code Clones from the main menu. Figure 1 shows the results of the analysis.
[Click on image for larger view.]
|Figure 1. Code Clone Analysis Results from analyzing the entire solution.|
The analysis shows two exact matches for a code clone. Moving the mouse over each line will show the matching code in a pop-up window. Double-click a line to open the file in Visual Studio. Opening the file this way will color code the duplicated code in the file.
Search For A Code Fragment
Code clone analysis can also be used to find a particular code fragment in a solution. Unlike analyzing the entire solution, this search can find code clones less than 10 lines in length. To get started, highlight the fragment of code to search for, right-click on it, and select Find Matching Clones in Solution from the context menu. Figure 2 shows the results of the search.
[Click on image for larger view.]
|Figure 2. Code Clone Analysis Results for a fragment of code.|
In this example, the analysis found both exact matches for the code clone, as well as "medium" matches. The medium matches indicate code was found that did not match 100 percent of the code fragment, but was very similar.
Code clone analysis comes in very handy when updating an existing code base. When a bug is fixed in one section of the code, it's imperative to make sure that same code wasn't used elsewhere in the application. Code clone analysis shows all the places where the code was copied and used, allowing for the code to be fixed in all locations.
It was mentioned briefly earlier, but code clone analysis only runs over a particular solution. It can't analyze multiple solutions to look for code sharing. Unfortunately, most developers have multiple solutions with multiple projects, between which they have shared code. One work-around for this limitation is to create a new solution containing all the developer's Visual Studio projects. This allows code clone analysis to run over all projects.
There may be files in a project that don't need to be analyzed by code clone analysis. These files can be excluded by creating a file in the project with a .codeclonesettings extension, then adding some XML to this file to exclude the appropriate files in the project.
Code clone analysis will help improve code quality. Whether analyzing the entire solution looking for clones, or searching for a particular code fragment, it makes it easier to find duplicate code and refactor it, which in turn will create higher quality, easier-to-maintain code.
Mickey Gousset spends his days as a principal consultant for Infront Consulting Group. Gousset is lead author of "Professional Application Lifecycle Management with Visual Studio 2012" (Wrox, 2012) and frequents the speaker circuit singing the praises of ALM and DevOps. He also blogs at ALM Rocks!. Gousset is one of the original Team System/ALM MVPs and has held the award since 2005.