Study : Refactoring by Martin Fowler(1)

Kerem Song·2021년 3월 22일
0

Refactoring - Study

목록 보기
1/22

Refactoring

Why should I do refactoring?

for what? =>
1. In my case, to solve problems about complex, dirty and heavy codes.
2. To understand easily

then, What is the Refactoring?
In the book, Martin Fowler said that is
"making design better after coding"

Let's start.

function statement(invoice, plays) {
    let totalAmount = 0;
    let volumeCredits = 0;
    let result = '청구 내역( 고객명: ${invoice.customer}\n';

    const format = new Intl.NumberForamt("en-US",
        {
            style: "currency", currency: "USD",
            minimumFractionDigits: 2
        }).format;

    for (let perf of invoice.performances) {
        const play = plays[perf.playID];
        let thisAmount = 0;

        switch (play.type) {
            case "tragedy":
                thisAmount = 40000;
                if(perf.audience > 40) {
                    thisAmount += 1000 * (perf.audience - 30);
                }
                break;
            case "comedy":
                thisAmount = 30000;
                if(perf.audience > 20) {
                    thisAmount += 10000 + 500 * (perf.audience - 20);
                }
                thisAmount += 300 * perf.audience;
                break;
            default:
                throw new Error(`Unknown Genre: ${play.type}`);


        }
        //save points
        volumeCredits += Math.max(perf.audience - 30, 0);
        //give more points per 5people
        if("comedy" === play.type) volumeCredits += Math.floor(pef.audience / 5);

        //print Total Amount and Volume credits
        result += `${play.name}: ${format(thisAmount/100)} (${perf.audience})\n`;
        totalAmount += thisAmount;
    }
    result += `Total Amount: ${format(totalAmount/100)}\n`;
    result += `Volume Credits: ${volumeCredits}points\n`;
    return result;
}

How about this code ?
Can you understand or recognize well?
In my opinion, That is little bit hard to understand.
Kinda "perf", "Intl". What is the perf?
If this book doesn't notice that is about plays and invoices, I wouldn't know "perf" is from the "performance".

And so long and complex with many variables.

First, I will study about principles about how to make code clean and nice conditions with refactoring ways in this book. and I want to introduce those ways for better codes.

profile
Tea and Dessert

0개의 댓글