Efficient Code Clone Management based on Historical Analysis and Refactoring Support

Code clones have recieved great interests in recent years from many researchers, engineers, and practitioners in the field of software engineering. A code clone is defined as a group of code fragments that are identical or similar to one another. Code clones are introduced into source code of software systems by various reasons, and the most typical one is code cloning by copy-and-paste operations for reusing existing features. Typical software systems contain a certain amount of code clones because code cloning is a common practice for software developers. The existence of code clones has been regarded as a bad smell for software evolution over a period of time because code clones require much attention to be maintained. Once code clones are introduced into source code, most of them should be consistently maintained. Unintended inconsistencies among code clones have a high risk for introducing bugs in software systems. However, it is not an easy task for developers or maintainers to be aware of all the code clones and maintain all of them consistently, specifically in the case of large software systems. This is a reason why code clones are regarded as bad factors for software evolution. Many researchers have proposed a variety of techniques to cope with code clones based on this common wisdom. However, some of recent empirical studies have been against it. That is, these studies revealed that code clones do not highly affect software evolution. The discussion for harmfulness of code clones remains inconclusive, but it is widely accepted that not all but a part of code clones have negative impacts on software evolution. For these reasons, it is not effective to prohibit software engineers from code cloning. Furthermore, prohibiting code cloning is also unrealistic because of advantages of it. Therefore, it is strongly required to manage code clones effectively. The objective of the work described in this dissertation is to promote efficient software evolution through effective managements for code clones. To achieve this objective, it is necessary to know state-of-the-art of research achievements. Therefore, we conducted a survey on research literatures on code clones. This survey categorized literatures into five categories, detection, removal, prevention, analysis, and bug detection. The survey told us current states of research on code clones. It also told us limitations of previous research on code clone analysis, and unclear points of characteristics of them. Based on the results of the survey, we conducted an empirical study to reveal how negative impacts code clones have. Although some existing studies investigated the impacts of code clones, they have some limitations to genealize their findings. To resolve these limitations, this study proposed a new metric and used multiple clone detectors. The metric proposed by this study captures modification frequencies on code clones, which is under an assumption that code clones have negative impacts if they are frequently modified. This study compared metric values on cloned code with non-cloned code to judge whether code clones affect software evolution on 15 open source software systems with four clone detectors. The experimental results indicate that code clones do not tend to be modified more frequently than non-cloned code, hence the study concluded that code clones do not have seriously negative impacts on software evolution. Through the emprical study, we recognized a necessity of further analyses on impacts of code clones. This is because our first study lumped code clones all together, and so it did not consider how individual code clones affect on software evolution. Analyzing histories of individual code clones is helpful to investigate the research problem. To anlayze histories of individual code clones, it is necessary to track code clones across version histories of source code. There are some techniques to tackle this challenging task, but they still remain some issues. Therefore, we developed a new technique to track code clones by enhancing an existing clone tracking technique. The key idea of the enhancement is to link clones located in not only exactly the same regions but also similar ones. We confirmed an improvement of accuracy of clone tracking through an experiment on two open source software systems. Furthermore, we conducted an investigation for reasons why code clones are gone as an application of the proposed technique for clone tracking. With the new tracking technique, we conducted another empirical study on evolution of code clones. This study detected clone genealogies, which represent how individual code clones evolved, and analyzed them. This investigation was interested in how long code clones survived and how many times they were modified throughout their lifetimes. The experimental results reaffirmed that most of clones had short lifetimes and were modified at most once, both of which were reported in previous research. Furthermore, the results revealed some characteritics that have not been revealed in previous research. One of the findings is that approximately 3% of code clones are long-lived and modified multiple times. In other words, approximately 97% of code clones do not require high costs to be maintained. This finding empirically supports an opinion that not all but a part of clones affect software evolution. Another finding is that code clones tend to be modified more frequently in the former halves of lifetimes than in the latter ones. This finding suggests that it is necessary to start managing code clones in earlier stages of their lifetimes for an effective code clone management. In the next, we proposed a way to cope with code clones, which is a support to remove them with a particular refactoring pattern. The refactoring pattern used in this study enables to remove code clones even if they include some gaps. Moreover, the proposed technique performs a fine-grained analysis on source code, which allows to handle instances that any of previous refactoring supports cannot handle. Furthermore, the proposed technique can handle all the code fragments included in a code clone as against to existing techniques that can only handle a pair of code fragments. We have developed a software tool as an implementation of the proposed technique, and validated the usefulness of the technique through two experiments, one is on open source software systems, and the other is with subjects. This dissertation is organized as follows. Chapter 1 gives the background of this work and an overview of this dissertation. Chapter 2 presents the survey results on literatures that are related to code clones. This chapter explains a definition of code clones, and then it discusses some perspectives of them. The introductions of each research achievement follow the discussion, with categorized loosely into five categories. In Chapter 3, we present the result of the empirical study on stabilities of code clones. The chapter describes how to perform the investigation, the experimental targets of this study, and the experimental results. In this study, we compared the experimental results of our experimental methodology with other methodologies proposed by other researchers. This chapter refers to the results of the comparisons, and then it discusses the experimental results. Chapter 4 proposes the new clone tracking technique. This technque is an enhanced version of an existing technique. We fist explain the key idea of the enhamcement, and then discuss the effectiveness of the proposed technique with an experiment on open source software systems. Furthermore, we conduct another experiment to reveal why clones are gone, which is an application of the proposed technique. Chapter 5 presents the result of the empirical study on genealogies of code clones. This chapter revisits two major findings on evolution of code clones, and then reveals some characteristics that any of previous reseach did not address. Chapter 6 proposes the refactoring support technqiue for code clones including some gaps. This chapter formally describes the technique and introduces an implementation of the proposed technique. It also presents the experimental results to confirm the usefulness of the proposed technique. Finally, Chapter 7 summalizes this dissertation and shows some future directions of this work.