반응형
before
public class ReplaceParameterWithMethodCall_before {
static public int quantity;
static public int itemPrice;
static public double getPrice() {
int basePrice = quantity * itemPrice;
int discountLevel;
if (quantity > 100)
discountLevel = 2;
else
discountLevel = 1;
double finalPrice = discountedPrice(basePrice, discountLevel);
return finalPrice;
}
static private double discountedPrice(int basePrice, int discountLevel) {
if (discountLevel == 2)
return basePrice * 0.1;
else
return basePrice * 0.05;
}
public static void main(String[] args) {
quantity = 10;
itemPrice = 2500;
System.out.println("price : " + getPrice());
}
}
after
public class ReplaceParameterWithMethodCall_before {
static public int quantity;
static public int itemPrice;
static public double getPrice() {
int basePrice = quantity * itemPrice;
double finalPrice = discountedPrice(basePrice);
return finalPrice;
}
static private double discountedPrice(int basePrice) {
if (quantity > 100) return basePrice * 0.1;
return basePrice * 0.05;
}
public static void main(String[] args) {
quantity = 10;
itemPrice = 2500;
System.out.println("price : " + getPrice());
}
}
반응형
'Design Architecture > Refactoring' 카테고리의 다른 글
Replace Parameter with Explicit Methods (0) | 2022.04.20 |
---|---|
Introduce Parameter Object (0) | 2022.04.20 |
Separate Query From Modifier (0) | 2022.04.20 |
Replace Nested Conditional With Guard Clauses (0) | 2022.04.20 |
Decompose Conditional (0) | 2022.04.20 |