Download Refactoring Conditional Statements: Consolidating, Decomposing, Replacing and more Study notes Computer Science in PDF only on Docsity!
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
24 - Refactoring: Simplifying Conditional
Expressions and Method Calls
CMPSCI 220 (291A)
Programming Methodology
Spring 2009
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Goal for the week
Build your catalog of familiar refactorings
Increase sense of incremental nature of refactoring
Today: Conditional Expressions (REF 9) and MethodCalls (REF 10)
Next time: Composing methods (REF 6), Movingfeatures around (REF 7), Data (REF 8)
Later: Generalization (REF 12) and Big Refactorings(REF 13)
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Decompose Conditional
Before: if (date.before(SUMMER_START) || date.after(SUMMER_END))
charge = quantity * _winterRate + _winterServiceCharge;
else charge = quantity * _summerRate; After: if (notSummer(date))
charge = winterCharge(quantity);
else charge = summerCharge(quantity);
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Decompose Conditional
Motivation: Reasons why things happen getobscured in details of condition(al)s
Mechanics
Extract condition to its own method
Extract then and else parts to their own methods
While the methods are often short, just the use ofnames like notSummer and winterCharge make thecode much clearer
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Consolidate Conditional Expression
Motivations:
Clarifies that the sequence is really
one
check of an “or” of
conditions
Often sets the stage for Extract Method
Mechanics:
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
Note: Can do the same thing with and-ed conditionals, etc.
Can sometimes use a ternary (? :) expression in a return
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Consolidate Duplicate ConditionalFragments
Before: if
(isSpecialDeal())
{
total
=
price
0.95;
send();
}
else
{
total
=
price
0.98;
send();
} After: if
(isSpecialDeal())
{
total
=
price
0.95;
}
else
{
total
=
price
0.98;
} send();
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag
There is a variable acting as a control flag for aseries of boolean expressions; use
break
or
return
instead.
Motivation:
Code arises from one-entry/one-exit style of coding
As usual, refactoring improves clarity
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag
Generic structure being replaced:
set
done
to
false
while not done
if (condition)
do somethingset done to true
next step of loop
Mechanics
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
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 1 - Start
void checkSecurity
(String[] people) {
boolean found = false;for (String person : people) {
if (!found) {
if (person.equals(“Don”)) {
sendAlert();found = true;
} if (person.equals(“John”)) {
sendAlert();found = true;
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 1 – After first
break
void checkSecurity
(String[] people) {
boolean found = false;for (String person : people) {
if (!found) {
if (person.equals(“Don”)) {
sendAlert();break;
} if (person.equals(“John”)) {
sendAlert();found = true;
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 1 – Remove flag variable
void checkSecurity
(String[] people) {
boolean found = false;for (String person : people) {
if (!found) {
if (person.equals(“Don”)) {
sendAlert();break;
} if (person.equals(“John”)) {
sendAlert();break;
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 1 – End
void checkSecurity (String[] people) {
for (String person : people) {
if (person.equals(“Don”)) {
sendAlert();break;
} if (person.equals(“John”)) {
sendAlert();break;
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 2 – After Extract Method
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
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Remove Control Flag:Example 2 – After first
return
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;