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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube