Consolidate Conditional Expression, ์กฐ๊ฑด์‹ ํ†ตํ•ฉํ•˜๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

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

๋ฐฐ๊ฒฝ

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

์ ˆ์ฐจ

  1. ํ•ด๋‹น ์กฐ๊ฑด์‹๋“ค ๋ชจ๋‘์— ๋ถ€์ž‘์šฉ(side effect)๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    • ๋ถ€์ˆ˜ ํšจ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด, ํ•จ์ˆ˜๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•ด๋‘์ž.
  2. ์กฐ๊ฑด๋ฌธ ๋‘ ๊ฐœ๋ฅผ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค.
  3. ํ…Œ์ŠคํŠธ ํ•œ๋‹ค.
  4. ์กฐ๊ฑด์ด ํ•˜๋‚˜๋งŒ ๋‚จ์„ ๋–„๊นŒ์ง€ 2-3์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  5. ์ตœ์ข…์ ์œผ๋กœ ๋‚˜์˜จ ์กฐ๊ฑด์‹์„ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ• ์ง€ ๊ณ ๋ คํ•œ๋‹ค.

์˜ˆ์‹œ

function disabilityAmount(anEmployee) {
    if (anEmployee.seniority < 2) return 0;
    if (anEmployee.monthsDisabled > 12) return 0;
    if (anEmployee.isPartTime) return 0;
    // ์žฅ์•  ์ˆ˜๋‹น ๊ณ„์‚ฐ
}
  • ๊ฐ™์€ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” ์„ธ๊ฐœ์˜ ์กฐ๊ฑด๋ฌธ์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ์ด๋ฅผ ํ•˜๋‚˜์˜ ์‹์œผ๋กœ ํ†ตํ•ฉํ•˜์ž.
function disabilityAmount(anEmployee) {
    if (isNotEligibleForDisability()) return 0;
    // ์žฅ์•  ์ˆ˜๋‹น ๊ณ„์‚ฐ
}
 
function isNotEligibleForDisability() {
    return ((anEmployee.seniority < 2)
        || (anEmployee.monthsDisabled > 12)
        || (anEmployee.isPartTime));
}

Reference