-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitTemporaryVariable.java
71 lines (60 loc) · 2.07 KB
/
SplitTemporaryVariable.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
package ch6;
/*
* You have a temporary variable assigned to more than once,
* but is not a loop variable nor a collecting temporary variable
*
* - Make a separate temporary variable for each assignment
* - Any variable with more than one responsibility should be replaced with
* a temp for each responsibility
*/
public class SplitTemporaryVariable {
double getDistanceTravelled(int time) {
double result;
double acc = _primaryForce / _mass;
int primaryTime = Math.min(time, _delay);
result = 0.5 * acc * primaryTime * primaryTime;
int secondaryTime = time - _delay;
if (secondaryTime > 0) {
double primaryVel = acc * _delay;
acc = (_primaryForce + _secondaryForce) / _mass;
result += primaryVel * secondaryTime + 0.5 * acc * secondaryTime * secondaryTime;
}
return result;
}
double getDistanceTravelledRefactored(int time) {
double result;
final double primaryAcc = _primaryForce / _mass;
int primaryTime = Math.min(time, _delay);
result = 0.5 * primaryAcc * primaryTime * primaryTime;
int secondaryTime = time - _delay;
if (secondaryTime > 0) {
double primaryVel = primaryAcc * _delay;
final double secondaryAcc = (_primaryForce + _secondaryForce) / _mass;
result += primaryVel * secondaryTime + 0.5 * secondaryAcc * secondaryTime * secondaryTime;
}
return result;
}
double getDistanceTravelledRefactoredMore(int time) {
double result = 0.5 * getPrimaryAcc() * getPrimaryTime(time) * getPrimaryTime(time);
if (getSecondaryTime() > 0) {
result += getPrimaryVel() * getSecondaryTime(time)
+ 0.5 * getSecondaryAcc() * getSecondaryTime(time) * getSecondaryTime(time);
}
return result;
}
double getPrimaryTime(int time) {
return Math.min(time, _delay);
}
double getPrimaryAcc() {
return _primaryForce / _mass;
}
double getPrimaryVel() {
return getPrimaryAcc() * _delay;
}
double getSecondaryTime(int time) {
return time - _delay;
}
double getSecondaryAcc() {
return (_primaryForce + _secondaryForce) / _mass;
}
}