Replace Nested Conditional With Guard Clauses, ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ์„ ๋ณดํ˜ธ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

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();
}

๋ฐฐ๊ฒฝ

  • ์กฐ๊ฑด๋ฌธ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‘๊ฐ€์ง€๋‹ค.
    • ์ฐธ์ด๊ฑฐ๋‚˜ ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ ๋ชจ๋‘๊ฐ€ ์ •์ƒ๋™์ž‘์œผ๋กœ ์ด์–ด์ ธ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • ์ฐธ์ธ ๊ฒฝ์šฐ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
  • ์œ„ ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ๋Š” ์˜๋„ํ•˜๋Š” ๋ฐ”๊ฐ€ ๋‹ค๋ฅด๋‹ˆ, ์ด ์˜๋„๊ฐ€ ํ˜•ํƒœ๋กœ ๋“œ๋Ÿฌ๋‚˜๋Š” ๊ฒƒ์ด ์˜ณ๋‹ค.
  • swift์—์„œ๋Š” guard ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ ˆ์ฐจ

  1. ๊ต์ฒดํ•ด์•ผ ํ•  ์กฐ๊ฑด ์ค‘ ๊ฐ€์žฅ ๋ฐ”๊นฅ ๊ฒƒ์„ ์„ ํƒํ•˜์—ฌ ๋ณดํ˜ธ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐ”๊พผ๋‹ค.
  2. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  3. 1~2๋ฅผ ํ•„์š”ํ•œ ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.
  4. ๋ชจ๋“  ๋ณดํ˜ธ ๊ตฌ๋ฌธ์ด ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด, ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ฉํ•œ๋‹ค.

์˜ˆ์‹œ

function payAmount(employee) {
    let result;
    if (employee.isSeparated) result = {amount: 0, reasonCode: "SEP"};
    else {
        if (employee.isRetired) result = {amount: 0, reasonCode: "RET"};
        else {
            // ๊ธ‰์—ฌ ๊ณ„์‚ฐ ๋กœ์ง
            lorem.ipsum(dolor.sitAmet);
            consectetur(adipiscing).elit();
            sed.do.eiusmod = tempor.incididunt.ut(labore) && dolore(magna.aliqua);
            ut.enim.ad(minim.veniam);
            result = someFinalComputation();
        }
    }
    return result;
}
  • ๋„๋Œ€์ฒด ๋ญ˜ํ•˜๋Š”์ง€ ํ™•์—ฐํžˆ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ด ์ด์œ ๋Š” ์ค‘์ฒฉ๋œ ์กฐ๊ฑด๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ์ž˜ ๋ณด๋ฉด, ๋ชจ๋“  ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ธ ์ƒํ™ฉ์— ๊ณ„์‚ฐ์„ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
function payAmount(employee) {
    let result;
    if (employee.isSeparated) return {amount: 0, reasonCode: "SEP"};
    if (employee.isRetired) {
        result = {amount: 0, reasonCode: "RET"};
    }
    else {
        // ๊ธ‰์—ฌ ๊ณ„์‚ฐ ๋กœ์ง
        lorem.ipsum(dolor.sitAmet);
        consectetur(adipiscing).elit();
        sed.do.eiusmod = tempor.incididunt.ut(labore) && dolore(magna.aliqua);
        ut.enim.ad(minim.veniam);
        result = someFinalComputation();
    }
    return result;
}
  • ์ผ๋‹จ ๊ฐ€์žฅ ์ฒ˜์Œ์— ๋งŒ๋‚˜๋Š” ์กฐ๊ฑด์— ๋Œ€ํ•ด result์— ๋‹ด์ง€ ์•Š๊ณ  return ์‹œ์ผฐ๋‹ค.
  • ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŒ๋‚˜๋Š” ์กฐ๊ฑด๋“ค์— ๋Œ€ํ•ด early return์„ ์‹œํ‚ค๋ฉด, ์ฝ”๋“œ๊ฐ€ ๋ณด๋‹ค ๋ช…ํ™•ํ•ด์ง„๋‹ค.
function payAmount(employee) {
    if (employee.isSeparated) return {amount: 0, reasonCode: "SEP"};
    if (employee.isRetired) return {amount: 0, reasonCode: "RET"};
    // ๊ธ‰์—ฌ ๊ณ„์‚ฐ ๋กœ์ง
    lorem.ipsum(dolor.sitAmet);
    consectetur(adipiscing).elit();
    sed.do.eiusmod = tempor.incididunt.ut(labore) && dolore(magna.aliqua);
    ut.enim.ad(minim.veniam);
    return someFinalComputation();
}

Reference