Professional Documents
Culture Documents
Evolution:
Special Change Called
Refactoring
Learning Outcomes: Refactoring
Apply appropriate refactoring
techniques to resolve design
problems in code
More perspective on
Software Change
Opening thoughts on
Refactoring
Example from Book…
Probably finish on your own
2
Origins of a Change
3
Change Types/Categories
Corrective Change – changes to fix errors in design, logic,
coding, documentation
(does not change requirements specifications)
Q1 4
Or,
New Requirements
Existing
Perfective
Requirements
Corrective
&
Preventive
5
Cost Breakdown by Change Type
Corrective
Adaptive
Perfective
50% Preventative
25%
Q2 6
What is Refactoring?
A disciplined technique for restructuring existing
code, altering internal structure without changing
external behavior
Q3 7
Extremely Simple Example
Using any number other than zero in functional
code is a road to disaster
Not clear why that value
Harder to change the value when rules change
Instead of writing
Feet := Miles * 5280;
Write…
const FEET_PER_MILE = 5280;
Feet := Miles * FEET_PER_MILE;
8
Simple Video Rental Example (1 of 5)
public class Movie {
public static final int CHILDRENS = 2;
public static final int NEW_RELEASE = 1;
public static final int REGULAR = 0;
10
Simple Video Rental Example (3 of 5)
public class Customer {
private String _name;
private Vector _rentals = new Vector();
12
Simple Video Rental Example (5 of 5)
Q5 15
First Step, Before you Start
Refactoring…
Refactoring is changing
software
Q6 16
Redistributing Statement Method (1 of 3)
Q7 17
Redistributing Statement Method (2 of 3)
private double amountFor(rental each) {
double thisAmount = 0;
switch (each.getMovie().getPriceCode()) {
case Movie.REGULAR:
thisAmount += 2; thisAmount = amountFor(each);
if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
thisAmount += each.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
return thisAmount;
} 18
Redistributing Statement Method (3 of 3)
19
Clarify Names in New Method
private double amountFor(rental aRental) {
double result = 0;
switch (aRental.getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (aRental.getDaysRented() > 2)
return += (aRental.getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
result += aRental.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (aRental.getDaysRented() > 3)
return += (aRental.getDaysRented() - 3) * 1.5;
break;
}
return result;
} 20
“Move Method” Technique
Q8 21
Move amountFor Method to Rental Class
Class Rental …
double getCharge() { Class Customer…
double result = 0; private double amountFor(rental
switch (getMovie().getPriceCode()) { aRental) {
case Movie.REGULAR: return result;
result += 2; }
if (getDaysRented() > 2)
return += (getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
result += getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (getDaysRented() > 3)
result += (getDaysRented() - 3) * 1.5;
break;
}
return result;
} 22
Exercise: Think/Pair/Share
23
23