Decompose Conditional, ์กฐ๊ฑด๋ฌธ ๋ถ„ํ•ดํ•˜๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

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

๋ฐฐ๊ฒฝ

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

์ ˆ์ฐจ

  • ์กฐ๊ฑด์‹๊ณผ ์กฐ๊ฑด์‹์— ๋”ธ๋ฆฐ ์กฐ๊ฑด์ ˆ ๊ฐ๊ฐ์„ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•œ๋‹ค.

์˜ˆ์‹œ

if (!aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd))
  charge = quantity * plan.summerRate;
else
  charge = quantity * plan.regularRate + plan.regularServiceCharge;
  • ์—ฌ๋ฆ„ ์ฒ ์ด๋ฉด ํ• ์ธ์œจ์ด ๋‹ฌ๋ผ์ง€๋Š” ์ฝ”๋“œ์ด๋‹ค.
if (summer())
    charge = quantity * plan.summerRate;
else
    charge = quantity * plan.regularRate + plan.regularServiceCharge;
 
function summer() {
    return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}
  • summer() ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์กฐ๊ฑด๋ฌธ์„ ๋ถ„๋ฆฌํ–ˆ๋‹ค.
if (summer())
    charge = summerCharge();
else
    charge = regularCharge();
 
function summer() {
    return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}
 
function summerCharge() {
    return quantity * plan.summerRate;
}
 
function regularCharge() {
    return quantity * plan.regularRate + plan.regularServiceCharge;
}
  • ๋‚˜๋จธ์ง€ ํ•จ์ˆ˜๋“ค๋„ ๋ชจ๋‘ ์ถ”์ถœํ–ˆ๋‹ค.

Reference