-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtractMethod.java
73 lines (62 loc) · 2.04 KB
/
ExtractMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ch6;
import java.util.Enumeration;
/*
* You have a code fragment that can be grouped together
*
* Turn the fragment into a method whose name explains the purpose of the method
*
* - New method name is important, name it by what it does, not by how it does it.
* If you can't come up with a more meaningful name, don't extract the code
*
* - One method better return zero or one variable, if one method need to return multiple variable,
* best option usually is to pick different code to extract
*/
public class ExtractMethod {
void printOwing() {
Enumeration<E> e = _orders.elements();
double outstanding = 0.0;
// print banner
System.out.println("***************************");
System.out.println("***** Customer Owes *******");
System.out.println("***************************");
// calculate outstanding
while (e.hasMoreElements()) {
Order each = (Order) e.nextElement();
outstanding += each.getAmount();
}
// print details
System.out.println("name:" + _name);
System.out.println("amount" + outstanding);
}
void printOwingRefactored() {
// No Local Variables
printBanner();
// Reassigning a Local Variable - variable not been used after the code is
// extracted then just return
// variable been used afterward, need to return
double outstanding = getOutstanding();
// Using Local Variables
printDetails(outstanding);
}
void printBanner() {
// print banner
System.out.println("***************************");
System.out.println("***** Customer Owes *******");
System.out.println("***************************");
}
void printDetails(double outstanding) {
// print details
System.out.println("name:" + _name);
System.out.println("amount" + outstanding);
}
double getOutstanding() {
Enumeration<E> e = _orders.elements();
double result = 0.0;
// Calculate outstanding
while (e.hasMoreElements()) {
Order each = (Order) e.nextElement();
result += each.getAmount();
}
return result;
}
}