Extract Function, ํ•จ์ˆ˜ ์ถ”์ถœํ•˜๊ธฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

// before
function printOwing(invoice) {
  printBanner();
  let outstanding = calculateOutstanding();
 
  // ์„ธ๋ถ€ ์‚ฌํ•ญ ์ถœ๋ ฅ
  console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
  console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
}
// after
function printOwing(invoice) {
  printBanner();
  let outstanding = calculateOutstanding();
 
  // ์„ธ๋ถ€ ์‚ฌํ•ญ ์ถœ๋ ฅ
  printDetails(outstanding);
 
  function printDetails(outstanding) {
    console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
    console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
  }
}

๋ฐฐ๊ฒฝ

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

์ ˆ์ฐจ

  1. ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  ๋ชฉ์ ์„ ๋“œ๋Ÿฌ๋‚ด๋Š” ์ด๋ฆ„์„ ๋ถ™์ธ๋‹ค.
    • ์–ด๋–ป๊ฒŒ์— ์ง‘์ค‘ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. ๋ฌด์—‡์„ ํ•˜๋Š”์ง€๊ฐ€ ๋“œ๋Ÿฌ๋‚˜์•ผ ํ•œ๋‹ค.
    • ๋ชฉ์ ์ด ๋” ์ž˜ ๋“œ๋Ÿฌ๋‚œ๋‹ค๋ฉด ํ•จ์ˆ˜์˜ ๊ธธ์ด๋Š” ์ค‘์š”์น˜ ์•Š๋‹ค. ํ•œ์ค„๋„ ์ƒ๊ด€์—†๋‹ค.
  2. ์ถ”์ถœํ•  ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ ํ•จ์ˆ˜์— ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.
  3. ์ถ”์ถœํ•œ ์ฝ”๋“œ ์ค‘ ์›๋ณธ ํ•จ์ˆ˜์˜ ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ์ถ”์ถœํ•œ ํ•จ์ˆ˜์˜ ์œ ํšจ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๋ณ€์ˆ˜๋Š” ์—†๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ์žˆ๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค.
    • ์ค‘์ฒฉํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.
    • ์‚ฌ์šฉ์€ ํ•˜์ง€๋งŒ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๋ณ€์ˆ˜๋ผ๋ฉด ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ๋‹ค ๋ฐ›์•„๋ฒ„๋ฆฐ๋‹ค.
    • ์ถ”์ถœํ•œ ์ฝ”๋“œ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์„ ์–ธ์„ ๋ฐ–์—์„œ ํ•œ๋‹ค๋ฉด ์•ˆ์œผ๋กœ ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
    • ์ถ”์ถœํ•œ ์ฝ”๋“œ ์•ˆ์—์„œ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๋ณ€์ˆ˜ ์ค‘์—์„œ ๊ฐ’์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฒƒ์€ ์ฃผ์˜ํ•˜์ž.
    • ๋„ˆ๋ฌด ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ์ถ”์ถœ์„ ๋ฉˆ์ถ”๊ณ  ๋” ์ž‘์€ ๋ฒ”์œ„์—์„œ ๋ฆฌํŒฉํ† ๋ง์„ ์‹œ๋„ํ•ด๋ณด์•„์•ผ ํ•œ๋‹ค.
  4. ๋ณ€์ˆ˜๋ฅผ ๋‹ค ์ฒ˜๋ฆฌํ–ˆ๋‹ค๋ฉด ์ปดํŒŒ์ผํ•œ๋‹ค.
  5. ์›๋ณธ ํ•จ์ˆ˜์—์„œ ์ถ”์ถœํ•œ ์ฝ”๋“œ ๋ถ€๋ถ„์„ ์ƒˆ๋กœ ๋งŒ๋“  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฌธ์žฅ์œผ๋กœ ๋ฐ”๊พผ๋‹ค.
  6. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  7. ๋‹ค๋ฅธ ์ฝ”๋“œ์— ๋ฐฉ๊ธˆ ์ถ”์ถœํ•œ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™๊ฑฐ๋‚˜ ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ์—†๋Š”์ง€ ์‚ดํ•€๋‹ค. ์žˆ๋‹ค๋ฉด ๋ฐฉ๊ธˆ ์ถ”์ถœํ•œ ์ƒˆ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ๋ฐ”๊ฟ€์ง€ ๊ฒ€ํ† ํ•œ๋‹ค.
    • ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ถ”์ถœํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ๋ฐ”๊พธ๊ณ  ํ…Œ์ŠคํŠธํ•œ๋‹ค.
    • ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ์—†๋‹ค๋ฉด ์ถ”์ถœํ•œ ์ƒˆ ํ•จ์ˆ˜๋ฅผ ์ธ๋ผ์ธํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•œ๋‹ค.

์˜ˆ์‹œ

function printOwing(invoice) {
  let outstanding = 0;
 
  console.log('***********************');
  console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
  console.log('***********************');
 
  // ๋ฏธํ•ด๊ฒฐ ์ฑ„๋ฌด(outstanding)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  // ๋งˆ๊ฐ์ผ(dueDate)์„ ๊ธฐ๋กํ•œ๋‹ค.
  const today = Clock.today;
  invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
 
  // ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
  console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
  console.log(`๋งˆ๊ฐ์ผ: ${invoice.dueDate.toLocaleDateString()}`);
}

