Top Banner
A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1 , Norihiro Yoshida 2 , Tomoo Masai 1 ,Yoshiki Higo 1 , Katsuro Inoue 1 1 Osaka University 2 Nara Institute of Science and Technology
37

A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

A Tool Support to Merge Similar Methods with a Cohesion Metric COB

○ Masakazu Ioka1, Norihiro Yoshida2,

Tomoo Masai1,Yoshiki Higo1, Katsuro Inoue1

1Osaka University2Nara Institute of Science and Technology

Page 2: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

2

Refactoring with Eclipse

• It is easy to perform extract method refactoring by using Eclipse.

Page 3: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

3

Refactoring for Similar Code Fragments

• Developers would like to merge similar code fragments for refactoring.– Developers need to modify only one code fragment

after merging

Modify

Merge

Modify

However, Eclipse doesn’t support this code modification.

Page 4: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

4

An Example of Similar Code Fragments

float answer() { int a = getAve(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

float answer() { int a = getMax(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

Page 5: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

5

Refactoring Candidate 1

float answer() { int a = getAve(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

float answer() { int a = getMax(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

Page 6: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

6

Refactoring Candidate 2

float answer() { int a = getAve(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

float answer() { int a = getMax(); int b = getBase(); float ans;

if(a > b) { ans = a / b; print(ans); } else { ans = b / a; print(ans); } return ans;}

Extracted methods will be similar methods

Page 7: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

7

Motivation

• It is difficult to decide which part is extracted.• If similar code fragments include multiple

differences, it is more difficult.

Which is better?

Cand.1 Cand.2 Cand.3

Show better candidates.

Page 8: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

8

Form Template Method

• Refactoring pattern based on Template Method pattern [1].– Merge similar code fragments including differences.

[1] M. Fowler. Refactoring: Improving the Design of Existing Code. Addison Wesley, 1999.

Merge

Page 9: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

9

Template Method

void select() { input(); sort(); output();}

Override

Override// quick sortvoid sort();

Subclass B

Subclass A

// merge sortvoid sort();

// bubble sortvoid sort();

Subclass COverride

Page 10: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

10

An Example of Form Template Method

…double base = _units*_rate*0.5;double tax = base*Site.TAX_RATE*0.2;return base + tax;

Site

ResidentialSite

getBillableAmount()

LifelineSite

getBillableAmount()

…double base = _units*_rate;double tax = base*Site.TAX_RATE;return base + tax;

Page 11: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

11

An Example of Form Template MethodStep1: Detection of Primitive Processes

…double base = _units*_rate*0.5;double tax = base*Site.TAX_RATE*0.2;return base + tax;

Site

ResidentialSite

getBillableAmount()

LifelineSite

getBillableAmount()

…double base = _units*_rate;double tax = base*Site.TAX_RATE;return base + tax;

Detecting differences

Detecting primitive processes from differences

Primitive processes

Page 12: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

12

An Example of Form Template MethodStep2: Extracting Primitive Processes

…double base = getBaseAmount();double tax = getTaxAmount();return base + tax;

Site

ResidentialSite

getBillableAmount()getBaseAmount()getTaxAmount()

LifelineSite

getBillableAmount()getBaseAmount()getTaxAmount()

…double base = getBaseAmount();double tax = getTaxAmount();return base + tax;

Extracting as methods

Page 13: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

13

An Example of Form Template MethodStep3: Pulling-Up Similar Methods

Site

ResidentialSite

getBaseAmount()getTaxAmount()

LifelineSite

getBaseAmount()getTaxAmount()

getBillableAmount()getBaseAmount()getTaxAmount()

…double base = getBaseAmount();double tax = getTaxAmount();return base + tax;

Defining abstract methods at super class

Pulling-Up to super class

Page 14: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

14

• It is difficult for developers to identify the common parts and the primitive processes from similar code fragments.– Developers need experience of Form Template Method

and knowledge of software.

A Problem of Form Template Method

Which is better?

Cand.1 Cand.2 Cand.3

Page 15: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

• Showing candidates of Form Template Method in order of high cohesion.– When a method has high cohesion, the method may

have a single functionality.

Research Goal

15

Page 16: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

[2] T. Miyake et al. “A software metric for identifying extract method candidates”, IEICE Trans. Inf.& Syst.(Japanese Edition) , 2009

Proposed Approach

Similar MethodsCOB = 0.9 COB = 0.2 COB = 0.75

1st 3rd 2nd

Cand.1 Cand.2 Cand.3

Cand.1 is better!!

Input: Similar methodsOutput: Ranked candidates of Form Template Method

Step1

Step2

Rank based on COB [2]

16

Page 17: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

17

Step1: Detecting Differences between Similar Methods

Similar methods

b

C

e

B

a

C

d e

A

Compare Differenceson AST

Detect

Detect

Differences onsource code

a

C

d e

A

b

C

e

B

generate

generate

Page 18: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

18

Step1: Generating Candidatesof Form Template Method

Verify extractableusing Eclipse JDT

Differences onsource code

Filter out wide range extraction candidates

Developers don’t know which candidate is better.

Page 19: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

• It is necessary for Form Template Method to be exact match methods after extracting differences (Cond.1, 2).

• A method should have a single functionality (Cond.3).

Conditions of Appropriate Divisions

Cond.1: Each primitive process is extractable as method.Cond.2: A pair of similar code fragments is match after extraction.Cond.3: Extracted method has functionality for each developer.

Conditions of division on this research

19

Page 20: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

20

An Example of Unsatisfying Any Conditions

void initTriangle(int x) { ... if(z > 0) { s = getArea(); t = s * getRate(); result = calc(s, t); print(result); } draw(); ...}

void initRectangle (int x) { ... if(z > 0) { s = getBase(); t = getRate(); result = calc(s, t); print(result); } draw(); ...}Unsatisfied division of

Cond.3

Different code fragment

Cond.3: Extracted method has functionality for each developers.

Page 21: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

21

An Example of Satisfying All Conditions

void initTriangle(int x) { ... if(z > 0) { s = getArea(); t = s * getRate(); result = calc(s, t); print(result); } draw(); ...}

void initRectangle (int x) { ... if(z > 0) { s = getBase(); t = getRate(); result = calc(s, t); print(result); } draw(); ...}Satisfied division of

Cond.3

Different code fragment

Cond.3: Extracted method has functionality for each developers.

Page 22: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

22

COB: Cohesion Of Blocks

• Evaluates cohesion between blocks– When a method has higher cohesion, the method

may have a single functionality.

b : the number of code blocks.

v : the number of used variables in the method.

: j-th variable used in the method.

: the number of code blocks using variable .

Page 23: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

23

void method() {int v1, v2, v3,

v4;BLOCK1 {

v1 = v1 + v2;

} BLOCK2 {

v2 = v1++;}

BLOCK3 {v3 = v3 *

v4;}

BLOCK4 {v4 = v3 +

1;}

}

An Example of Low COB

v 1 v2 v3 v4

BLOCK1 ✓ ✓

BLOCK2 ✓ ✓

BLOCK3 ✓ ✓

BLOCK4 ✓ ✓

COB = 0.5

Page 24: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

24

void method() {int v1, v2, v3;BLOCK1 {

v1 = v1 + v2;

} BLOCK2 {

v2 = v1++;}

BLOCK3 {v3 = v3 *

v1;}

BLOCK4 {v1 = v3 +

1;}

}

An Example of High COB

COB = 0.66

v 1 v2 v3 v4

BLOCK1 ✓ ✓

BLOCK2 ✓ ✓

BLOCK3 ✓ ✓

BLOCK4 ✓ ✓

Replaced v4 with v1Replaced v4 with v1

Page 25: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

25

Step2: Ranking Candidates of Form Template Method

COB = 0.4 COB = 0.2 COB = 0.8

Calculate COB

1st 3rd2nd

Page 26: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

26

DemonstrationSelect menu

Input: Two methodsOutput: Ranked candidates of Form Template Method

26

Target methods

Page 27: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

27

Demonstration - Select Target Class -

Select target class

27

Page 28: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

28

Demonstration - Select Target Method -

Select target method

Select another method in the same way

28

Page 29: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

29

Demonstration - First Candidate-

Corresponding extraction code

fragments

Extracting into children classes as same name method

Generating all candidates

Page 30: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

30

Demonstration - All Candidates-

30

Caption of each tab means the order of rank based on metric COB.

Page 31: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

31

Related Work (1/3)

• Juillerat et al. proposed an approach of automatic “Form Template Method” [3].

[3] N. Juillerat et al. Toward an Implementation of the “Form Template Method” Refactoring, 2007.

Page 32: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

32

Related Work (2/3)

Similar methods

a

C

d e

A

b

C

e

B

Compare Differences onsource code

[○, □, a, A, □, ○, □, d, e, C, □, □]

[○, □, b, B, □, ○, □, e, C, □, □]

Our approach compares abstract syntax trees.

Page 33: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

33

Related Work (3/3)

Differences onsource code

Only one candidate

Our approach shows many candidates in order of COB.

Page 34: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

34

Conclusion and Future Work

• Conclusion– We proposed the tool that shows Good Candidates of

Form Template Method by cohesion metric COB.

• Future Work– How to decide a threshold for filtering.

Page 35: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

35

Thank You for Listening

Page 36: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

36

Page 37: A Tool Support to Merge Similar Methods with a Cohesion Metric COB ○ Masakazu Ioka 1, Norihiro Yoshida 2, Tomoo Masai 1,Yoshiki Higo 1, Katsuro Inoue 1.

37