아래 내용은 리팩터링 2판 내용과 한달한권 읽기 강의를 기반하여 정리한 글입니다.
//전
if(!aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd))
charge = quantity * plan.summerRate;
else
charge = quantity * plan.regularRate + plan.regularServiceCharge;
//후
if(summer())
charge = summerCharge();
else
charge = regularCharge();
//전
if(anEmployee.seniority < 2) return 0;
if(anEmployee.monthsDisabled > 12) return 0;
if(anEmployee.isPartTime) return 0;
//후
if(isNotEligibleForDisability()) return 0;
function isNotEligibleForDisability() {
return ( (anEmployee.seniority<2)
|| (anEmployee.monthsDisabled > 12)
|| (anEmployee.isPartTime));
}
//전
function getPayAmount() {
let result;
if(isDead)
result = deadAmount();
else {
if(isSeparated)
result = separatedAmount();
else {
if(isRetired)
result = retiredAmount();
else
result = normalPayAmount();
}
}
}
return result;
//후(보호구문 적용결과)
function getPayAmount() {
if(isDead) return deadAmount();
if(isSeparated) return separatedAmount();
if(isRetired) return retiredAmount();
return normalPayAmount();
}
//전
switch(bird.type) {
case 'EuropeanSwallow'
return "보통이다";
case 'AfricanSwallow'
return (bird.numberOfCoconuts > 2) ? "지쳤다" : "보통이다";
case 'NorwegianBlueParrot'
return (bird.voltage > 100) ? "그을렸다" : "예쁘다";
default:
return "알 수 없다";
}
//후
class EuropeanSwallow {
get plumage() {
return "보통이다";
}
class AfricanSwallow {
get plumage() {
return (bird.numberOfCoconuts > 2) ? "지쳤다" : "보통이다";
}
class NorwegianBlueParrot {
get plumage() {
return (bird.voltage > 100) ? "그을렸다" : "예쁘다";
}
}
// 전
function blahblah() {
....
let customerName;
if(aCustomer === "미확인 고객") customerName = "거주자";
else customerName = aCustomer.name;
}
//후
function blahblah() {
...
return aCustomer.name
}
class UnkwonCustomer {
get name() {return "거주자";}
}
// 전
if( this.discountRate) {
base = base - (this.discountRate * base);
}
// 후
assert(this.discountRate >= 0)
if(this.discountRate) {
base = base - (this.discountRate * base);
}
//전
for(const p of people) {
if(!found) {
if(p === "조커"){
sendAlert();
found = true;
}
`if(p === "사루만"){
sendAlert();
found = true;
}
}
}
//후
for(const p of people) {
if(!found) {
if(p === "조커"){
sendAlert();
return;
}
if(p === "사루만"){
sendAlert();
return;
}
}
}
//더 나은 버젼
if (people.some(p => ["조커","사루만"].includes(p)))) sendAlert();