Program Refactoring
Mitch Soden
Union College
Agenda
• Definition
– Unit Testing
– Examples
•
•
•
•
Why Refactor?
Limitations
Just Another SW Eng Practice?
Automation tools
Definition
•
•
•
•
Controlled/deliberate process
Improve design/internal structure
Existing code
Behavior-preserving
Unit Testing
•
•
•
•
Solid test suite - 1st step in refactoring
Test after each refactoring
Should be automated & self-checking
Focus tests based on risk
Example – Extract Method
Make code fragment into method whose name explains the purpose.
...
// print banner
System.out.println (“*********************”);
System.out.println (“*** Customer Info ***”);
System.out.println (“*********************”);
// perform calculations
while (x.hasMoreElements()) {
...
}
// print details
System.out.println (“Name:
” + name);
System.out.println (“Balance: ” + balance);
Example – Extract Method
Refactored code:
...
printBanner();
// perform calculations
while (x.hasMoreElements()) {
...
}
printDetails(balance);
}
void printBanner() {
System.out.println (“*********************”);
System.out.println (“*** Customer Info ***”);
System.out.println (“*********************”);
}
void printDetails(double balance) {
...
Example – Replace Error Code
with Exception
Make a method throw an exception instead of returning a special code to
indicate an error.
int withdraw(int amount) {
if (amount > _balance)
return –1;
else {
_balance -= amount;
return 0;
}
}
Example – Replace Error Code
with Exception
Refactored code:
void withdraw(int amount) throws BalanceException {
if (amount > _balance) throw new BalanceException();
_balance -= amount;
}
Why Refactor?
Problems
• Software modifications degrade structure
• Maintenance costs too high
• Inability to meet schedules
Why Refactor?
Refactoring helps by
• Making software easier to understand
• Making software cheaper to modify
• Allowing developers become more
productive
Limitations/Drawbacks
•
•
•
•
Time
Human factor
Changing interfaces
Non-object oriented languages
Just Another Software
Engineering Practice?
• Immediate payback
• Personal benefits
• It feels good
Automation Tools
Refactoring Browser
– 1st refactoring tool built
– Univ. of Illinois PhD
work
– Supports Smalltalk
– Integrates w/ Smalltalk
Browser
Automation Tools
RefactorIt
– Aqris Software, 2002
– Integrates w/ JBuilder,
Forte, JDeveloper
– Many common
refactorings
– Includes code metrics
– Automates auditing
and corrective actions
Questions, comments,
opinions?
Descargar

Program Refactoring