1. ์œ ํšจ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๋•Œ

function printOwing(invoice) {
  let outstanding = 0;
 
  printBanner();
 
  // ๋ฏธํ•ด๊ฒฐ ์ฑ„๋ฌด(outstanding)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  // ๋งˆ๊ฐ์ผ(dueDate)์„ ๊ธฐ๋กํ•œ๋‹ค.
  const today = Clock.today;
  invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
 
  printDetails();
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
 
  function printDetails() {
    console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
    console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
    console.log(`๋งˆ๊ฐ์ผ: ${invoice.dueDate.toLocaleDateString()}`);
  }
}

2. ์ง€์—ญ ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉํ•  ๋•Œ

function printOwing(invoice) {
  let outstanding = 0;
 
  printBanner();
 
  // ๋ฏธํ•ด๊ฒฐ ์ฑ„๋ฌด(outstanding)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  // ๋งˆ๊ฐ์ผ(dueDate)์„ ๊ธฐ๋กํ•œ๋‹ค.
  const today = Clock.today;
  invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
 
  printDetails(invoice, outstanding);
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
}
 
function printDetails(invoice, outstanding) {
  console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
  console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
  console.log(`๋งˆ๊ฐ์ผ: ${invoice.dueDate.toLocaleDateString()}`);
}
  • ์ค‘์ฒฉ ํ•จ์ˆ˜์—์„œ ์ง€์—ญ ํ•จ์ˆ˜๋กœ ๋นผ๋ฒ„๋ ธ๋‹ค.
  • ๊ทธ ๊ณผ์ •์—์„œ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋„๋ก ๋ณ€๊ฒฝํ–ˆ๋‹ค.
function printOwing(invoice) {
  let outstanding = 0;
 
  printBanner();
 
  // ๋ฏธํ•ด๊ฒฐ ์ฑ„๋ฌด(outstanding)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
}
 
function recordDueDate(invoice) {
  const today = Clock.today;
  invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
}
 
function printDetails(invoice, outstanding) {
  console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
  console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
  console.log(`๋งˆ๊ฐ์ผ: ${invoice.dueDate.toLocaleDateString()}`);
}
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งˆ๊ฐ์ผ ์„ค์ • ๋กœ์ง๋„ ์ถ”์ถœํ•œ๋‹ค.

3. ์ง€์—ญ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ

  • ์ง€์—ญ๋ณ€์ˆ˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๊นŒ๋‹ค๋กœ์›Œ์ง„๋‹ค.
  • ๋‹น์žฅ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ๋ณด๊ณ  ๋ณต๊ธฐํ•ด๋ณด์ž.
function printOwing(invoice) {
  let outstanding = 0;
 
  printBanner();
 
  // ๋ฏธํ•ด๊ฒฐ ์ฑ„๋ฌด(outstanding)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
}
function printOwing(invoice) {
  printBanner();
 
  let outstanding = 0;
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
}
  1. ์ผ๋‹จ ๋ณ€์ˆ˜ ์„ ์–ธ๋ฌธ์„ ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ ๊ทผ์ฒ˜๋กœ โ€œ์Šฌ๋ผ์ด๋“œโ€ํ•œ๋‹ค.
function printOwing(invoice) {
  printBanner();
 
  let outstanding = 0;
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
 
  function printBanner() {
    console.log('***********************');
    console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
    console.log('***********************');
  }
}
 
function calculateOutstanding(invoice) {
  let outstanding = 0;
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
  return outstanding;
}
  1. ์ผ๋‹จ ์ถ”์ถœํ•  ๋Œ€์ƒ ์ฝ”๋“œ๋ฅผ ๋ฝ‘์•„์„œ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
function printOwing(invoice) {
  printBanner();
 
  let outstanding = calculateOutstanding(invoice);
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
}
 
function printBanner() {
  console.log('***********************');
  console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
  console.log('***********************');
}
 
function calculateOutstanding(invoice) {
  let outstanding = 0;
  for (const o of invoice.orders) {
    outstanding += o.amount;
  }
  return outstanding;
}
  1. outstading ๋ณ€์ˆ˜๋Š” ๋ณ€๊ฒฝ๋˜๋Š” ๊ณณ์ด ์ œํ•œ์ ์ด๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์„œ ๋ณ€๊ฒฝํ•œ๋‹ค.
function printOwing(invoice) {
  printBanner();
 
  const outstanding = calculateOutstanding(invoice);
 
  recordDueDate(invoice);
  printDetails(invoice, outstanding);
}
 
function printBanner() {
  console.log('***********************');
  console.log('**** ๊ณ ๊ฐ ์ฑ„๋ฌด ****');
  console.log('***********************');
}
 
function calculateOutstanding(invoice) {
  let result = 0;
  for (const o of invoice.orders) {
    result += o.amount;
  }
  return result;
}
 
function recordDueDate(invoice) {
  const today = Clock.today;
  invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
}
 
function printDetails(invoice, outstanding) {
  console.log(`๊ณ ๊ฐ๋ช…: ${invoice.customer}`);
  console.log(`์ฑ„๋ฌด์•ก: ${outstanding}`);
  console.log(`๋งˆ๊ฐ์ผ: ${invoice.dueDate.toLocaleDateString()}`);
}
  1. ์ฝ”๋”ฉ ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

Reference