Code Clone Analysis Tool in Visual Studio

Introduction:

Code clone analysis tool in Visual Studio is used to find the duplicate section code in your solution. In this blog you will learn about how to install and use the code clone analysis tool to find a duplication section of code.

Pre-Request:

Visual Studio 2017/2019 Enterprise Edition.

This tool is available only in Enterprise edition.

Installation:

Open Visual Studio installer, make sure the code clone under Individual components is installed. If not please enable and install it.

Code Clone
Figure 1: Code Clone

Code Clone Analysis Usage:

Open Visual studio and load your solution

Analyzing single line of code:

     Select a line of code for which you need to find a duplication and do right click. From the list of menus select Find matching clones in solution, as shown in the below figure.     

Code clone analysis
Figure 2: Code clone analysis
Duplicate Findings
Figure 3: Duplicate Findings

Now, you can select result and compare it with the original just by using the compare option by doing the right click as shown in below figure.

Compare
Figure 4: Compare
Comparison Result
Figure 5: Comparison Result

The above figure shows the comparison result between the selected files.

Analyzing the Solution:

 So far, we have seen finding the duplication for selected lines, now let’s extend the analysis for complete solution.

From menu select Analyze -> Analyze solutions for Code Clones.

Analyze Solution for Code Clones
Figure 6: Analyze Solution for Code Clones

This will list out all the duplication code blocks in the solution. Based on the matching level it will categories as weak, medium, exact and strong match as shown in below figure.

Code Clone Analysis Result
Figure 7: Code Clone Analysis Result

Summary:

    Things we learned,

  1. How to install and the usage of code clone tool in Visual Studio.
  2. How to perform code line level and solution level analysis using the code clone tool.

    I hope you have enjoyed this blog, your valuable feedback, questions, or comments about this blog are always welcome.

   Cheers !!!  

gowthamk91

Leave a Reply

%d bloggers like this: