Refactoring Support Tool:Cancer
Yoshiki HigoOsaka University
2
Gemini & Cancer Gemini
shows all code clones detected by CCFinder. gives user panoramic views of code clones in source
code. has several quantitative information of code clones of
each file and clone class.
Cancer extracts refactoring-oriented-code clones from ones
detected by CCFinder. appends several metric values to extracted code clones. enables user to know how to remove code clones if
certain degree of its metric conditions is satisfied.
3
Code clones in Cancer All code clones ( which are extracted from
ones detected by CCFinder) correspond to structural blocks of programming language.
Currently, Cancer can apply only Java language.
Declaration unit code clone class declaration 、 interface declaration
Method unit code clone method body 、 constructor 、 static initializer
Statement unit code clone do 、 for 、 if 、 switch 、 synchronized 、 try 、
while
4
Metrics in Cancer(1/3)
Cancer characterizes extracted code clones using 6 metrics.
Using these metrics, we can get how to remove them.
6 metrics are RVK,RVN,DCH,LEN,POP,DFL
5
Metrics in Cancer(2/3)
Variables which are used in code clone, but defined outside.
1. RVK : the number of such variables.2. RVN : the sum of used count of such
variables.
Relation of code clone on class hierarchy3. DCH : the degree of dispersion on class
hierarchy
DCH takes into account only class hierarchy of target software (don’t include class hierarchy of library).
Class A{
・・・
}
Class B extends A{
・・・
void foo( ・・・ ){
・・・
}
・・・
}
Class C extends A{
・・・
void bar( ・・・ ){
・・・
}
・・・
}
code clone
The value of DCH : 1
Class A{
・・・
void foo( ・・・ ){
・・・
}
・・・
void bar( ・・・ ){
・・・
}
・・・
}
code clone
The value of DCH : 0
int a;
MyClass myClass = new MyClass();
・・・
for( int i = a ; i < 10 ; i++ ){
int c = a + 1;
myClass.set(c);
}
・・・
code clone
RVK variable
The value of RVK : 2
The value of RVN : 2 + 1 = 3
6
Metrics in Cancer(3/3)
4. LEN: the average of length of code clones in a clone class
5. POP: the number of code clones in a clone class
6. DFL: Estimation of how many tokens would be removed from source files when all code clones of a clone class are replaced with caller statements of a new identical routine
new sub routinecaller
statements
7
Removal of Code clone using Refactoring Pattern We use Refactoring Pattern[1] for
removal of code clones with 6 metrics Following patterns are applicable.
Extract Method Pull Up Method
[1] M. Fowler: Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.
8
The value of metrics for “Extract Method”
Target Unit : Method Unit, Statement Unit DCH == 0 RVK <= 1
int i;
・・・
・・・
for( int j = 0 ; j < i ; j++ ){
・・・
}
・・・
int i;
・・・
・・・
i = newMethod(i);
・・・
int newMethod(int a){
for( int j = 0 ; j < a ; j++ ){
・・・
}
return a;
}
・・・
9
The value of metrics for “Pull up Method”
Target Unit : Method Unit DCH >= 1
10
ShapShot of CancerClone class list
Metric Graph
clone category selection Panel
11
Specifying code clones
User can specify code clones using Metric Graph and Clone Category Selection Panel Metrics Graph : Specifying based on metric
values Clone Category Selection Panel : Specifying
based on unit of code clone Clone class list shows only specified
clone classes
12
Specifying based on metrics values
LEN POP DFL RVK RVN
DCH
13
Specifying based on unit of code clone
LEN POP DFL RVK RVN
DCH
Method
While
・・・・・・・・・・・・・
・・・・・・・・・・・・・・
Method Unit
While Unit
14
Case study
Target software : Ant 1.5.4, which is an open source java software.
LOC : about 15k
The result Using “Pull up Method”, 2 clone classes are
removed. Using “Extract Method”, 1 clone class is
removed
15
16
Code Clone Code clone is a code fragment in source files
that is identical or similar to another
Code clone is one of factors that make software maintenance more difficult.
If some faults are found in a code clone, it is necessary to consider pros and cons of modification in its all code clones.
Clone Pair
Clone Class
17
Example Value of RVK, RVN Variables, “a” and “b” which are used in code
clone, but defined outside, are used 2 and 3 times.
RVK : 1 + 1 = 2 RVN : 2 + 3 = 5
18
Example values of DCH If all code fragments in a clone class are in
same class, DCH : 0
If all code fragments in a clone class are in a certain class and its child classes,
DCH : 1
If classes which include code fragments of a certain clone class don’t have common parent class,
DCH : -1
Top Related