내가 주로 보는 코드리뷰 내용
계속해서 정리 vs 다 만들고 한번에 정리 (개인의 선호도)
- 복잡한 조건문 안 연산식
const isDateBefore = !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd)
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();
- 반복문 쪼개기
let youngest = people[0] ? people[0].age : Infinity;
let totalSalary = 0;
for (const p of people) {
if (p.age < youngest) youngest = p.age;
totalSalary += p.salary;
}
return `최연소: ${youngest}, 총 급여 : ${totalSalary}`;
function totalSalary() {
return people.reduce((total, p) => total + p.salary, 0);
}
function youngestAge() {
return Math.min(...people.map((p) => p.age));
}
- 임시 변수 질의로 바꾸기
class Order {
constructor(quantity, item) {
this._quantity = quantity;
this._item = item;
}
get price() {
const basePrice = this.quantity * this._item.price;
let discountFactor = 0.98;
if (basePrice > 1000) {
discountFactor -= 0.03;
}
return basePrice * discountFactor;
}
}
class Order {
constructor(quantity, item) {
this._quantity = quantity;
this._item = item;
}
get price() {
return this.basePrice * this.discountFactor;
}
get basePrice() {
return this.quantity * this._item.price;
}
get discountFactor() {
let discountFactor = 0.98;
if (this.basePrice > 1000) {
discountFactor -= 0.03;
}
return discountFactor;
}
}
- 함수 선언식을 자주 활용하기 (map, filter, foreach)