{'fraction_non_alphanumeric': 0.023054303077827878, 'fraction_numerical': 0.00652813173887473, 'mean_word_length': 4.790124858115777, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Use of Source Code Similarity Metrics in Software Defect Prediction

In recent years, defect prediction has received a great deal of attention in the empirical software engineering world. Predicting software defects before the maintenance phase is very important not only to decrease the maintenance costs but also increase the overall quality of a software product. There are different types of product, process, and developer based software metrics proposed so far to measure the defectiveness of a software system. This paper suggests to use a novel set of software metrics which are based on the similarities detected among the source code files in a software project. To find source code similarities among different files of a software system, plagiarism and clone detection techniques are used. Two simple similarity metrics are calculated for each file, considering its overall similarity to the defective and non defective files in the project. Using these similarity metrics, we predict whether a specific file is defective or not. Our experiments on 10 open source data sets show that depending on the amount of detected similarity, proposed metrics could achieve significantly better performance compared to the existing static code metrics in terms of the area under the curve (AUC).


INTRODUCTION
Testing is regarded as one of the most time and resource consuming phases in the software engineering life cycle, because underestimating tests can lead to serious problems for customers during the maintenance. According to Brooks, considering component and system tests together, testing and debugging activities takes about 1/2 of the project time, while coding takes only about 1/6 of it [9]. On the other hand, predicting defects in early development stages or at least before the maintenance is very crucial for project schedule, budget, and quality. The later a defect is observed and fixed means more cost and less customer satisfaction in a software project. Fagan states that the cost of reworking software bugs in programs becomes higher the later they are reworked in the software development process, so every attempt should be made to find and fix errors as early as possible in the development process [15].
Software defect prediction helps software engineers to improve the efficiency of the testing phase and gives the opportunity to increase the overall quality of a software product. According to Boehm and Papaccio, software rework tends to follow the Pareto-Zipf-type law, where 80 percent of the software rework costs result from 20 percent of problems [7]. Therefore, while allocating resources during the testing period, project managers should make use of the defect prediction techniques to make more optimal decisions and focus on the more defect prone modules rather than the less or non defective ones.
Fenton and Neil state that one of the key issues affecting the historical research direction of the software engineering community is the problem of using size and complexity metrics as sole predictors of defects [16]. Different and novel software metrics are always welcome, especially if their defect prediction performance is comparable or better on certain experiment contexts when compared to the existing metrics. As the software projects get larger and the amount of the shared code pieces increases within a project, the importance of measuring inter file attributes increases as well. This paper proposes a novel set of software metrics that are based on source code similarities detected among the source code files of a project and shows that proposed metrics are better defect predictors compared to the traditional source code metrics. Software metrics play a key role in measuring the defect proneness of a software product since the usefulness of a defect prediction model is highly dependent on the the quality of the underlying software metrics. Different types of software metrics have been proposed so far to measure software defect proneness. Software metrics are divided into two main categories as product and process based metrics. According to Henderson-Sellers [23] product metrics take the snapshot of a software at a certain time, whereas process metrics measure the software changes over time. Some of the product based metrics that measure the source code related attributes of a software product are Object Oriented metrics (CK suite) [10], Halstead metrics [22], McCabe metrics [32], and line of code metrics. On the other hand, process based metrics that measure the changing nature of the software are collected over a certain period of time and might be related to developers, revisions and source code changes. For example number of developers [45] [38] [51] [18] [37] [28] [35], number of revisions [41] [28] [35], and source code change related metrics [36] [18] [28] [28] [47] [35] are some of the process metrics that are commonly used in the defect prediction literature.
Static code metrics have a very long history in the defect prediction literature considering the fact that the Halstead [22] and McCabe [32] metrics were defined in 1970s. Furthermore, the extraction of static code metrics is relatively easy, because there are automated tools that mine the source code repositories of projects and calculate static code measures in a feasible amount of time. However, most of the static code metrics proposed so far show intra module characteristics, i. e., measure inner module attributes. Although some of them take into account inter module relationships [10], they can be used only if the object oriented programming paradigm is used. The metrics proposed in this paper are based on the source code similarities among the files of a software project, and take into account the inter module (file) relationships independent from the underlying software development methodology. This is a key novelty of the metric set defined in this paper, when compared to the existing source code metrics in the software defect prediction literature.
In this paper, the relationship between the syntactic or semantic similarities of source files and their defect proneness is investigated. Based on the hypothesis that a defective file could be more similar to a defective file, when two source code files are similar, we wonder if there is a relationship between their similarity and defectiveness? To measure the similarity among different source code files, the state of the art plagiarism and clone detection techniques are used. After finding the similarities among the source code files of a software system, two source code metrics are calculated for each file. One of these metrics is named Similarity to Defectives (STD) and used to measure the overall similarity of a file to all defective files in the software system. The second metric is named Similarity to Nondefectives (STN) and used to calculate the overall similarity of a file to all non defective files in the system. We use these novel source code similarity metrics that are summarized in Section 3.2 in order to predict software defectiveness. We test the performance of the proposed similarity metrics on 10 open source data sets listed in Table 2. The area under the ROC curve (AUC) is used as performance indicator, to show that the performance of the proposed metrics is significantly better when compared to the existing static code metrics listed in Table 3.
This paper is organized as follows: In Section 2, we present a brief review of the related work about software metrics and source code similarity detection in the literature. We explain the proposed source code similarity metrics in Section 3. We describe the experiment methodology and discuss our results in Section 4 before we conclude in Section 6.

PREVIOUS WORK
While developing complex software applications composed of thousands of lines of source code, the probability of committing errors is quite high. Therefore, no software system of any realistic size is ever completely debugged [52]. This research proposes to use a novel metric set that is based on the similarity of the source files in a software system to predict software defectiveness. Before describing the details of the proposed methodology, we provide a summary of the previous studies in defect prediction, software metrics, and different source code similarity detection approaches.
Hall et al. [21] carry out a systematic literature review using 208 fault prediction studies published between January 2000 and December 2010, and find out that simple modelling techniques like naive Bayes or logistic regression perform better than more complex modelling techniques like SVM. They conclude that the methodology used to build a prediction model seems to be significant for predictive performance. Lessmann et al. [29] use 10 public domain data sets from the NASA Metrics Data repository to compare the defect prediction performance of 22 classifiers. They conclude that the importance of a specific classification technique is less important than it was assumed to be before, because the differences in the prediction performance of the top 17 classifiers are not statistically significant. On the other hand, Song et al. [48] propose a general framework for software defect prediction to analyze the prediction performance of competing learning schemes and conclude that it is better to choose different learning schemes for different data sets since small details in the evaluation of experiments can completely change findings. Shepperd and Kadoda [46] state that there is a strong relationship between the success of a particular defect prediction technique and characteristics of the underlying data set, so the defect prediction methods must be evaluated in their experiment contexts before saying a technique is the best in general.
On the other hand, D'Ambros et al. [12] state that external validity is a challenging problem in the defect prediction literature, because although some defect prediction techniques perform better than others in specific contexts, no baseline exists to compare existing prediction approaches. Menzies et al. [34] state that debates for the merits of different metric sets are irrelevant, because how the metrics are used to build the prediction model is much more important than which particular metric set is used. Therefore, conclusions regarding the best metric(s) may not apply when different data sets are tried. They also conclude that data mining static code metrics to learn defect predictors is still useful. The novel metric set proposed in this research helps to build better prediction models, when the amount of similarity among the files of a software system is high.

Software Metrics
Different types of defect prediction metrics exist to provide objective, reproducible and quantifiable measurements about a software product. Daskalantonakis categorize software metrics based on their intended use as product, process and project metrics. He states that although a metric can belong to more than one category, product metrics are used to measure a software product itself, process metrics are used for improving the software development and maintenance process and the project metrics are utilized for tracking and improving a project [13].
McCabe shows that a software module can be modelled as a directed flow graph where each node is for a program statement and each arc is for the flow of control from one statement to another. McCabe introduces the cyclomatic, design and essential complexity terms and suggests to calculate static code metrics considering the directed flow graph generated from a software module [32].
Halstead states that software modules that are hard to read tend to be more defect prone. He calculates software complexity of a module by defining four key attributes as the number of operators, the number of operands, the number of unique operators, and the number of unique operands, then derives his metrics that provide insight about the source code complexity [22]. Although the theory behind Halstead metrics has been questioned repeatedly [17], together with McCabe metrics, these metrics are one of the most widely known source code complexity metrics in the defect prediction literature.
Chidamber and Kemerer (CK) propose a metric set specifically for software systems that are developed using the the object-oriented programming paradigm [10]. These metrics measure the degree of coupling and cohesion among the classes of a software system, as well as the depth of inheritance relationships. Henderson-Sellers proposes to use LCOM3 metric as an alternative to measure the class cohesion by taking into account the attribute method correlation [23]. Basili et al. use the object oriented metrics proposed by CK and mark some of them more useful compared to the remaining ones [5]. Furthermore Gyimothy et al. compare the accuracy of CK metrics together with some other metrics, to predict defective classes in Mozilla and conclude that CBO has the best predictive power [19]. Okutan and Yildiz use CK object oriented metric set together with line of code metric and show that response for class (RFC) is one of the most effective metrics on defectiveness, whereas the number of children (NOC) and the depth of inheritance tree (DIT) have very limited effect and are untrustworthy [37].
Bansiya and Davis state that there are metrics to model design properties like abstraction, messaging and inheritance, but no object oriented design metrics exists for several other design properties like encapsulation and composition. Furthermore, they state that some existing metrics to measure complexity, coupling and cohesion require almost a full implementation and that is why are difficult to generate during design. So, they define five new metrics (known as QMOOD metrics suite) which can be calculated from the design information only [4].
Line of code metrics are one of the most widely used static code metrics to measure the size of a software system. The importance of each single line could be different especially when considering whether it is an empty, comment or a single bracket (or begin) line. Therefore, different types of line of code counts proposed to measure each of these different types of lines like loc total, loc blank, loc code and comment, loc comments, and loc executable.
In addition to the static code, process, and line of code metrics, there are some other types of measures that are used to predict software defects. Zimmermann et al. propose a prediction model that is based on Windows Server 2003 to generate a module dependency graph and conduct network analysis on that graph. They define network metrics like degree of centrality, closeness, and betweenness centrality and show that network metrics on dependency graphs are useful to predict the number of defects. Furthermore, they also show that network metrics are better in predicting critical binaries and defectiveness when compared to the complexity metrics [53]. Meneely et al. use a new set of developer metrics that are based on a developer social network where two developers are connected if they work on the same files during the same period of time. They observe a significant correlation between the file based developer metrics and software failures [33].

Source Code Similarity
There are previous research studies about source code clones and plagiarism, but there is no prior study where source code similarity based metrics are used to predict software defectiveness. Below we briefly summarize different techniques that are used in the previous studies to find source code similarity or plagiarism: the C, C++, Java, COBOL source files. They use JDK, FreeBSD, Linux, NetBSD, and several other projects from the industry to show that the their optimized token based comparison technique is successful in detecting code clones [25]. Li et al. propose a tool named CP-Miner that is based on the token based approach to find source code clones. They show that compared to the CCFinder it finds between 17% and 52% more copy pasted source code, since it tolerates code modifications better [30]. 5) Semantic: Semantic techniques use the program dependency graphs (PDG) to detect similarities between code fragments [31]. They tolerate statement insertion or reordering and take into account semantic similarities (and data flow) between code fragments. They are less fragile to statement reordering and code insertion compared to other techniques, especially if experienced plagiarists try to disguise. In our research context, the focus is on detecting source code similarities rather than catching any disguises or plagiarised code fragments. Therefore, we use token based similarity detection tools during our experiments.

PROPOSED APPROACH
We believe that source code similarity based software metrics could be used to predict software defectiveness. Plagiarism detection tools use fingerprint based or content comparison techniques to find structural similarity among source code pieces. We use plagiarism detection tools or clone detection techniques to extract source code similarities and generate a similarity matrix that shows the extent of similarity among the files in a software system. Then using this similarity matrix, we calculate the proposed source code similarity based software metrics for each file in the system.

Source Code Similarity Detection
Token based similarity detection techniques are more successful compared to the string and tree based approaches. Because, they tolerate both variable renaming and even statement reordering by using fingerprinting [3]. JPlag [40] and MOSS [3] are two famous token based tools that are used worldwide to measure source code similarities. JPlag uses a token based approach to extract structural source code similarities and the similarity is calculated based on the percentage of token strings covered. JPlag is not so successful in finding all similarities in a software project due to some parse deficiencies [11]. MOSS (Measure of Software Similarity) is a token based plagiarism detection tool that tolerates statement insertion or reordering fragility by using fingerprinting. It uses an algorithm that divides programs into k-grams, where a k-gram is a contiguous substring of length k. Each k-gram is hashed and a subset of these hashes are selected as the fingerprint of a file. Similarity between any two files is determined by the number of fingerprints shared between these files. MOSS uses an efficient and scalable winnowing algorithm that includes positional information of the fingerprints and tends to use fewer fingerprints compared to previous techniques. Schleimer at al. report that after years of service, there was no false positives and the rare reports of false negatives (instances of common sub strings that could not be discovered) have found to be either an implementation bug or user error [44]. MOSS is a web based tool developed by Alex Aiken and hosted by Stanford University. One can submit the source code of a software project to MOSS (or a collection of projects together) to check plagiarism online [3]. Due to its ease of use and proven successful track record, we use MOSS to extract source code similarities among the files of the software projects used in the experiments. Besides the plagiarism detection tools, clone detection techniques are used as an alternative way of finding source code similarities. Karp-Rabin string matching algorithm compares the hash values of string tokens rather than the strings themselves. The copy paste detector (CPD) of the open source PMD source code analyzer uses Karp-Rabin string matching algorithm to find code clones among source code files in a software project [26]. Furthermore, CCFinder [25] uses a token based approach to detect code clones among the source code files. Ragkhitwetsagul et al. state that highly specialised source code similarity detection techniques and tools are more successful compared to the textual similarity measurement approaches and they show that CCFinder is one of the best performing clone detection techniques [42]. In addition to MOSS, CCFinder and CPD are used as clone detection techniques to find the clone based source code similarities among the source code files of each project.
As a summary, to find similarities among the source code files of each project in Table 2, MOSS plagiarism detection tool, and CPD and CCFinder clone detection tools are used. The parameters used with each each of these similarity detection tools are described below. 1) MOSS: MOSS uses two parameters, i. e., m that represents the maximum number of times a shared code may appear across files and n that determines the maximum number of matching files to include in the results. Assuming there are N files in a project, we use m = N and n = N 2 to detect and include all possible similarities.

2) CPD:
The open source PMD source code analyzer scans source code written in Java and other languages and looks for potential problems like bugs, dead or sub-optimal codes, over-complicated code pieces and duplicated code. The Copy/Paste Detector (CPD) of PMD was used to find the number of tokens shared among different files in each project.
To detect more similarities, the minimum number of tokens to report was set to 50. The total number of shared tokens between two files is used as a measure of similarity between them. 3) CCFinder: The ccfx command line utility of the CCFinder clone detection tool is used to extract shared clones among source code files in each project. Ccfx is run in the detection mode (with parameter −d) for each project directory to find clones, and then used in the print mode (with parameter −p) to print the contents of the the binary clone file generated in the detection mode.

Proposed Similarity Metrics
Assuming there are N files in a software system where each file is represented with f i , we generate a similarity matrix S of size N by N that shows the source code similarities among the files. Each S(f i , f j ) term in the matrix gives the amount of similarity between files f i and f j .
Then for each file f i we calculate ST D i (similarity to defective files) and ST N i (similarity to the non defective files) metrics. We find where we take into account On the other hand, we wonder how defectiveness is affected when the file size is considered together with the similarity. For instance, we want to see whether being 20% similar to a file of size 100 kilobytes is more meaningful when compared to being 20% similar to a file of size one kilobytes. That is why we introduce two more metrics and compare if they are better defect predictors compared to STD and STN. Assuming the size of a file f i is s i in terms of kilobytes, we calculate ST DS i (similarity to defective files with size) and ST N S i (similarity to the non defective files with size) metrics for each file f i as where we include S(f i , f j ) in the sum only if f j is non defective. As a summary, the list of the studied source code similarity based software metrics is shown in Table 1. The density of the similarity matrix (S) depends on the extent of the similarity of files in each project (data set). Therefore, the amount of detected similarity among the files of a project is very important while calculating the proposed similarity metrics. For each project, we first use MOSS plagiarism tool to detect the similarities of files, generate a similarity matrix, and calculate the proposed similarity metrics. To cross check our findings, we also use CPD clone detection technique and CCFinder to find the similarities of files, generate alternative similarity matrices, and calculate the same metrics for each project. As a summary, we calculate the STD and STN similarity metrics for MOSS, CPD, and CCFinder in each project.

Metric Generation Process
To extract similarity metrics listed in Table 1 based on the similarities detected by MOSS (with m = N , n = N 2 ), CPD (with minimum number of tokens set to 50), and CCFinder (with −d parameter), a special software was developed in Java. The main steps of the metric extraction process are: 1) First we find the syntactic or semantic similarities among the source code files of each data set using MOSS. The similarity output for each project is saved as a comma-separated file with three columns, i. e., the name of the first file, the name of the second file and the amount of similarity detected between them. The output file contains all similarities detected between any pairs of source code files in the project. 2) Using the output in the previous step, the similarity matrix S defined in Equation 1 is generated for each data set. 3) Based on the similarity matrix S, the proposed similarity metrics STD and STN are calculated for each file in each data set, as described in Section 3.2. For each source code file in each data set, a metric file is generated in Weka ARFF format that contains STD, STN metrics, and the defectiveness class (0 or 1). The similarity metric set generated from the similarity output of MOSS is denoted as M m for each data set. 4) As alternative similarity detection methods, CPD and CCFinder clone detection techniques are used to find the similarities among the files of each project. Using the similarities detected by CPD and CCFinder, the previous three steps are followed to generate alternative metric files that include STD, STN, and defectiveness (0 or 1). The metric sets generated from the similarities detected by CPD and CCFinder are denoted as M c and M f respectively. 5) How defectiveness prediction performance changes when the sizes of the source code files are considered while calculating the suggested similarity metrics? To answer this question, the similarities detected by MOSS are used to calculate the STDS and STNS metrics that take into account file size for each file in each data set. The similarity metric set that considers the sizes of the source code files is denoted as M ms .
The big picture of the metric extraction process is illustrated in Figure 1 where four metric sets, i. e., M m , M c , M f , and M ms are calculated for each data set based on the similarities detected by MOSS, CPD, and CCFinder.

Data Sets
Menzies et al. state that to measure the improvement in a prediction model, a well-defined baseline should exist and that baseline needs to be based on a public domain data set and the model should be repeatable [34]. The on-line Promise [1] data repository is one of the largest repositories for Software Engineering research data and includes a variety of publicly available research data sets. We use open source data sets in the Promise data repository and believe that everybody else can repeat our experiments on the same data sets to verify our observations. To select data sets for the experiments we have two objectives. First, the data sets need to be open source since we need to measure the similarity of the files in each data set to calculate the proposed similarity metrics. Second, the data sets need to be large enough, i. e., include sufficient number of files to be able to perform cross validation. Based on these two objectives, we select 10 publicly available projects [24], i. e., ant, camel, ivy, jedit, lucene, poi, synapse, tomcat, velocity, and xalan that are listed in Table 2 and have static code metrics available in the Promise data repository. In order to compare the performance of the proposed similarity metrics with the static code measures, the static code metrics in the Promise data repository are used. The same set of static code metrics in the repository (under the CK Metrics group) are used for all data sets. The names and sources of these static code metrics are listed in Table 3. The static code metric set includes object oriented metrics proposed by Chidamber and Kemerer [10], Bansiya and Davis [4], and the complexity metrics proposed by McCabe [32]. These object oriented and complexity metrics were explained in Section 2.1. The static code metric set also includes object oriented metrics defined by Schanz and Izurieta [43], Henderson-Sellers [23], and Tang et al. [49].

Classifiers
The classifiers used in this study are Random Forest (RF), Naive Bayes (NB), and k-Nearest Neighbours (kNN). These classifiers are selected for their common use in the software engineering defect prediction literature. In general, default settings of these classifiers are used as specified in Weka [20].
RF is an ensemble learning classification technique and one of the most successful defect prediction methods that constructs decision trees during training and outputs the class label that is the mode of the classes of the individual trees in the training set [8]. RF is a way of averaging multiple decision trees and boosts the performance of the classification model in spite of causing a small increase in the bias.
NB is a simple probabilistic classification method that makes use of Bayes rule of conditional probability. It is named naive, because it assumes conditional independence of its attribute set. NB classifiers are successful, even on real-world data where the variables are not conditionally independent [14]. Domingos and Pazzani state that since it has advantages in terms of simplicity, learning speed, classification speed, storage space and incrementality, the use of Bayesian classifier should be considered more often [14].
k-nearest neighbor (kNN) is a classification algorithm which is also known as IBK (instance based classifier) [2]. In kNN an instance is labelled considering the majority label of its k neighbours where k >= 1. We use the default parameters of kNN in Weka, except we set distanceWeighting parameter to by 1/distance and the crossValidate parameter to true. Because we want to assign higher weights to the nearer (more similar) instances while deciding on the defect proneness of a specific instance in each data set, we set the distanceWeighting parameter to 1/distance.

Performance Metric
Considering defect prediction as a binary classification problem, let S = {(x i , y i )} be a set of N examples where x i represents software metrics and y i represents the target class, i. e., whether the software is defective or not. Let us assume that defective modules (positive instances) are represented with + and non defective modules (negative instances) are represented with -, so we have y i ∈ {+, −}.

4) False positive (FP): If it is non defective but is classified as defective
A sample confusion matrix showing the prediction outcomes together with the actual class is shown in Table 4. It is desirable for a defect predictor to be able to mark as or FP rate (also called false alarm rate) It is a common way to look at the error rates of classifiers while making comparisons. However, this is not true in real life, because first, the proportions of classes are not equal. For instance, in defect prediction, the proportion of defective files are much smaller compared to the non defective ones. Furthermore, the cost of FP and FN are not the same, i. e., FN is more costly than FP. Considering a defective file as non defective is more costly than considering a non defective file as defective. As a result, instead of making comparisons using sensitivity or false alarm rate, it is better and more convenient to consider them together. ROC analysis is used in the literature and considers TP rate (sensitivity) and FP rate (hit) together. ROC curve is a two dimensional graphical representation where sensitivity (TP) is the y-axis and false alarm rate (FP) is the x-axis. It is desirable to have high sensitivity and small false alarm rate. So, as the area under the ROC curve (AUC) gets larger, the classifier gets better. Therefore, we use AUC as a comparison measure to compare the performance of the proposed metrics and the existing static code metrics in the literature.

Experiment Methodology
For each data set we compare the performance of the static code metrics and the proposed similarity metrics in terms of AUC. All classifications are carried out on the Weka experimenter with 5 × 5 folds cross validation. Static code metrics listed in Table 3 are used from the Promise data repository. The proposed similarity metrics are calculated for each file in each data set as it is described in Section 4.1. The experiments carried out on each data set are: 1) We use the static code metrics listed in Table 3 (denoted as M sc ) to learn the relationship between static code metrics and defect proneness for each data set. A brief overview of the experiments is shown in Figure 2 where five metric sets are compared using Random Forest, Naive Bayes, and k-Nearest Neighbour classifiers.   Table 3. To compare the performance of different metric sets, a two tailed t-test with a p-value of 0.05 is used in all statistical significance tests for all experiments.

Data Analysis
The similarity matrices (Equation 1) based on the plagiarism or clone detection techniques are sparse for all data sets. The number of rows and columns (RC), the number of non zero entries (N N E) and the density (ρ) of each similarity matrix generated for each data set is shown in Table 5 for MOSS, CPD, and CCFinder. The density column (ρ) shows the percentage of the non zero entries among the total number of entries in each similarity matrix. For instance, for camel data set, all similarity matrices have a size of 195 (195 rows and columns), and they have 2624, 46, and 21 non zero entries for MOSS, CPD, and CCFinder respectively. The densities of these similarity matrices are 6.9 %, 0.12 % and 0.06 % for MOSS, CPD, and CCFinder. The similarity matrix generated with CPD is 57.04 times more sparse (2624 / 46) compared to the similarity matrix generated with MOSS.
The similarity matrices generated from CPD are sparser compared to the similarity matrices generated from MOSS where all CPD matrices have a density value of less than 1. Except the poi and synapse data sets, the similarity matrices generated from CCFinder are also sparser compared to MOSS. For highly imbalanced data sets like camel this sparseness could be a problem if the number of non-zero similarity metrics is not enough for the classifier to learn the relationship of the similarity metrics and the defect proneness for the defective instances.  5 For each data set, the number of rows and columns (RC), the number of non zero entries (N N E), and the percentage of non zero entries (ρ) in the similarity matrices are shown for MOSS, CPD, and CCFinder. The key novelty of this research is based on the hypothesis that a defective file would be more similar to defective files. In order to check that and have a better insight about the distinctive characteristics of the detected similarities, we analyze the distribution of similarities found between defective files and the distribution of similarities seen between non-defective files. Therefore, in addition to the overall number of non-zero entries in the similarity matrices, we count the number of non-zero entries where both files are defective (E D ), and the number of non-zero entries where both files are non-defective (E N ). Table 6 shows that the majority of the detected similarities are either between defective files or non-defective files in each data set. In Table 6, the S column shows the total percentage of similarities represented by E D + E N for each similarity detection technique. For instance, for the ant data set, the total percentage of similarities between defective or nondefective files are 63.93 %, 70.07 % and 76.11 % respectively for MOSS, CPD and CCFinder. Having a high S means that defective files are more similar to the defective files and nondefective files are more similar to the non-defective files. We observe that S is larger than 70 % for 5, 7, and 10 data sets for MOSS, CPD and CCFinder respectively. In fact, for some data sets like camel, lucene, poi, and xalan, S is larger than 80 % for all similarity detection tools, i. e., MOSS, CPD, and CCFinder.

Analysis of Results with RF
Using the RF classifier, the AUC values obtained for each data set is shown in Table 7. We first observe that M m metrics are better defect predictors compared to the static code metrics (M sc ) for all data sets. The AUC values of M m are consistently and clearly higher than the AUC values of M sc in all data sets and the differences are statistically significant except ivy and velocity data sets. Furthermore, M m metrics are better than other similarity metric sets, i. e., M c and M f for all data sets and the differences are statistically significant.
Furthermore, we observe that similarity metrics calculated using the CPD (M c ) clone detection tool are not better defect predictors compared to the static code metrics (M sc ). Because, although the AUC values of M c are higher compared to the AUC values of static code metrics for lucene and synapse data sets, M c metric set has lower or comparable AUC values for other data sets. Similarly, TABLE 6 The number and percentage of similarities found between defective and non-defective files. except the ivy, lucene, synapse, and xalan data sets, the AUC value of the M f metric set is comparable or lower when compared to the static code metrics. The similarity matrices generated with CPD and CCFinder are more sparse compared to the similarity matrices generated with MOSS. The amount of similarity detected by CPD and CCFinder is less compared to MOSS, because CPD and CCFinder focus on the syntactic similarity and detect source code clones only. Therefore, for the majority of files in each data set the similarity metrics generated from CPD and CCFinder are zero, and that might be degrading the performance of the prediction model (See the lower AUC value of M c and M f for the highly imbalanced camel data set which also has a very sparse similarity matrix). Moreover, we observe that the similarity metrics that take the file size into account (M ms ) are better defect predictors when compared to the static code metrics and the differences are significant in all data sets except poi and velocity. However, when the performances of M m and M ms are compared, we observe that M ms achieves a slightly higher AUC in ivy data set. But, M m metrics are better defect predictors for the remaining data sets and the differences in the AUC values are statistically significant except poi and velocity data sets. Therefore, we conclude that taking the file size into account does not make a significant contribution to the prediction model.

MOSS CPD CCFinder Data Sets
As   Table 8. The AUC values observed for the similarity metrics calculated with MOSS (M m ) are higher when compared to the AUC values found for the static code metrics (M sc ). Furthermore, in eight of the ten data sets (except camel and ivy) the differences in the AUC values are statistically significant. The box plots of the AUC values for M m and M sc are shown in Figure 4 for all data sets. With the NB classifier, the following observations support previous findings with RF.  Table 5).
• Taking file size into account does not improve the defect prediction model, since the AUC values of M ms are lower compared to the AUC values of M m in 7 of 10 data sets (See Table 8).

Analysis of Results with kNN
The k-Nearest Neighbour algorithm (kNN) is used to compare the performance of the calculated similarity metrics (M m , M c , M f , and M ms ) with the static code metrics (M sc ).
The AUC values found with the kNN classifier is shown in Table 9 for each data set. We observe that the AUC values of M m are higher than the AUC values of M sc in all data sets and all the differences are statistically significant. The statistically significant differences between the AUC values of M m and the static code metrics (M sc ) are shown in the box plot in Figure 5. Similar to the previous observations with other classifiers, since the underlying similarity matrices are sparser, the similarity metrics calculated via CPD (M c ) and CCFinder (M f ) do not perform well when compared to M m metric set. Considering all similarity metric sets, i. e., M m , M c , M f , and M ms , the metric set M m achieves the highest performance in terms of AUC when kNN is used. Furthermore, it is hard to conclude that M c and M f are better defect predictors when compared to the static code metrics, because they both perform worse on some data sets like camel, poi, and xalan when compared to the M sc metric set.
Moreover, we find out that considering the size of the files while calculating the similarity metrics does not add a value to the prediction model. The AUC values of M ms are comparable with the AUC values of M m in some data sets like poi, tomcat, and xalan. However, the AUC values of M m are better in the remaining data sets although the differences are not significant in ant, camel, jedit, and velocity data sets. As a result, looking at the overall performance of M m and M ms in all data sets, it is hard to conclude that considering the file size (while calculating the proposed similarity metrics) improves the prediction model.

Summary of Results
First we observe that the performance of the proposed similarity metrics is consistent for all classifiers. We show that using the similarity metrics based on the similarities detected among the files of a software project, it is possible to predict software defectiveness. The following are some common observations with different classifiers: • Similarity metrics calculated from the output of the plagiarism tool (M m ) achieve a higher performance compared to the static code metrics (M sc ) in terms of the AUC. The differences in the AUC values of M m and M sc are statistically significant for all classifiers in almost all data sets.
• M m metric set is better in predicting software defectiveness when compared to other similarity metrics, i. e., M c and M f . When the underlying similarity detection method is not successful in catching existing similarities among the files in a project, then the performance of a classifier might get worse. Therefore, we observe that the similarity metric sets calculated via CPD and CCFinder do not perform well when compared to the static code metrics.
• Considering file sizes while calculating the similarity metrics does not improve the defectiveness prediction performance of the classifiers.
• If a data set is skewed and the density of its similarity matrix is low, a classifier might have a relatively poorer performance in predicting defective files. We observe lower AUC values for the highly skewed camel data set when the similarity matrices generated via CPD and CCFinder are used (See Tables 7,  8, and 9).

THREATS TO VALIDITY
Internal, construct and external validity are three types of validity threats that should be taken into consideration in research studies [39]. We briefly explain the measures taken to mitigate these validity threats.
An internal validity threat implies that cause effect relationships between the dependent and independent variables are not established properly. In our research context, an internal validity threat might be related to the selection of classification methods and their parameters, data sets, and the underlying similarity detection tools. To mitigate the internal validity threats, first we use different common classifiers with no parameter tuning and apply 5 × 5 folds cross validation during each experiment. Moreover, we use 10 different publicly available data sets to show that proposed similarity metrics have much better defectiveness prediction capability compared to the traditional static code metrics. But, we should note that the similarity metrics proposed in this study are highly dependent on the underlying similarity detection technique. If the amount of detected similarity is low (generated similarity matrix is so sparse), then the similarity metrics calculated from such a matrix could have a poorer defect prediction performance compared to the existing software metrics.
Construct validity threats can be observed when there are errors in the measurements. To mitigate construct validity, we automate the similarity metric calculation to prevent any calculation errors. Furthermore, we use different measurement tools to calculate source code similarities among the files of the data sets.
An external validity threat implies that the results observed on one data set are not valid for others. Although we use 10 open source data sets, alternative classifiers, and 5×5 folds cross validation in our experiments, it is not easy to generalize our findings. Our study is replicable since anyone else can calculate the same metrics using the same data sets to cross check our observations. We believe that further studies with different data sets and different classifiers are needed to justify our findings.

