Change Function Declaration, ํ•จ์ˆ˜ ์„ ์–ธ ๋ฐ”๊พธ๊ธฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

function circum(radius) { ... }
function circumference(radius) { ... }

๋ฐฐ๊ฒฝ

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

์ด๋ฆ„์ด ์•ˆ๋‚˜์˜ฌ ๋•Œ ํ•จ์ˆ˜์˜ ๋™์ž‘์„ ์ฃผ์„์œผ๋กœ ์จ๋ณด์ž. ๊ทธ ์ฃผ์„์—์„œ ํžŒํŠธ๋ฅผ ์–ป์–ด ์ด๋ฆ„์œผ๋กœ ๋Œ€์ฒดํ•˜์ž.

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

์ ˆ์ฐจ

๊ฐ„๋‹จํ•œ ์ ˆ์ฐจ

  1. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ฑฐ๋“  ๋จผ์ € ํ•จ์ˆ˜ ๋ณธ๋ฌธ์—์„œ ์ œ๊ฑฐ ๋Œ€์ƒ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ณณ์€ ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  2. ๋ฉ”์„œ๋“œ ์„ ์–ธ์„ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋ฐ”๊พผ๋‹ค.
  3. ๊ธฐ์กด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋ฌธ์„ ์ƒˆ๋กœ์šด ํ˜•ํƒœ๋กœ ๋งž์ถ˜๋‹ค.
  4. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  • ๋ณ€๊ฒฝํ•  ๊ฒŒ ๋‘˜ ์ด์ƒ(๋งค๊ฐœ๋ณ€์ˆ˜ ์ œ๊ฑฐ, ๋ฉ”์„œ๋“œ ์„ ์–ธ ๋ฐ”๊พธ๊ธฐ)์ด๋ผ๋ฉด ํ•˜๋‚˜์”ฉ ๋‚˜๋ˆ ์„œ ํ•˜์ž.

๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ ˆ์ฐจ

  1. ์ด์–ด์ง€๋Š” ์ถ”์ถœ ๋‹จ๊ณ„๋ฅผ ์ˆ˜์›”ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค๋ฉด ํ•จ์ˆ˜ ๋ณธ๋ฌธ์„ ์ ์ ˆํžˆ ๋ฆฌํŒฉํ† ๋งํ•œ๋‹ค.
  2. ํ•จ์ˆ˜ ๋ณธ๋ฌธ์„ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•œ๋‹ค.
    • ์ด ๊ฒฝ์šฐ ์ถ”์ถœํ•  ์ด๋ฆ„์ด ์ด์ „ ํ•จ์ˆ˜์™€ ์ด๋ฆ„์ด ๊ฐ™์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์ž„์‹œ ์ด๋ฆ„์„ ์„ ์ •ํ•˜์ž.
  3. ์ถ”์ถœํ•œ ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๊ฐ„๋‹จํ•œ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  4. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  5. ๊ธฐ์กด ํ•จ์ˆ˜๋ฅผ ์ธ๋ผ์ธํ•œ๋‹ค.
  6. ์ด๋ฆ„์„ ์ž„์‹œ ์ด๋ฆ„์—์„œ ์›๋ž˜ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
  7. ํ…Œ์ŠคํŠธํ•œ๋‹ค.

๋”ฐ๋ผํ•˜๊ธฐ

ํ•จ์ˆ˜ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ(๊ฐ„๋‹จํ•œ ์ ˆ์ฐจ)

function circum(radius) {
    return 2 * Math.PI * radius;
}
function circumference(radius) {
    return 2 * Math.PI * radius;
}
  • ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๋„ˆ๋ฌด ์ถ•์•ฝํ•œ ์ผ€์ด์Šค์ด๋‹ค.
  • ์š”์ฆ˜์„ IDE์—์„œ ์ฐพ์•„์ฃผ๋‹ˆ ์ด๋Ÿฐ๊ฑด ๊ธˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ(๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ ˆ์ฐจ)

function circum(radius) {
    return 2 * Math.PI * radius;
}
function circum(radius) {
    return circumference(radius);
}
 
function circumference(radius) {
    return 2 * Math.PI * radius;
}
  • ์ด์ „์— ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ์ธ circum์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„๋งŒ์„ ๋Œ€์ฒดํ•˜์—ฌ ํ•˜์œ„ ๋ฒ„์ „ ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ–ˆ๋‹ค.
  • ๊ณต๊ฐœ API์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
  • deprecated๋กœ ์ฒ˜๋ฆฌํ•ด๋‘๊ณ , ํŠน์ • ์‹œ์ ์— ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ

  • ๋„์„œ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์˜ Book ํด๋ž˜์Šค์— ์˜ˆ์•ฝ ๊ธฐ๋Šฅ์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž.
