380 likes | 478 Views
A Metric-based Approach for Reconstructing Methods in Object-Oriented Systems. Tatsuya Miyake, Yoshiki Higo, Katsuro Inoue. Research Overview. Propose a method that automates some of refactoring steps Identify where the software should be refactored
E N D
A Metric-based Approachfor Reconstructing Methods in Object-Oriented Systems Tatsuya Miyake, Yoshiki Higo, Katsuro Inoue
Research Overview • Propose a method that automates some of refactoring steps • Identify where the software should be refactored • Determine which refactoring pattern should be applied • Estimate the effect of the refactoring ⇒ Support to perform appropriate refactorings with low cost. WoSQ2008
Refactoring • What’s Refactoring • A set of operations to improve internal attributes of a software system without changing the external behavior of it • One of trenchant countermeasures to handle large-scale and complex software systems • Refactoring Procedure • Step 1: Identify where the software should be refactored • Step 2: Determine which refactoring pattern should be applied to the identified place • Step 3: Estimate the effect of the refactoring • Step 4: Apply the refactoring • Step 5: Maintain the consistency between the refactored program code and other software artifacts WoSQ2008
Issue with Refactoring • Refactoring often requires the high costs • A good deal of knowledge and experiences • There are few skillful developers • Inappropriate refactorings may be performed instead of appropriate ones • A lot of time and energy • It is difficult to precisely estimate the effect of refactorings on the early stage of the refactoring • The refactoring may not be worth the cost WoSQ2008
Research Purpose • Automatization of a part of refactoring process • Reduction of the refactoring cost • Appropriate refactoring • Improve the maintainability • Efficient and effective refactoring • Estimate the effect of refactoring WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Identify where the software should be refactored • Measure metrics on each method and each block statements in it • Cyclomatic complexity • The complexity of control logics • The number of linearly independent paths from the start node to the end one of a graph • Represented by the number of conditional expressions plus 1 • LOC (Lines Of Code) • Used for measurement of the identified fragment’s occupancy rate (OR) in the method • ALV ( Available Local Variables ) • Identify the block statements having undesirable metrics value with their surrounding code WoSQ2008
Local Variable Encapsulation • Attributes of classes should be hidden for improving maintainability • Declare a field as a private attribute • Use accessor methods as needed • Field encapsulation • To hide unused local variables may improve maintainability of a mehtod • Extract the code fragment as a new method • Access by arguments and a return value as needed • Local variable encapsulation ⇒Reduce the number of available variables WoSQ2008
Example of Local Variable Encapsulation The unused variables, j, b, str2, are hidden ALV : 6 public void sample ( ) { int i= 0; String str; int j = 0; boolean b; String str2; ・ ・ while( hoge() ) { int k = bar(); jar( str ); foo(i, k); } ・ } public void sample ( ) { int i = 0; String str; int j = 0; boolean b; String str2; ・ newMethod(i, str); ・ } void newMethod(int i, String str) { while( hoge() ) { int k = bar(); jar(str); foo(i,k); } } ALV : 5 Only the used variables are referenced as arguments j, b, str2 are not used ALV : 6 Refactoring ALV : 3 There are 6 available variables i, str, j, b, str2, k Available variables are only i, str, k WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Relation between block statements sample ( ) public void sample ( ) { int i = 0; String str; while( hoge() ) { str = “string”; if( jar( I ) ) { make(str); } foo(i); for( int j = 0; j < I ; j++) { sam( i ); if( log ) { System.out.println(i); } } } ・ while( ) { bar(i); make(str); } } • Vertical relation • Between a block statement and its outer or inner block statements. • Horizontal relation • Between two block statements in the same scope while while if for if WoSQ2008
Vertical Coupling between Block Statements • A coupling between block statements that have the vertical relation each other • Measure based on the number of outer variables that are the used local variables defined outside the bock statement • NRV ( Number of Referenced Variables) • Pass the values to the new method as arguments, If extract the new method • NAV ( Number of Assigned Variables ) • The new method may have to return the assigned variables as return value WoSQ2008
Example of Vertical Coupling ~ Low Coupling~ public void sample ( ) { ・ ・ while( hoge() ) { int i = 0; String str = “string”; foo(i); } ・ ・ } public void sample ( ) { ・ ・ newMethod(); ・ ・ } private void newMethod ( ) { while( hoge() ) { int i = 0; String str = “string”; foo(i); } } NRV : 0 NAV : 0 Just extract as a new method WoSQ2008
Example of Vertical Coupling ~ Strong Coupling~ If the number of assigned outer variables is more than one These value have to be passed to the new method as arguments public void sample ( ) { int i = 0; boolean bool ; String str; ・ ・ while( hoge() ) { str = “string”; foo(i); bar( bool ); } ・ ・ return str; } public void sample ( ) { int i = 0; boolean bool ; String str; str = newMethod(i, bool); ・ return str; } private String newMethod (int i, boolean bool) { String str; while( hoge() ) { str = “string”; foo(i); bar( bool ); } return str; } An outer variable, str, is assigned NRV : 2 NAV : 1 Strong vertical coupling ⇒ a lot of operations to complete the refactoring Extract as a new method Have to return a value of the assigned variable Two outer variables, i and bool, are referenced str is subsequently referenced WoSQ2008
Horizontal Coupling between Block Statements • A coupling between two block statements that have horizontal relation each other • Measured based on the number and the kind of data flows between the two block statements threshold Low coupling Strong coupling Usage of the same variables in both block statements The kind of data flows no data flow indirect data flows Extract as different new methods Extract as a new single method Refactoring WoSQ2008
Example of Horizontal Coupling ~Strong coupling~ public void sample ( ) { int i = 0; String str; ・ ・ while( hoge() ) { str = “string”; foo(i); } ・ while( ) { bar(i); make(str); } ・ ・ } public String newMethod1 (int i) { String str; while( hoge() ) { str = “string”; foo(i); } return str; } public void sample ( ) { int i = 0; String str; ・ ・ str = newMethod1 ( i ); ・ newMethod2( i, str ); ・ } Two variables, str and i, are referenced or assigned Two same variables, str and i, are referenced or assigned Extract as different new methods public void newMethod2 (int i, String str){ while( ) { bar(i); make(str); } } The two variables, str and i, have to be shared The two variables, str and i, have to be shared WoSQ2008
Example of Horizontal Coupling ~Strong coupling~ All you have to do is to pass variable i public void sample ( ) { int i = 0; String str; ・ ・ while( hoge() ) { str = “string”; foo(i); } ・ while( ) { bar(i); make(str); } ・ ・ } Public void sample () { int i = 0; ・ newMethod ( i ); ・ } public String newMethod1 (int i) { String str; while( hoge() ) { str = “string”; foo(i); } ・ while( ) { bar(i); make(str); } } Extract as a new single method The signature is simple Easier than extracting as different methods WoSQ2008
Example of Horizontal Coupling ~Low coupling~ There is no outer variable used in the both while statement There is no outer variable used in the both while statement public void sample ( ) { int i = 0; String str; ・ ・ while( hoge() ) { str = “string”; foo(i); } ・ String str2 = str; ・ while( ) { bar(); make(str2); } ・ } • Extract as a new single method • The signature of the new method is simple • Few arguments • Need no return value • Extract as different new methods • Division based on functionality • Each variable has its own role A data flow between two while-statement by the assignment of str to str2 WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Determine which refactoring should be applied • Determine based on what kinds of members are used in the target fragment • Members of its own class are mainly used ⇒ Apply “Extract Method” • Only members of the super class are used ⇒ Apply “Pull Up Method” • Members of other classes are mainly used • Local feature envy • Only the identified spot may be interested in other classes • Extract the identified spot as a method of the class whose members are most used ⇒ Apply both “Extract Method” and “Move Method” WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Estimate the effect of refactoring • Definition of the effect of refactoring • Change of complexity metrics values • Cyclomatic complexity • The number of conditional expressions in the extracted code • LOC( Lines of Code) • The number of lines of the code extracted as a new method • ALV( Available Local Variables ) • The number of local variable declaration in the extracted code • Coupling between classes • May change in the case of applying “Move Method” WoSQ2008
Proposal Technique -Overview- • Identify where the software should be refactored • Target: A code fragment within a method • Several software metrics • Coupling of block statements with their surrounding code • Vertical coupling • Horizontal coupling • Determine which refactoring pattern should be applied to the identified place • By reference to the used variables and the invoked methods • Turn the fragment into a new method • Extract Method • Pull Up Method • Move Method • Estimate the effect of the determined refactoring • Based on the change of software metrics WoSQ2008
Experiment • Target language: Java • Target software • High-quality software • JHotDraw • Low-quality software • Programs written by undergraduate students • Several versions of the same software • Evaluation standard • Adequacy of automatic identification of the spot should be refactored • Adequacy of automatic determination of the applied refactoring • Change of complexity metrics value WoSQ2008
Case Study Low vertical coupling ↓ It is easy to extract from outer block statements There is no outer variable referenced in both “for-statement” Identification is adequate from functional standpoint because of the code comment Low horizontal coupling ↓ The upper for-statement is not included in the extracted part The number of available outer variable is 13 The number of really used outer variable is 3 WoSQ2008
Summary • In this study, we • proposed ALV of new metrics for representing the maintainability of software • proposed the method that automates some of refactoring steps based on ALV and other metrics • Showed there are cases where our method is effective WoSQ2008
Future work • We are going to • experiment more to confirm the adequacy of our refactoring method • Quantitative data derived from the result of our planned experiment • Evaluation of the result of our method by developers • investigate the relation between ALV and the quality of software • Whether there is correlation between ALV and bugs or not • propose and implement program slicing method to identify more adequately the surrounding code of the block statement WoSQ2008
Thank you for your attention Please speak slowly, if you have some questions. WoSQ2008
Target software • High-quality software • JHotDraw5.3b • About 18000 loc • About 288 classes • Low-quality software • Programs written by under undergraduate students • Support code clone analysis • About 3000 loc • About 107 classes • Several versions of the same software WoSQ2008
Refactoring • What’s Refactoring • A set of operations to improve internal attributes of a software system without changing the external behavior of it • One of trenchant countermeasures to handle large-scale and complex software systems • Effect of Refactoring • Enhance readability of a target software • Improve software design • Maintainability • Scalability • Reusability • Find bugs of a target software WoSQ2008
Procedure of Refactoring • Step 1: Identify where the software should be refactored • Long Method • Complicated Control Structure • Step 2: Determine which refactoring should be applied to the identified place • Various refactoring patterns have been proposed • Step 3: Estimate the effect of the refactoring • Prevent from performing inefficient or ineffective refactorings • Step 4: Apply the refactoring • Step 5: Maintain the consistency between the refactored program code and other software artifacts WoSQ2008
Example of applying “Extract Method” class A { public boolean field1; public void a2(); public void a3(); public void sample ( ) { ・ while( field1() ) { a2(); b.b1(); a3(); } ・ } } Class B { public b1(); public b2(); } class A { public boolean field1; public void a2(); public void a3(); public void sample(); public void newMethod ( ) { while( field1() ) { a2(); b.b1(); a3(); } } } Class B { public b1(); public b2(); } Extract as a new method of the same class Members of the same class are mainly used WoSQ2008
Example of applying “Pull Up Method” class A extends B{ ・・・・・・・・・・ public void sample ( ) { a1(); a2(); newSuperMethod(); a3(); } } class B { protected bool fieldB; protected void b1(); protected void b2(); protected void newSuperMethod() { while( fieldB ) { b1(); b2(); } } } class A extends B{ public void a1(); public void a2(); public void a3(); public void sample ( ) { a1(); a2(); while( fieldB ) { b1(); b2(); } a3(); } } class B { protected bool fieldB; protected void b1(); protected void b2(); } Using only members of the super class in the extraction target Extract as a new method of the super class WoSQ2008
Case Study (2/2) Low vertical coupling with owner method Low horizontal coupling Strong horizontal coupling WoSQ2008
Determine which refactoring should be applied • Determine based on what kinds of members are used in the target fragment • Members of its own class are mainly used ⇒ Apply “Extract Method” • Only members of the super class are used ⇒ Apply “Pull Up Method” • Members of other classes are mainly used • Local feature envy • Only the identified spot may be interested in other classes • Extract the identified spot as a method of the class whose members are most used ⇒ Apply both “Extract Method” and “Move Method” WoSQ2008
Estimate the effect of refactoring • Definition of the effect of refactoring • Change of complexity metrics values • Cyclomatic complexity • The number of conditional expressions in the extracted code • LOC( Lines of Code) • The number of lines of the code extracted as a new method • ALV( Available Local Variables ) • The number of local variable declaration in the extracted code • Coupling between classes • May change in the case of applying “Move Method” WoSQ2008