Title: A Metricbased Approach for Reconstructing Methods in ObjectOriented Systems
1A Metric-based Approach for Reconstructing
Methods in Object-Oriented Systems
- Tatsuya Miyake,
- Yoshiki Higo,
- Katsuro Inoue
2Research 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.
3Refactoring
- Whats 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
4Issue 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
5Research 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
6Proposal 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
7Proposal 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
8Proposal 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
9Identify 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 fragments
occupancy rate (OR) in the method - ALV ( Available Local Variables )
- Identify the block statements having undesirable
metrics value with their surrounding code
10Local Variable 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
- Attributes of classes should be hidden for
improving maintainability - Declare a field as a private attribute
- Use accessor methods as needed
- Field encapsulation
? Reduce the number of available variables
11Example of Local Variable Encapsulation
The unused variables, j, b, str2, are hidden
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)
Only the used variables are referenced as
arguments
j, b, str2 are not used
Refactoring
There are 6 available variables i, str, j, b,
str2, k
Available variables are only i, str, k
12Proposal 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
13Relation 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 lt I j) sam(
i ) if( log )
System.out.println(i)
while( )
bar(i) make(str)
while
while
if
for
if
- Vertical relation
- Between a block statement and its outer or inner
block statements. - Horizontal relation
- Between two block statements in the same scope
14Vertical 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
15Example of Vertical Coupling Low Coupling
public void sample ( )
newMethod() private void
newMethod ( ) while( hoge() ) int
i 0 String str string
foo(i)
public void sample ( )
while( hoge() ) int i
0 String str string
foo(i)
NRV 0 NAV 0
Just extract as a new method
16Example 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 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
public void sample ( ) int i 0
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
Two outer variables, i and bool, are referenced
str is subsequently referenced
17Horizontal 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
Strong coupling
Low coupling
Usage of the same variables in both block
statements
The kind of data flows
no data flow
indirect data flows
Extract as a new single method
Extract as different new methods
Refactoring
18Example of Horizontal Coupling Strong coupling
public void sample ( ) int i 0
String str
str newMethod1 ( i )
newMethod2( i, str )
Extract as different new methods
19Example of Horizontal Coupling Strong coupling
All you have to do is to pass variable i
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
20Example of Horizontal Coupling Low coupling
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
21Proposal 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
22Determine 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
23Proposal 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
24Estimate 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
25Proposal 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
26Experiment
- 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
27Case Study
Identification is adequate from functional
standpoint because of the code comment
28Summary
- 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
29Future 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
30- Thank you for your attention
- Please speak slowly,
- if you have some questions.
31Target 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
32Refactoring
- Whats 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
33Procedure 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
34Example 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
35Example 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
36Case Study (2/2)
37Determine 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
38Estimate 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