Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why...

15
Program Program Refactoring Refactoring Mitch Soden Union College

Transcript of Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why...

Page 1: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Program RefactoringProgram Refactoring

Mitch Soden

Union College

Page 2: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

AgendaAgenda

• Definition– Unit Testing– Examples

• Why Refactor?

• Limitations

• Just Another SW Eng Practice?

• Automation tools

Page 3: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

DefinitionDefinition

• Controlled/deliberate process

• Improve design/internal structure

• Existing code

• Behavior-preserving

Page 4: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Unit TestingUnit Testing

• Solid test suite - 1st step in refactoring

• Test after each refactoring

• Should be automated & self-checking

• Focus tests based on risk

Page 5: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

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);

Page 6: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

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) {...

Page 7: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

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;

}

}

Page 8: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

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;

}

Page 9: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Why Refactor?Why Refactor?

Problems

• Software modifications degrade structure

• Maintenance costs too high

• Inability to meet schedules

Page 10: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Why Refactor?Why Refactor?

Refactoring helps by

• Making software easier to understand

• Making software cheaper to modify

• Allowing developers become more productive

Page 11: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Limitations/DrawbacksLimitations/Drawbacks

• Time

• Human factor

• Changing interfaces

• Non-object oriented languages

Page 12: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Just Another Software Just Another Software Engineering Practice?Engineering Practice?

• Immediate payback

• Personal benefits

• It feels good

Page 13: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Automation ToolsAutomation Tools

Refactoring Browser– 1st refactoring tool built

– Univ. of Illinois PhD work

– Supports Smalltalk

– Integrates w/ Smalltalk Browser

Page 14: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Automation ToolsAutomation Tools

RefactorIt– Aqris Software, 2002

– Integrates w/ JBuilder, Forte, JDeveloper

– Many common refactorings

– Includes code metrics

– Automates auditing and corrective actions

Page 15: Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.

Questions, comments, Questions, comments, opinions?opinions?