Inside TFS

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.

About the Author

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.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube