Skip to content

How to Detect Cross Component Clones

Teamscale's clone detection allows detecting so-called cross component clones. A component, in this context, refers to a component as per Teamscale's architecture conformance model and can essentially be an arbitrary subset of the code within a Teamscale project. This feature enables distinguishing between clones with clone relationships across the boundaries of certain code areas (cross component clones) and those that only have clone relationships within a code area (intra component clones). An example would be a situation where the code of a component (or project) was forked from another one, and you are interested in the commonalities between the two components.

TIP

A clone is a cross component clones as soon as any of its siblings is located in another component as per the cross component clone detection architecture. A clone is an intra component clone if all of its siblings are located in the same component as per the cross component clone architecture.

For cross component clone detection, Teamscale provides two dedicated finding types: Cross Component Clones and Intra Component Clones. In addition, the corresponding metrics Cross Component Clone Coverage and Intra Component Clone Coverage are provided. To enable cross component clone detection, have a look at the analysis profile. In the analysis group Redundancy ❯ Code Clones (see screenshot below), you find three checks for the different clone types. Activate cross or intra component clones here, or both, to get clone findings for the respective clone type. Note that plain clones cannot be activated in that case.

Analysis Profile Group

You can access additional related options by clicking the gear symbol next to the Code Clones group (left side in the above screenshot). In the upcoming dialog (see screenshot below), you can enable the mentioned clone coverage metrics and specify the name of the architecture file that defines the components to use for cross component clone detection, or you can keep the provided default. Please note that providing an architecture model is mandatory for cross component clone detection, i.e., an architecture model with the name specified here must exist in the project. If the model does not exist, the cross component clone detection will not detect any clones or will report all clones as intra component clones until the model becomes available.

Analysis Profile Options

TIP

When using cross component clone detection, the normal clone detection cannot be used at the same time. Thus, when enabling any of the cross component options, the Clones finding option has to be disabled. The Clone Coverage metric can still be used and will refer to the clone coverage of all clones (both cross and intra component clones).

Cross Component Clone Findings

Cross Component Clone Coverage

TIP

The architecture used for cross component clone detection has to be a file-based architecture. It is recommended to set the Finding Creation option to None for this architecture and, thus, exclusively use it for cross component clone detection.

Cross component clones will be detected as soon as the configured architecture model exists in the project history. If you want to analyse cross component clones for the history of a project, the architecture model has to be added in time-travel model to the project's commit history in Teamscale.

Please note that both the cross component findings and the cross component clone coverage are sensitive regarding changes to the architecture model used for the detection. Thus, changing the architecture model may result in findings being removed from one clone finding type and added to the other. Similarly, both metrics can change. Note again that this will only affect the findings/metrics from the point in time on when the change to the architecture takes place. If you want to alter the component definition in the project history, the architecture needs to be edited in time-travel mode.

Exclusive Intra Component Detection (Alternative Detection Mode)

In addition to the normal cross component clone detection as described above, there is an alternative mode for the exclusive detection of intra component clones. This mode can be enabled in the Code Clone options described above (see screenshot). If the exclusive intra component detection is enabled, cross component clones cannot be detected anymore. Hence, cross component clone coverage and cross component clone findings must be disabled in the analysis profile.

In this mode, all code that does not reside in a component, as defined by the cross component architecture, is ignored and no clones within such code will be detected. For each detected clone, its component is reported in addition to the normal clone properties such as location, instance count, and length. The exclusive mode supports shared code between components, i.e., a file may be included in more than one component. This can be configured by enabling the structure only mode in the used cross component architecture. For clone siblings that reside in shared files, only one joint clone finding for all involved components is created per location, and the involved components are listed in their properties.