// Book Class
addReservation(custormer) {
    this._reservations.push(customer);
}
  • ๊ทธ๋Ÿฐ๋ฐ ์˜ˆ์•ฝ์‹œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ง€์›ํ•˜๋ผ๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ’์„ ๋ฐ›์•„ ๋ถ„๊ธฐ์ณ์„œ ๋„ฃ์„ ์˜ˆ์ •์ด๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด addReservation์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ณณ์—์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.
  • ์ ์œผ๋ฉด ๊ทธ๋ƒฅ ๋‹ค ๋ฐ”๊พธ๊ณ , ๋งŽ๋‹ค๋ฉด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ ˆ์ฐจ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋‘๋ฒˆ์งธ ์ƒํ™ฉ์ด๋ผ๊ณ  ํ•ด๋ณด์ž.
// Book Class
addReservation(customer) {
    this.zz_addReservation(customer, false);
}
 
zz_addReservation(customer, isPriority) {
    this._reservations.push(customer);
}
  • ์ผ๋‹จ ํ•จ์ˆ˜๋ฅผ ์ถ”์ถœํ•ด๋‘์ž.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ์“ธ ์ˆ˜ ์—†์–ด ์ž„์‹œ ์ด๋ฆ„์œผ๋กœ ๋ถ™์˜€๋‹ค.
// Book Class
addReservation(customer) {
    this.zz_addReservation(customer, false);
}
 
zz_addReservation(customer, isPriority) {
    this._reservations.push(customer);
}
  • ์ƒˆ ํ•จ์ˆ˜์˜ ์„ ์–ธ๋ฌธ๊ณผ ํ˜ธ์ถœ๋ฌธ์— ์›ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ๋‹ค์Œ์œผ๋กœ๋Š” zz_addReservation ํ˜ธ์ถœ๋ถ€๋ฅผ ํ•จ์ˆ˜ ์ธ๋ผ์ธํ•œ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์†์„ฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ

  • ์ด๋ฒˆ์—” ์ข€๋” ๋ณต์žกํ•œ ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด์ž
function inNewEngland(aCustomer) {
    return ["MA", "CT", "ME", "VT", "NH", "RI"].includes(aCustomer.address.state);
}
 
// ํ˜ธ์ถœ๋ฌธ
 
const newEnglanders = someCustomers.filter(c => inNewEngland(c));
  • ์ด ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๊ณ ๊ฐ์ด ๋‰ด์ž‰๊ธ€๋žœ๋“œ์— ์‚ฌ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • ์ด ๊ฒฝ์šฐ customer ์ž์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ  ์žˆ์–ด ์ปคํ”Œ๋ง์ด ์‹ฌํ•˜๋‹ค.
  • ์ฃผ ์‹๋ณ„ ์ฝ”๋“œ๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค๋ฉด ์˜์กด์„ฑ์ด ์ œ๊ฑฐ๋˜์–ด ๋” ๋„“์€ ๋ฌธ๋งฅ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
function inNewEngland(aCustomer) {
    const stateCode = aCustomer.address.state;
    return ["MA", "CT", "ME", "VT", "NH", "RI"].includes(stateCode);
}
  • ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฆฌํŒฉํ„ฐ๋ง ํ•ด๋‘๊ณ , ํ•จ์ˆ˜ ์ถ”์ถœํ•˜๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์ž.
function inNewEngland(aCustomer) {
    const stateCode = aCustomer.address.state;
    return xxNEWinNewEngland(stateCode);
}
 
function xxNEWinNewEngland(stateCode) {
    return ["MA", "CT", "ME", "VT", "NH", "RI"].includes(stateCode);
}
  • ์ด์ œ ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์ธ๋ผ์ธํ•˜์ž.
function inNewEngland(aCustomer) {
    return xxNEWinNewEngland(aCustomer.address.state);
}
 
function xxNEWinNewEngland(stateCode) {
    return ["MA", "CT", "ME", "VT", "NH", "RI"].includes(stateCode);
}
  • ํ•จ์ˆ˜ ์ธ๋ผ์ธํ•˜๊ธฐ๋ฅผ ์ ์šฉํ•˜์ž. ๊ทธ๋ฆฌ๊ณ  ์ƒˆ ํ•จ์ˆ˜๋กœ ๊ต์ฒดํ•˜์ž.

Reference