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.

Slides:



Advertisements
Similar presentations
Duplicate code detection using Clone Digger Peter Bulychev Lomonosov Moscow State University CS department.
Advertisements

Unification and Refactoring of Clones Giri Panamoottil Krishnan and Nikolaos Tsantalis Department of Computer Science & Software Engineering Clone images.
Search in Source Code Based on Identifying Popular Fragments Eduard Kuric and Mária Bieliková Faculty of Informatics and Information.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Extraction of.
Each design pattern is designed to facilitate one kind of change, i.e. changes in one dimension. However, software evolution can happen in multiple dimensions.
Min Zhang School of Computer Science University of Hertfordshire
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Extracting Code.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Memories of Bug Fixes Sunghun Kim, Kai Pan, and E. James Whitehead Jr., University of California, Santa Cruz Presented By Gleneesha Johnson CMSC 838P,
The Template Method By Sinclair Schuller. What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points.
SMIILE Finaly COBOL! and what else is new Gordana Rakić, Zoran Budimac.
Refactoring Support Tool: Cancer Yoshiki Higo Osaka University.
1 Computer Science 340 Software Design & Testing © Ken Rodham 2003 The “Visitor” Design Pattern Source: "Design Patterns: Elements of Reusable Software"
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Industrial Application.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University Mining Coding Patterns to Detect Crosscutting Concerns.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University ICSE 2003 Java.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Finding Similar.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University What Kinds of.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Refactoring.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University A Criterion for.
Graph Data Management Lab, School of Computer Science gdm.fudan.edu.cn XMLSnippet: A Coding Assistant for XML Configuration Snippet.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Investigation.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University A clone detection approach for a collection of similar.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 ARIES: Refactoring.
Reviewing Recent ICSE Proceedings For:.  Defining and Continuous Checking of Structural Program Dependencies  Automatic Inference of Structural Changes.
2002/12/11PROFES20021 On software maintenance process improvement based on code clone analysis Yoshiki Higo* , Yasushi Ueda* , Toshihiro Kamiya** , Shinji.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University Detection and evolution analysis of code clones for.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Applying Clone.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University Inoue Laboratory Eunjong Choi 1 Investigating Clone.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University How to extract.
Toward an Implementation of the “Form Template Method” Refactoring Nicolas Juillerat University of Fribourg Switzerland.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Development of.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University Retrieving Similar Code Fragments based on Identifier.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University 1 Towards an Assessment of the Quality of Refactoring.
IDENTIFYING SEMANTIC DIFFERENCES IN ASPECTJ PROGRAMS Martin Görg and Jianjun Zhao Computer Science Department, Shanghai Jiao Tong University.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University 1 Towards an Investigation of Opportunities for Refactoring.
Duplicate code detection using anti-unification Peter Bulychev Moscow State University Marius Minea Institute eAustria, Timisoara.
Copyright © 2015 NTT DATA Corporation Kazuo Kobori, NTT DATA Corporation Makoto Matsushita, Osaka University Katsuro Inoue, Osaka University SANER2015.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Finding Code Clones.
Evaluating and Improving an OpenMP-based Circuit Design Tool Tim Beatty, Dr. Ken Kent, Dr. Eric Aubanel Faculty of Computer Science University of New Brunswick.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Cage: A Keyword.
Software Engineering Research Group, Graduate School of Engineering Science, Osaka University A Slicing Method for Object-Oriented Programs Using Lightweight.
Gordana Rakić, Zoran Budimac
Automatically detecting and describing high level actions within methods Presented by: Gayani Samaraweera.
Design Patterns: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Wei-Tek Tsai who’s.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Extracting Sequence.
What kind of and how clones are refactored? A case study of three OSS projects WRT2012 June 1, Eunjong Choi†, Norihiro Yoshida‡, Katsuro Inoue†
Department of Computer Science, Graduate School of Information Science and Technology, Osaka University Towards a Collection of Refactoring Patterns Based.
A proposal to support the extreme programming development methodology from the quality assurance point of view Authors: Calin Jebelean –
1 Gemini: Code Clone Analysis Tool †Graduate School of Engineering Science, Osaka Univ., Japan ‡ Graduate School of Information Science and Technology,
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Aries: Refactoring.
Department of Computer Science, Graduate School of Information Science & Technology, Osaka University A Metric-based Approach for Reconstructing Methods.
Estimating Code Size After a Complete Code-Clone Merge Buford Edwards III, Yuhao Wu, Makoto Matsushita, Katsuro Inoue 1 Graduate School of Information.
An Algorithm for Detecting and Removing Clones in Java Code Nicolas Juillerat & Béat Hirsbrunner Pervasive and Artificial Intelligence ( ) Research Group,
Design Patterns: MORE Examples
Do Developers Focus on Severe Code Smells?
Ruru Yue1, Na Meng2, Qianxiang Wang1 1Peking University 2Virginia Tech
○Yuichi Semura1, Norihiro Yoshida2, Eunjong Choi3, Katsuro Inoue1
Design Pattern: Visitor
Predicting Fault-Prone Modules Based on Metrics Transitions
Refactoring Support Tool: Cancer
On Refactoring Support Based on Code Clone Dependency Relation
Composite Design Pattern By Aravind Reddy Patlola.
Dotri Quoc†, Kazuo Kobori†, Norihiro Yoshida
Presentation transcript:

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

Refactoring with Eclipse It is easy to perform extract method refactoring by using Eclipse. 1

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 2 Modify Merge Modify However, Eclipse doesn’t support this code modification.

An Example of Similar Code Fragments 3 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; }

Refactoring Candidate 1 4 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; }

Refactoring Candidate 2 5 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

Motivation It is difficult to decide which part is extracted. If similar code fragments include multiple differences, it is more difficult. 6 Which is better? Cand.1Cand.2Cand.3 Show better candidates.

Form Template Method Refactoring pattern based on Template Method pattern [1]. –Merge similar code fragments including differences. 7 [1] M. Fowler. Refactoring: Improving the Design of Existing Code. Addison Wesley, Merge

Template Method 8 void select() { input(); sort(); output(); } Override // quick sort void sort(); Subclass B Subclass A // merge sort void sort(); // bubble sort void sort(); Subclass C Override

An Example of Form Template Method 9 … 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;

An Example of Form Template Method Step1: Detection of Primitive Processes 10 … 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

An Example of Form Template Method Step2: Extracting Primitive Processes 11 … 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

An Example of Form Template Method Step3: Pulling-Up Similar Methods 12 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

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 13 Which is better? Cand.1Cand.2Cand.3

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 14

[2] T. Miyake et al. “A software metric for identifying extract method candidates”, IEICE Trans. Inf.& Syst.(Japanese Edition), 2009 Proposed Approach Similar Methods COB = 0.9COB = 0.2 COB = st 3rd 2nd Cand.1 Cand.2 Cand.3 Cand.1 is better!! Input: Similar methods Output: Ranked candidates of Form Template Method Step1 Step2 Rank based on COB [2] 15

Step1: Detecting Differences between Similar Methods 16 Similar methods b C e B a C de A Compare Differences on AST Detect Differences on source code a C de A b C e B generate

Step1: Generating Candidates of Form Template Method 17 Verify extractable using Eclipse JDT Differences on source code Filter out wide range extraction candidates Developers don’t know which candidate is better.

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 18

An Example of Unsatisfying Any Conditions 19 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.

An Example of Satisfying All Conditions 20 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.

COB: Cohesion Of Blocks 21

22 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 v1v1 v2v3v4 BLOCK1 ✓✓ BLOCK2 ✓✓ BLOCK3 ✓✓ BLOCK4 ✓✓ COB = 0.5

23 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 v1v1 v2v3v4 BLOCK1 ✓✓ BLOCK2 ✓✓ BLOCK3 ✓✓ BLOCK4 ✓✓ Replaced v4 with v1

Step2: Ranking Candidates of Form Template Method 24 COB = 0.4 COB = 0.2COB = 0.8 Calculate COB 1st3rd2nd

Demonstration 25 Select menu Input: Two methods Output: Ranked candidates of Form Template Method 25 Target methods

Demonstration - Select Target Class - 26 Select target class 26

Demonstration - Select Target Method - 27 Select target method Select another method in the same way 27

Demonstration - First Candidate- 28 Corresponding extraction code fragments Extracting into children classes as same name method Generating all candidates

Demonstration - All Candidates- 29 Caption of each tab means the order of rank based on metric COB.

Related Work (1/3) Juillerat et al. proposed an approach of automatic “Form Template Method” [3]. 30 [3] N. Juillerat et al. Toward an Implementation of the “Form Template Method” Refactoring, 2007.

Related Work (2/3) 31 Similar methods a C de A b C e B Compare Differences on source code [○, □, a, A, □, ○, □, d, e, C, □, □] [○, □, b, B, □, ○, □, e, C, □, □] Our approach compares abstract syntax trees.

Related Work (3/3) 32 Differences on source code Only one candidate Our approach shows many candidates in order of COB.

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. 33

Thank You for Listening 34

35

36

Refactoring Candidate 3 37 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; }

Demonstration - Eliminated Candidate - 38 Extraction ranges are about 70% against the methods

Merging Similar Method Including Differences It is possible to merge similar methods including differences by applying Template Method pattern[1]. –However, it is difficult to apply it except for experts. 39 [1] E. Gamma, R. Helm, R. Johnson, and J. M. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995.

Template Method Pattern Common process is defined into super class –Each primitive process is implemented into children classes respectively. 40 It is easy to understand common processes. It is easy to add some children classes. It is possible to restrict places of bug.

An Example of Template Method Pattern 41 AbstractClass #method1() #method2() +templateMethod() ConcreteClass1 #method1() #method2() ConcreteClass2 #method1() #method2() public … templateMethod { … this.method1() … this.method2() … } NewClass #method1() #method2() Adding child class only by overriding abstract methods Define common process into super class Implementing each process into child class respectively

Proposed Approach Generating candidates of Form Template Method from given pair of similar methods. Ranking candidates based on a cohesion metric. –The higher cohesion, the more functional a method has. –We use a metric COB proposed by Miyake et al. for evaluating cohesion of code fragment[3]. 42 [3] T. Miyake et al. “A software metric for identifying extract method candidates”, 2009.

Demonstration - Other Example - 43 One of call statements is not match after extraction

Related Work Juillerat et al. proposed an approach of automatic “Form Template Method”[4]. –Detects different sub-trees by comparing sequences of abstract syntax tree nodes that are generated by using post-order traversal. –Shows only one candidate of “Form Template Method”. 44 [4] N. Juillerat et al. Toward an Implementation of the “Form Template Method” Refactoring, 2007.