CONCLUSION
Source code similarity has been applied in software engineering literature to detect code clones or check for software plagiarism. However, there is no prior study using source code similarity metrics for defect prediction purposes. This paper proposes to use two novel software metrics which are based on the source code similarity to predict defect proneness. While one of the proposed metrics measures the overall similarity of a file to the remaining files within a software project that are marked as defective (STD), the other metric measures its overall similarity to the files that are marked as non defective (STN). Commonly used defect prediction methods, i. e., Random Forest, Naive Bayes and k-Nearest Neighbour classifiers are used with 5 × 5 folds cross validation, to check the predictive power of the proposed similarity metrics on 10 different open source data sets. We show that depending on the amount of detected similarity, proposed source code similarity metrics are better defect predictors compared to the traditional static code metrics in terms of the area under the ROC curve (AUC). Our results strongly endorse the usage of source code similarity metrics for software defect prediction.
Furthermore, we also check how the prediction capability of the proposed similarity metrics changes when the size of the files are taken into account while calculating these metrics. We show that including the file size in the metric calculation process does not add a significant value to the prediction model in none of the used projects.
Moreover, proposed source code similarity metrics are based on the amount of the similarity detected among the files of a software system. Therefore, we use both plagiarism and clone detection techniques to detect similarities within each software project and generate alternative source code similarity matrices for each data set. We observe that the performance of the proposed similarity metrics can increase or decrease depending on the amount of similarity detected by the underlying similarity detection technique. We show that the similarity matrices calculated using the MOSS plagiarism tool are more dense compared to the similarity matrices generated from the output of the clone detection techniques, i. e., CPD and CCFinder. For all data sets and with all classifiers, we observe that source code similarity metrics that are based on a dense similarity matrix (MOSS) are better defect predictors compared to the similarity metrics derived from a less dense (more sparse) similarity matrix.
There might be several reasons to have similar pieces of code or clones in software projects. First, copy and pasting a piece of code in the same project is a bad practice. Because, the code that is copied should be included in a method or class and called repeatedly. However, most of the time copy and pasting cannot be prevented even in the enterprise level software projects. Second, each developer has a certain source code development style like following certain custom coding patterns, and most of the time this style is consistent within a project or even across projects. One may expect to have a higher amount of similarity among the source code files developed by the same developer. Therefore, it is possible to find clones or very similar code pieces in almost all software projects. On the other hand, if the amount of similarity among the files of a software system is high, one might expect defective files to be more similar to other defective files. Similarly, non-defective files are expected to be more similar to other non-defective files. Because, having similar code pieces or common clones among different source code files, increases the probability of sharing a defective or bug free piece of code. In fact, this research is showing that if there is a high amount of similarity in a software project, the overall similarity of each file to the defective and non-defective files in the project could be used to predict defectiveness.
The Halstead and McCabe static code metrics were defined around 1970s and are still used in the defect prediction literature. Although there have been numerous phenomenal advances in the compiler, software development, and distributed computing technologies since 1970, relatively there are not enough studies regarding inter module software defect prediction metrics. Proposed metrics measure the inter module characteristics within a software project and have shown to be better defect predictors compared to the traditional static code metrics. As software projects get larger and the amount of common source code among software modules increases, more studies that focus on the inter module features of software projects are needed to achieve better defect prediction results.
This paper uses open source projects to extract similarity metrics and compares the performance of these metrics with the static code metrics which are available in the Promise data repository. Therefore, the study is reproducible, i. e., anybody else might use the same set of projects and the static code metrics in the Promise data repository, iterate through the defined experiment steps to generate similar results. Moreover, the study presented in this paper is also replicable. Because, the methodology defined in this study could easily be expanded to use a new data set other than the Promise data repository that might include a different set of software metrics and defect data.
As a future work, we plan to revise our study to merge the similarity matrices calculated using different similarity, plagiarism or clone detection techniques, recalculate the similarity metrics based on a composite matrix and check if the prediction capability of the similarity measures changes. As the amount of the similarity detected among the files of a software system gets higher, the proposed similarity metrics could achieve a higher defect prediction performance.