Exercise 2: Code Clone Analysis

In this exercise, you will learn about the new Code Clone analysis feature in Visual Studio 11. This tool looks for semantically similar code using a heuristic search technique, rather than simply searching for exact matches.

  1. Select Analyze | Analyze Solution for Code Clones from the main menu in Visual Studio.

    Figure 19

    Analyze solution for code clones

  2. Once the analysis is complete, the Code Clone Analysis Results window will show clone candidates grouped by match strength. Select the ‘+’ button to the left of the Strong Match group to expose the two files that had a strong match.

    Figure 20

    Viewing the strong file matches

  3. Each line shows the class and method, specific file, and lines that were determined to be strong matches. If you mouse over each match, a snippet of code gives you quick glance at some of the matching code.

    Figure 21

    Mouse over results to see details

  4. Double-click on each match to open them in code editor windows, and then right click on the title tab for one of them and select New Horizontal Tab Group from the context menu.

    Figure 22

    Opening both files for comparison

  5. Scroll through the code for the AssignSchedule method for each file simultaneously and note that it is identical except for the last line that calls the RedirectToAction method. This indicates that this method is a good candidate for refactoring. This kind of broad search is particularly useful when looking for code that can be refactored for easier maintenance in the future.

    Figure 23

    Comparing the strong match from the code clone analysis

  6. Select Window | Close All Documents from the main menu to clear up some screen real estate space.
  7. You can also narrow the focus of the search for code clones if desired. In Solution Explorer, navigate to Controllers folder of the FabrikamFiber.Web project and open CustomersController.cs in the code editor.

    Figure 24

    Opening the CustomersController.cs source file

  8. Scroll down to the Create method that takes a Customer parameter and select the three lines of code within the first “if” statement.

    Figure 25

    Selecting code for a code clone analysis

  9. Right-click on the selected lines of code and then select the Find Matching Clones in Solution option from the context menu.

    Figure 26

    Finding matching clones for the selected code

  10. After the search is complete, the Code Clone Search Results window shows snippet matches of varying strength.

    Figure 27

    Results of code clone search

  11. Expand all of the clone groups to expose the discovered matches, including the original clone group. You may want to increase the size of the Code Clone Search Results window so that you can see all matches at a glance.

    Figure 28

    Code clone results with groups expanded

  12. Hold the mouse cursor over the original code snippet to remind ourselves what we are searching for clones for.

    Figure 29

    Original code snippet

  13. Hold the mouse cursor over the Exact Match result and note that the Edit method uses exactly the same code as the code from the Create method.
  14. Hold the mouse cursor over the Strong Match result and note that the only difference is that the first line is a call to a Delete method.

    Figure 30

    Code snippet result with strong match to original

  15. Hold the mouse cursor over the first MediumMatch result and note that the snippet is similar to the original, but now we are working with an entirely different object (now employeeRepository).

    Figure 31

    Code snippet result with strong match to original

  16. In summary, there are three main scenarios where the identification of code clones may be useful to developers:
    1. Identification of candidates for code refactoring
    2. Bug fixes or enhancements are made in code and the developer wants to see if there are other similar locations that should be updated
    3. As a learning tool for a new developer starting work on a new team project, e.g. the developer adds code to update a customer record and wants to see if there are practices used by the rest of the codebase such as using a Try… Catch block