Open leeyuunsung opened 2 years ago
public class Customer { private Double discountRate; public double applyDiscount(double amount) { return (this.discountRate != null) ? amount - (this.discountRate * amount) : amount; } public Double getDiscountRate() { return discountRate; } public void setDiscountRate(Double discountRate) { this.discountRate = discountRate; } }
applyDiscount()
setDiscountRate()
public class Customer { private Double discountRate; public double applyDiscount(double amount) { return (this.discountRate != null) ? amount - (this.discountRate * amount) : amount; } public Double getDiscountRate() { return discountRate; } public void setDiscountRate(Double discountRate) { assert discountRate != null && discountRate > 0; this.discountRate = discountRate; } }
AssertionError
-ea
냄새 24. 주석 (Comments)
리팩토링 43. 어서션 추가하기 (Introduce Assertion)
Before
applyDiscount()
메서드상에서 null 이 아님을 체크하고 있다setDiscountRate()
메서드에서 값을 대입하기 전에 assert 로 체크할 수 있다After
AssertionError
가 발생하는데-ea
옵션 (enable assertion) 이 켜 있기 때문이고