좋은 API는 데이터를 갱신하는 함수그저 조회만하는 함수를 명확하게 구분한다.


11.1 질의 함수와 변경 함수 분리하기

리팩토링 전😅

function getTotalOutstandingAndSendBill() {
    const result = customer.invoices.reduce((total, each) => each.amount + total, 0);
    sendBill();
    return result;
}
function sendBill() {
    emailGateway.send(formatBill(customer));
}

리팩토링 후😇 - 부수효과 제거

function getTotalOutstandingAndSendBill() {
    const result = customer.invoices.reduce((total, each) => each.amount + total, 0);
}
function sendBill() {
    emailGateway.send(formatBill(customer));
}

11.2 함수 매개변수화하기

리팩토링 전😅

function tenPercentRaise(aPerson) {
    aPerson.salary = aPerson.salary.multiply(1.1);
}
function fivePercentRaise(aPerson) {
    aPerson.salary = aPerson.salary.multiply(1.05);
}

리팩토링 후😇 - 다른 값만 매개변수 처리

function raise(aPerson, factor) {
    aPerson.salary = aPerson.salary.multiply(1 + factor);
}