是怎樣?
重構(gòu)前:
public int discount(int inputVal, int quantity, int yearToDate) {
if (inputVal > 50) {
inputVal -= 2;
}
if (quantity > 100) {
inputVal -= 1;
}
if (yearToDate > 10000) {
inputVal -= 4;
}
return inputVal;
}
重構(gòu)后:
> ```Java
public int discount(int inputVal, int quantity, int yearToDate) {
int result = inputVal;
if (inputVal > 50) {
result -= 2;
}
if (quantity > 100) {
result -= 1;
}
if (yearToDate > 10000) {
result -= 4;
}
return result;
}
如何做?
- 新建一個(gè)臨時(shí)變量 result 并初始化值為 inputVal拓哺,將所有對(duì) inputVal 賦值的地方都改為對(duì) result賦值。(新建臨時(shí)變量這一步可以使用android studio 快捷鍵 cmd+opt+v)
- 測試扎运。