











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A case study on various refactoring techniques for simplifying complex conditional statements. The techniques covered include decompose conditional, consolidate conditional expression, consolidate duplicate conditional fragments, remove control flag, and replace nested conditional with guard clauses. The document also introduces the concept of introduce null object to eliminate null checks and conditional statements.
Typology: Study notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 2
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 3
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 4
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 5
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 6
Before:
After:
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 7
Clarifies that the sequence is really one check of an “or” of conditions Often sets the stage for Extract Method
The conditions must not have side-effects. Replace the string of ifs with one if using logical operators Compile and test Consider applying Extract Method to the result
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 8
if (isSpecialDeal()) { total = price * 0.95; send(); } else { total = price * 0.98; send(); }
if (isSpecialDeal()) { total = price * 0.95; } else { total = price * 0.98; } send();
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 9
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 10
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 11
set done to false while not done if (condition) do something set done to true next step of loop
Determine the value that gets you out Replace assignments of that value with break/continue
Compile and test after each replacement
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 12
set done to false while not done if (condition) do something set done to true next step of loop
Extract the logic into a method Determine the value that gets you out Replace assignments of that value with a return Compile and test after each replacement
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 19
void checkSecurity (String[] people) { String found = foundMiscreant(people); someLaterCode(found); } String foundMiscreant (String[] people) { String found = “”; for (String person : people) { if (found.equals(“”)) { if (person.equals(“Don”)) { sendAlert(); found = “Don”; } if (person.equals(“John”)) { sendAlert(); found = “John”; } } } return found; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 20
String foundMiscreant (String[] people) { String found = “”; for (String person : people) { if (found.equals(“”)) { if (person.equals(“Don”)) { sendAlert(); return “Don”; } if (person.equals(“John”)) { sendAlert(); found = “John”; } } } return found; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 21
String foundMiscreant (String[] people) { String found = “”; for (String person : people) { if (found.equals(“”)) { if (person.equals(“Don”)) { sendAlert(); return “Don”; } if (person.equals(“John”)) { sendAlert(); return “John”; } } } return found; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 22
String foundMiscreant (String[] people) { String found = “”; for (String person : people) { if (found.equals(“”)) { if (person.equals(“Don”)) { sendAlert(); return “Don”; } if (person.equals(“John”)) { sendAlert(); return “John”; } } } return “”; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 23
String foundMiscreant (String[] people) { for (String person : people) { if (person.equals(“Don”)) { sendAlert(); return “Don”; } if (person.equals(“John”)) { sendAlert(); return “John”; } } return “”; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 24
double getPayAmount () { double result; if (_isDead) result = deadAmount(); else { if (_isSeparated) result = separatedAmount(); else { if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); }; } return result; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 25
double getPayAmount () { if (_isDead) return deadAmount(); if (_isSeparated) return separatedAmount(); if (_isRetired) return retiredAmount(); return normalPayAmount(); }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 26
Should either return or throw an exception
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 27
double getPayAmount () { double result; if (_isDead) result = deadAmount(); else { if (_isSeparated) result = separatedAmount(); else { if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); }; } return result; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 28
double getPayAmount () { double result; if (_isDead) return deadAmount(); else { if (_isSeparated) result = separatedAmount(); else { if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); }; } return result; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 29
double getPayAmount () { double result; if (_isDead) return deadAmount(); if (_isSeparated) return separatedAmount(); else { if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); }; return result; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 30
double getPayAmount () { double result; if (_isDead) return deadAmount(); if (_isSeparated) return separatedAmount(); if (_isRetired) return retiredAmount(); else result = normalPayAmount(); return result; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 37
public double getAdjustedCapital () { // example requires reversing sense of conditionals double result = 0.0; if (_capital <= 0.0) return 0.0; if (intRate <= 0.0 || _duration <= 0)) return 0.0; result = (_income / _duration) * ADJ_FACTOR; return result; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 38
public double getAdjustedCapital () { // example requires reversing sense of conditionals double result = 0.0; if (_capital <= 0.0) return 0.0; if (intRate <= 0.0 || _duration <= 0)) return 0.0; return (_income / _duration) * ADJ_FACTOR; return result; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 39
public double getAdjustedCapital () { // example requires reversing sense of conditionals // Note: May want to combine tests into one conditional, // or separate into three conditionals if (_capital <= 0.0) return 0.0; if (intRate <= 0.0 || _duration <= 0)) return 0.0; return (_income / _duration) * ADJ_FACTOR; }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 40
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 41
if (customer == null) plan = BillingPlan.basic(); else plan = customer.getPlan();
Plan = customer.getPlan();
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 42
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 43
Create a Null version subclass. Implement an isNull() method for the original and new class. The null object should be a singleton of the null class
Compile
Replace setting to Java null with the null object
Replace comparisons with null by isNull() calls
Compile and test
For each conditional, add a method to the Null class that overrides; compile
Remove the condition check; compile and test
Repeat until all conditionals removed
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 44
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 45
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 46
class Customer … public boolean isNull () { return false; } protected Customer () {} // needed by subclass … class NullCustomer extends Customer { @Override public boolean isNull () { return true; } } // optional: use Nullable interface interface Nullable { boolean isNull(); } // have Customer implement this interface
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 47
class Customer …
static Customer newNull() { return new NullCustomer(); // or … set up to use singleton pattern } …
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 48
class Site … Customer getCustomer () { return (customer == null)? Customer.newNull() : _customer; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 55
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 56
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 57
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 58
double getExpenseLimit () { // should have a limit or a primary project return (_expenseLimit != NULL_EXPENSE)? _expenseLimit : _primaryProject.getMemeberExpenseLimit(); }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 59
double getExpenseLimit () { assert (_expenseLimit != NULL_EXPENSE) || (_primaryProject != null); return (_expenseLimit != NULL_EXPENSE)? _expenseLimit : _primaryProject.getMemeberExpenseLimit(); }
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 60
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 61
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 62
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 63
Compare getinvcdtlmt vs. getInvoiceableCreditLimit Choosing good names is a skill that you develop
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 64
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 65
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 66
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 73
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 74
void sendAlert (String[] people) { if (! foundPerson(people).equals(“”)) sendAlert(); }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 75
fivePercentRaise() {…}
tenPercentRaise() {…}
raise(float percentage) {…}
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 76
Dollars baseCharge () { double result = Math.min(lastUsage(),100) * 0.03; if (lastUsage() > 100) { result += (Math.min(lastUsage(),200)-100)0.05; }; if (lastUsage() > 200) { result += (lastUsage() – 200) * 0.07; }; return new Dollars(result); }*
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 77
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 78
void setValue (String name, int value) { if (name.equals(“height”)) { _height = value; return; } if (name.equals(“width”)) { _width = value; return; } assert false; } // change to new methods: void setHeight (int arg) { _height = arg; } void setWidth (int arg) { _width = arg; }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 79
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 80
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 81
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 82
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 83
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 84
class Room … boolean withinPlan (HeatingPlan plan) { int low = daysTempRange().getLow(); int high = daysTempRange().getHigh(); return plan.withinRange(low, high); }
class HeatingPlan … private TempRange _range; boolean withinRange (int low, int high) { return (low >= _range.getLow() && high <= _range.getHigh()); }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 91
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 92
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 93
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 94
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 95
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 96
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 97
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 98
Makes intent clear Removes possibility of change
Mostly obvious, so we skip In a subclass, may need a setter in superclass for setting some private fields, but give it a name that makes clear it is not an ordinary setter
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 99
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 100
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 101
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 102
But clone() is a good example that remains Recall that we put the downcast inside the memento() method
(We also put an exception handler there: same principle)
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 109
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 110
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 111
class ResourcePool … private ArrayList
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 112
class ResourcePool … private ArrayList
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 113
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 114