Find duplicate code using code clone detection

Source: Internet
Author: User

Code clones are very similar to separate code fragments. This behavior often occurs in applications that have been in development for some time. Cloning improves the difficulty of changing an application because you have to find and update multiple clips.

Visual Studio Ultimate or Visual Studio Premium helps you find code clones so you can refactor them.

You can find a clone of a specific fragment, or you can find all the clones in the solution. In addition to discovering fragments that are copied directly, the Clone analysis tool can find fragments with different names for variables and parameters, and fragments that are rearranged by some statements.

The code cloning Analyzer can be used to search the entire visual Studio solution for duplicate code in Visual C # and visual Basic projects. find a clone of a specific code fragment

Highlight the code snippet in the method or Get/set definition.

Description

You can find a statement clone, but you cannot find declarations such as fields, methods, or property signatures.

On the shortcut menu for the fragment, select Find matching clones in solution.

Use this method when you want to know if a similar method or fragment already exists in the solution. Find all clones in solution

On the Analyze menu, select Parse solution for code cloning.

This method is especially useful when you are reviewing the code.

Description

This command does not scan a method that has less than 10 statements in length.

The results are presented in the order of similarity. Expand each item to see the code snippet.

Note that even if the local variables used by similar fragments have different names, and several statements are inserted or deleted, they are also detected. Compare clones

In the Code Clone Results window, select two files or a clone group that contains two files.

From the shortcut menu, choose Compare.

The comparison tool used by this feature is the same as the tool used to compare versions under source code control. If you want to change it, choose Options from the Tools menu. Expand source Control and Visual Studio Team Foundation Server. Select Configure user Tools, and then select Add. exclude specific files or methods from analysis

Add a new XML file to the Visual Studio project that defines the method that you want to exclude.

It does not matter whether the file belongs to the project. It must be located in the top-level directory of the project.

Change the file name extension to. codeclonesettings

Edit the contents of the file so that it resembles the following example. Xml

<CodeCloneSettings>
  <Exclusions>
    <!--Add Any combination of the following node types.
    <!--Absolute or relative path names:-
    <File>MyFile.cs</File>
    <!--filepaths May Contain wildcards:-
    <File>GeneratedFiles\*.cs</File>
    <!--Namespace, Type, and FunctionName must be fully qualified:-
    <Namespace>MyCompany.MyProject</Namespace>
    < Type>mycompany.myproject.myclass1</type>
    <FunctionName> Mycompany.myproject.myclass2.mymethod</functionname>
    <!--Names may contain wildcards:
    -- <namespace>*. Anotherproject</namespace>
    <type>*. Anotherclass*</type>
    <functionname>myproject.*. anothermethod</functionname>
  </Exclusions>
</CodeCloneSettings>      
Find out which code clones.

The code cloning Analyzer will look for clones that are "within limits."

Code cloning is usually caused by a developer copying the code and then rewriting it based on the new location. Therefore, it is easiest to consider the extent to which the parser will not find a clone until the changes to the clone are reached. Cloning can still be recognized after the following modifications have been made. In each case, there is a certain limit to the number of such modifications:

Renames the identifier.

Insert and DELETE statements.

Rearrange the statements. which duplicate code will not be found.

The system does not compare type declarations. For example, you have a class with two fields that declare a group very similar, and the system does not report it as a clone.

The system only compares the statements in the method and property definitions.

"Parsing a solution for code cloning" does not look for a clone in a method that is less than 10 statements in length.

However, you can apply "find matching clones in solution" to shorter fragments.

A fragment with a marked change of more than 40% has been changed.

If the project contains a. codeclonesettings file, and the code element is specified in the exclusions section of the. codeclonesettings file, the system does not search for those code elements that are defined in the project.

To generate some kind of code:

*.designer.cs, *.designer.vb

InitializeComponent method

However, this method does not automatically apply to all generated code. For example, when working with text templates, you might want to exclude them by specifying the generated files in the. codeclonesettings file. When to use code cloning analysis

Finding clones is usually useful in the following situations:

When updating existing code. When you fix a bug or respond to a change in demand, you will typically find the location in the code that needs to change first. Search for a clone of the code fragment before making changes. If a clone is found:

Consider whether you need to make the same changes for each clone.

Consider whether this is also a good opportunity to refactor the cloned code in order to add it to a shared method or class.

Architecture cleanup. At the end of each iteration, use parse solution for code cloning on the Analysis menu.

When you create the encoding. After you write the new code, use the tool to find similar code that already exists. applying cloning analysis to a large code base

Code is often duplicated between completely separate parts of a large project, and often copied to different parts of the organization. Therefore, you should look for clones in as large a codebase as possible, rather than only in the current solution you are working with.

To apply a Code clone analyzer between the source trees, create a solution that contains all the items in the repository.

Tips

Keep in mind that a project can be included in multiple solutions. To analyze a large number of projects for code cloning, you can create a solution that contains all the projects. You do not need to remove items from a solution that typically accesses the project.

The generated code

Code cloning analysis is not intended for use with generated code. For example:

The code generated by the T4 template.

For more information about T4, see Code Generation and T4 text templates.

Code generated from the designer, such as the Silverlight or WPF user interface designer. exclude files generated by the T4 template from code cloning analysis

Place the template under a subdirectory of the Visual Studio project. Name it, for example Generatedfiles.

Add a new text file to your project, change its name and extension to T4exclusions.codeclonesettings

Change the contents of the file as follows: XML

<CodeCloneSettings>
  <Exclusions>
    <File>GeneratedFiles\*.cs</File>
  </ Exclusions>
</CodeCloneSettings>

Article Source: https://msdn.microsoft.com/zh-cn/library/hh205279 (v=vs.120). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.