Combine Functions into Transform, ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋กœ ๋ฌถ๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

function base(aReading) {...}
function taxableCharge(aReading) {...}
function enrichReading(argReading) {
    const aReading = _.cloneDeep(argReading);
    aReading.baseCharge = base(aReading);
    aReading.taxableCharge = taxableCharge(aReading);
    return aReading;
}

๋ฐฐ๊ฒฝ

  • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ˜•ํƒœ์˜ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค.
  • ์ด๋Ÿด ๊ฒฝ์šฐ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ค‘๋ณต๋กœ์ง์ด ๋˜๋ฒ„๋ฆฐ๋‹ค.
  • ์ด๋Ÿด ๊ฒฝ์šฐ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋กœ ๋ฌถ์–ด๋ฒ„๋ฆฌ๋ฉด ์—ฌ๋Ÿฌ๊ณณ์—์„œ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ˆ์ฐจ

  1. ๋ณ€ํ™˜ํ•  ๋ ˆ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ณ€ํ™˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.
    • ์ด ์ž‘์—…์€ ๋Œ€์ฒด๋กœ ๊นŠ์€ ๋ณต์‚ฌ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
    • ๋ณ€ํ™˜ ํ•จ์ˆ˜๊ฐ€ ์›๋ณธ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ๋งˆ๋ จํ•ด๋‘๋ฉด ์ข‹๋‹ค.
  2. ๋ฌถ์„ ํ•จ์ˆ˜ ์ค‘ ํ•จ์ˆ˜ ํ•˜๋‚˜๋ฅผ ๊ณจ๋ผ์„œ ๋ณธ๋ฌธ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋กœ ์˜ฎ๊ธด๋‹ค.
    • ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๊ฐ€ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•œ๋‹ค.
    • ๋กœ์ง์ด ๋ณต์žกํ•˜๋ฉด ํ•จ์ˆ˜ ์ถ”์ถœํ•˜๊ธฐ ๋ถ€ํ„ฐ ํ•œ๋‹ค.
  3. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  4. ๋‚˜๋จธ์ง€ ํ•จ์ˆ˜๋„ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

์˜ˆ์‹œ

reading = {customer: "ivan", quantity: 10, month: 5, year: 2017};
 
// Client 1
const aReading = aquireReading();
const baseCharge = baseRate(aReading.month, aReading.year) * aReading.quantity;
 
// Client 2
const aReading = aquireReading();
const base = (baseRate(aReading.month, aReading.year) * aReading.quantity);
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year));
 
// Client 3
const aReading = aquireReading();
const basicChargeAmount = calculateBaseCharge(aReading);
 
function calculateBaseCharge(aReading) {
    return baseRate(aReading.month, aReading.year) * aReading.quantity;
}
  • ๊ณ„์‚ฐ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์ด๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ 3์—์„œ๋Š” ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ด ๋ชจ๋‘๋ฅผ ํ•˜๋‚˜์˜ ๋ณ€ํ™˜ ๋‹จ๊ณ„๋กœ ๋ชจ์•„๋ณด์ž.
function enrichReading(original) {
    const result = _.cloneDeep(original);
    result.baseCharge = calculateBaseCharge(result);
    result.taxableCharge = Math.max(0, result.baseCharge - taxThreshold(result.year));
    return result;
}
 
// Client 1
const rawReading = aquireReading();
const aReading = enrichReading(rawReading);
const baseCharge = aReading.baseCharge;
 
// Client 2
const rawReading = aquireReading();
const aReading = enrichReading(rawReading);
const taxableCharge = aReading.taxableCharge;
 
// Client 3
const rawReading = aquireReading();
const aReading = enrichReading(rawReading);
const basicChargeAmount = aReading.baseCharge;

์ž ์žฌ์  ๋ฌธ์ œ

  • ์ง€๊ธˆ์€ ์›๋ณธ ํ•„๋“œ ์ž์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๊ณ , ๋‚ด๋ณด๋‚ผ ๋•Œ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๋‚ด๋ณด๋‚ด๊ณ  ์žˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ ์›๋ณธ ํ•„๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.
  • ์ฐจ๋ผ๋ฆฌ ํด๋ž˜์Šค๋กœ ๋ฌถ์–ด์„œ ๋ฑ‰๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์ธ๋‹ค.

Reference