A Large-Gap Clone Detection Approach Using Sequence Alignment via Dynamic Parameter Optimization

Jinze Liu, Tao Wang, Chenhui Feng, Huaimin Wang, Dongsheng Li

Abstract

Code clone detection is a critical task in software engineering for maintaining code quality and preventing bug propagation. However, identifying "large-gap" Type-3 clones—where significant modifications, additions, or deletions separate original logic—remains a challenge for existing tools. This study introduces a bio-inspired approach that treats source code as biological sequences, utilizing the Smith-Waterman algorithm for local sequence alignment. The core innovation is a Dynamic Parameter Optimization strategy that scales match rewards and gap penalties relative to code length. Experimental results on the BigCloneBench dataset demonstrate a recall of 98-99% for Type-3 clones, significantly outperforming traditional static-threshold methods while maintaining high precision across large-scale industrial projects.

Introduction

In software development, "code clones" refer to fragments of source code that are highly similar or identical. While Type-1 (exact matches) and Type-2 (renamed variables) clones are relatively easy to detect, Type-3 clones involve more complex modifications, including statement insertions and deletions. A particularly difficult subset is the "large-gap" clone, where the core logic of a reused block is separated by substantial new content.

Traditional tools often struggle with these gaps. They either fail to recognize the relationship between the fragments or require similarity thresholds so low that they produce an unmanageable number of false positives. This paper addresses this gap by applying bioinformatics techniques to software analysis.

Example of a Type-3 code clone with significant modifications and gaps.

Methodology

The proposed detection workflow transforms source code into a sequence alignment problem, consisting of four primary stages:

1. Preprocessing and Fingerprinting

To handle variations in formatting and naming, the code is first normalized. Comments are removed, and identifiers (variables, method names) are replaced with generic tokens. The normalized code is then broken into functional fragments and hashed using MD5 to create a sequence of "fingerprints."

The four-step preprocessing workflow: Original code is tokenized, normalized, and finally converted into a sequence of hashes (fingerprints).

2. Sequence Alignment via Smith-Waterman

The Smith-Waterman algorithm, originally designed for local DNA sequence alignment, is used to find the best-matching sub-segments between two fingerprint sequences. This allows the tool to identify similar logic even if it is embedded within different surrounding code.

3. Dynamic Parameter Optimization

The central innovation of this research is the Dynamic Parameter Optimization strategy. Standard alignment algorithms use static scores for matches and penalties for gaps. However, a fixed penalty might be too restrictive for long code blocks or too lenient for short ones. The authors developed a method to calculate these parameters dynamically based on the length of the code fragments and a target similarity threshold, ensuring the algorithm remains sensitive to "gappy" clones without losing precision.

4. Directed Clone Identification

The tool distinguishes between "mutual clones" and "directed clones." By measuring similarity in both directions, the system can identify cases where a smaller code fragment has been incorporated into a much larger, evolved version of itself.

Evaluation and Results

The approach was rigorously tested using the BigCloneBench dataset, which contains over 8 million validated clones.

  • Recall: The tool achieved a 99% recall rate for "Very Strongly Type-3" clones and 98% for "Strongly Type-3" clones, matching or exceeding the performance of state-of-the-art tools like NiCad and SourcererCC.
  • Precision: In manual evaluations of five major open-source projects (Maven, Mockito, OpenNLP, Karaf, and MPAndroidChart), the tool maintained a precision range of 93% to 97%.
  • Efficiency: The system processed over 10 million lines of code in under 2.5 hours, demonstrating its scalability for industrial applications.

Conclusion

By treating code evolution as a series of "mutations" similar to genetic sequences, this approach provides a robust solution for detecting large-gap clones. While the method shows some sensitivity issues with very short fragments (under 10 lines), its ability to handle heavily modified Type-3 clones represents a significant advancement. Future work will focus on integrating semantic analysis to further reduce false positives and refining thresholds for smaller code snippets.

Summary updated

BibTeX
@article{liu2019large,
  author = {Jinze Liu and Tao Wang and Chenhui Feng and Huaimin Wang and Dongsheng Li},
  title = {{A Large-Gap Clone Detection Approach Using Sequence Alignment via Dynamic Parameter Optimization}},
  journal = {IEEE Access},
  year = {2019},
  doi = {10.1109/access.2019.2940710},
}
Figure