Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why...
-
Upload
harry-peters -
Category
Documents
-
view
214 -
download
2
Transcript of Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why...
Program RefactoringProgram Refactoring
Mitch Soden
Union College
AgendaAgenda
• Definition– Unit Testing– Examples
• Why Refactor?
• Limitations
• Just Another SW Eng Practice?
• Automation tools
DefinitionDefinition
• Controlled/deliberate process
• Improve design/internal structure
• Existing code
• Behavior-preserving
Unit TestingUnit Testing
• Solid test suite - 1st step in refactoring
• Test after each refactoring
• Should be automated & self-checking
• Focus tests based on risk
Example – Extract MethodExample – Extract MethodMake 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 MethodExample – Extract MethodRefactored code:
...printBanner();
// perform calculationswhile (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 Example – Replace Error Code with Exceptionwith 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 Example – Replace Error Code with Exceptionwith Exception
Refactored code:
void withdraw(int amount) throws BalanceException {
if (amount > _balance) throw new BalanceException();
_balance -= amount;
}
Why Refactor?Why Refactor?
Problems
• Software modifications degrade structure
• Maintenance costs too high
• Inability to meet schedules
Why Refactor?Why Refactor?
Refactoring helps by
• Making software easier to understand
• Making software cheaper to modify
• Allowing developers become more productive
Limitations/DrawbacksLimitations/Drawbacks
• Time
• Human factor
• Changing interfaces
• Non-object oriented languages
Just Another Software Just Another Software Engineering Practice?Engineering Practice?
• Immediate payback
• Personal benefits
• It feels good
Automation ToolsAutomation Tools
Refactoring Browser– 1st refactoring tool built
– Univ. of Illinois PhD work
– Supports Smalltalk
– Integrates w/ Smalltalk Browser
Automation ToolsAutomation Tools
RefactorIt– Aqris Software, 2002
– Integrates w/ JBuilder, Forte, JDeveloper
– Many common refactorings
– Includes code metrics
– Automates auditing and corrective actions
Questions, comments, Questions, comments, opinions?opinions?