πŸ‘¨β€πŸ’» μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ”© μ»¨λ²€μ…˜κ³Ό ν…ŒμŠ€νŠΈ - OOP Study 1πŸ‘¨β€πŸ’» μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ”© μ»¨λ²€μ…˜κ³Ό ν…ŒμŠ€νŠΈ - OOP Study 1

πŸ‘¨β€πŸ’» μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ”© μ»¨λ²€μ…˜κ³Ό ν…ŒμŠ€νŠΈ - OOP Study 1

μ΅œμ’… νŽΈμ§‘ μΌμ‹œ
Last updated June 14, 2022
νƒœκ·Έ
Javascript
배포일자
Apr 20, 2020
4μ›”λΆ€ν„° μŠ€ν„°λ””λ₯Ό μ‹œμž‘ν–ˆλ‹€. μŠ€ν„°λ””λŠ” java μ–Έμ–΄λ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ λ‚˜λŠ” javascriptλ₯Ό μ‚¬μš©ν•΄μ„œ μ°Έμ—¬ν•˜κ³  μžˆλ‹€..!πŸ˜‚ μ‹€μ œ μ½”λ“œμ— λŒ€ν•œ ν”Όλ“œλ°±μ€ 많이 받지 λͺ»ν•΄μ„œ μ•„μ‰½μ§€λ§Œ java와 javascript 각각의 μ–Έμ–΄μ˜ κ°œλ°œμ² ν•™λ„ 비ꡐ해볼 μˆ˜λ„ 있고 무엇보닀 언어에 κ΅­ν•œλ˜μ§€ μ•ŠλŠ” ν”„λ‘œκ·Έλž˜λ° κΈ°λ³Έ 원칙듀, μ½”λ“œ μ§œλŠ” 방법에 λŒ€ν•΄μ„œ 많이 생각해볼 수 μžˆλŠ” 쒋은 μ‹œκ°„μ΄ 될 것이라고 μƒκ°ν•œλ‹€. 맀주 μ•Œκ²Œλœ λ‚΄μš©μ΄λ‚˜ μ •λ¦¬ν• λ§Œ ν•œ λ‚΄μš©μ„ κΈ°λ‘ν•˜λ €ν•œλ‹€.

#. Series

μ‹œλ¦¬μ¦ˆ ν•œλˆˆμ—λ³΄κΈ°[μ ‘κΈ°/펼치기]

##. Source

κΉƒν—ˆλΈŒ μ†ŒμŠ€[μ ‘κΈ°/펼치기]

1. λ§€μ§λ„˜λ²„ μƒμˆ˜ν™”

λ§€μ§λ„˜λ²„λŠ” μ„€λͺ… 없이 λ¬΄μž‘μ • λ“±μž₯ν•˜λŠ” μƒμˆ˜λ₯Ό μ˜λ―Έν•œλ‹€.
if같은 μ œμ–΄λ¬Έμ΄λ‚˜Β for같은 λ°˜λ³΅λ¬Έμ—μ„œ 많이 λ“±μž₯ν•˜λ©°, λ§€μ§λ„˜λ²„λ₯Ό μ‚¬μš©ν•˜λ©΄ μ½”λ“œλ₯Ό μˆ˜μ •ν•  λ•Œ 일일이 λ‹€ μˆ˜μ •ν•΄μ•Όν•œλ‹€λŠ” λ²ˆκ±°λ‘œμ›€ 뿐만 μ•„λ‹ˆλΌ μ½”λ“œλ₯Ό 보고 μ΄ν•΄ν•˜λŠ”λ° 큰 어렀움을 μ€€λ‹€. κ·Έλž˜μ„œ μ΄λŸ¬ν•œ λ§€μ§λ„˜λ²„λŠ”Β constΒ ν‚€μ›Œλ“œλ₯Ό 톡해 ν•¨μˆ˜ 상단에 μƒμˆ˜ λ³€μˆ˜μ— λ„£μ–΄μ€€ ν›„, μ‚¬μš©ν•΄μ£ΌλŠ” 것이 μ’‹λ‹€.
μ•„λž˜Β μ½”λ“œλŠ” μˆ«μžμ•Όκ΅¬ 랜덀숫자λ₯Ό λ¬Έμžμ—΄μ— λ‹΄λŠ” ν•¨μˆ˜μΈλ° 반볡문 내뢀에 λ§€μ§λ„˜λ²„ 3을 μ‚¬μš©ν–ˆλ‹€.
bad..!
const chooseRandomNumber = () => { const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] answerNum = '' for (let i = 0; i < 3; i++) { const randomNumber = Math.floor(Math.random() * (9 - i)) const chosenNumber = numbers.splice(randomNumber, 1)[0] const chosen = String(chosenNumber) answerNum += chosen } return answerNum }
BASEBALLNUMBERLENGTH λΌλŠ” λ³€μˆ˜μ— 3을 μ΄ˆκΈ°ν™”ν•΄μ€ŒμœΌλ‘œμ¨, λ§€μ§λ„˜λ²„λ₯Ό μ—†μ• μ£Όμ—ˆλ‹€.
good!πŸ‘
const chooseRandomNumber = () => { const BASEBALL_NUMBER_LENGTH = 3 const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] answerNum = '' for (let i = 0; i < BASEBALL_NUMBER_LENGTH; i++) { const randomNumber = Math.floor(Math.random() * (9 - i)) const chosenNumber = numbers.splice(randomNumber, 1)[0] const chosen = String(chosenNumber) answerNum += chosen } return answerNum }

2. λ³€μˆ˜ 넀이밍 κ·œμΉ™

λ¨Όμ € μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” λ³€μˆ˜λͺ…Β μ²«κΈ€μžμ— κΈ€μž ν˜Ήμ€ _, $만 올 수 μžˆλ‹€. κ·Έ μ΄μ™Έμ˜ μˆ«μžλ‚˜ λ‹€λ₯Έ νŠΉμˆ˜λ¬ΈμžλŠ” μ—λŸ¬λ₯Ό 좜λ ₯ν•œλ‹€.
const 1num; // x const num1 // o const _name; // o const $name; // o

μ§€μ—­λ³€μˆ˜λ‚˜ private λ³€μˆ˜μ˜ κ²½μš°μ—λŠ”Β _λ₯Ό μ‚¬μš©ν•œλ‹€.
let _privateVariableName let _privateFunctionName

그리고 μ˜ˆμ•½μ–΄λŠ” λ³€μˆ˜λͺ…μœΌλ‘œ λΆˆκ°€λŠ₯ν•˜λ‹€. λ§ˆμ°¬κ°€μ§€λ‘œ μ—λŸ¬λ₯Ό 좜λ ₯ν•œλ‹€.Β MDN μ˜ˆμ•½μ–΄ λͺ¨μŒ
const true; // x const case; // x const break; // x // ...

그리고 λ³€μˆ˜λͺ…은 μΌλ°˜μ μœΌλ‘œΒ μΉ΄λ©œμΌ€μ΄μŠ€μ™€Β μŠ€λ„€μ΄ν¬μΌ€μ΄μŠ€λ₯Ό ν˜Όμš©ν•΄μ„œ μž‘μ„±ν•œλ‹€.
μΉ΄λ©œμΌ€μ΄μŠ€λŠ” μ—¬λŸ¬ 단어가 λ“€μ–΄κ°€λŠ” λ³€μˆ˜λͺ…일 경우, μ²«λ‹¨μ–΄μ˜ μ²«κΈ€μžλŠ” μ†Œλ¬Έμžλ‘œ, λ‘λ²ˆμ§ΈλΆ€ν„°μ˜ μ²«κΈ€μžλŠ” λŒ€λ¬Έμžλ‘œ μž‘μ„±ν•˜λŠ” ν‘œκΈ°λ²•μ΄λ‹€.
let myname = 'taeeun' // bad let myName = 'taeeun' // good
μŠ€λ„€μ΄ν¬μΌ€μ΄μŠ€λŠ” μ—¬λŸ¬ 단어가 λ“€μ–΄κ°€λŠ” λ³€μˆ˜λͺ…일 경우, _λ₯Ό 톡해 λ„μ–΄μ„œ ν‘œκΈ°ν•˜λŠ” 방법이닀.
const MAXLENGTH = 10 // bad const MAX_LENGTH = 10 // good
보톡 μƒμˆ˜μ— μŠ€λ„€μ΄ν¬μΌ€μ΄μŠ€λ₯Ό, 일반적인 λ³€μˆ˜μ—λŠ” μΉ΄λ©œμΌ€μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” 것 κ°™λ‹€.

3. λ³€μˆ˜μ— νƒ€μž…μ΄λ‚˜ μžλ£Œκ΅¬μ‘°ν˜• λͺ…μ‹œν•˜μ§€ μ•ŠκΈ°

λ³€μˆ˜λ₯Ό 넀이밍할 λ•ŒλŠ” ꡳ이 νƒ€μž…μ΄λ‚˜Β μžλ£Œκ΅¬μ‘°ν˜•μ„ λͺ…μ‹œν•˜μ§€ μ•ŠλŠ” 것이 μ’‹λ‹€.
κ·Έ μ΄μœ λŠ” ν•΄λ‹Ή λ³€μˆ˜μ˜ νƒ€μž…μ΄λ‚˜ μžλ£Œκ΅¬μ‘°ν˜•μ„ λ°”κΏ€ 경우, λͺ¨λ“  λ³€μˆ˜μ΄λ¦„μ„ λ°”κΏ”μ€˜μ•Ό ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€. λ³€μˆ˜ 넀이밍은 κ·Έ λ³€μˆ˜μ˜ ν˜•μ‹λ³΄λ‹€λŠ” ν• λ‹Ήλœ κ°’μ˜ 의미λ₯Ό λ“œλŸ¬λ‚΄λŠ” 것이 μ’‹λ‹€.
ν•¨μˆ˜μ˜ 경우
// bad const compareTwoArray = (numbers1, numbers2) => {} // good! const compareNumbers = (numbers1, numbers2) => {}
λ°°μ—΄μ˜ 경우
const numberArr = [1, 2, 3, 4] const numbers = [1, 2, 3, 4]

4. μ€‘λ³΅λœ κΈ°λŠ₯ ν”Όν•˜κΈ°

μ€‘λ³΅λœ κΈ°λŠ₯은 μ΅œλŒ€ν•œ ν”Όν•˜κ³  κ°€λŠ₯ν•œ 주어진 fieldλ₯Ό μ‚¬μš©ν•œλ‹€.
ν•΄λ‹ΉΒ μ½”λ“œλŠ” μžλ™μ°¨λ₯Ό μƒμ„±ν•˜κ³  μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 숫자만큼 랜덀으둜 각각의 μžλ™μ°¨λ₯Ό μ΄λ™μ‹œμΌœ κ°€μž₯ 멀리간 μžλ™μ°¨λ₯Ό 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ΄λ©°,
처음 μ½”λ“œλ₯Ό μž‘μ„±ν•  λ•Œ, λ‚˜λŠ” 기쑴에 μžλ™μ°¨ 객체 car λ‚΄λΆ€ position 값이 μžˆμœΌλ©΄μ„œλ„ μžλ™μ°¨μ˜ μ΄λ™ν•œ 거리λ₯Ό 전역에 carDistances 배열에 λ”°λ‘œ μ €μž₯ν–ˆμ—ˆλ‹€.
bad..!
class Car { name position = 0 constructor(name) { this.name = name } go() { this.position = this.position + 1 } } const carDistances = [] // 각각의 μžλ™μ°¨ 이동거리 moveCar(car, carNames, carDistances) // μžλ™μ°¨λ₯Ό μ΄λ™μ‹œν‚€κ³  carDistances κ°’ λ³€κ²½ // getWinner ν•¨μˆ˜μ—μ„œλ„ λΆˆν•„μš”ν•˜κ²Œ carDistances 배열을 λ°›μ•„μ•Όν•œλ‹€. const getWinner = (carNames, carDistances) => { const max = Math.max(...carDistances) let winner = '' for (let i = 0; i < carNames.length; i++) { if (max === carDistances[i]) winner += `${carNames[i]},`} return winner }
ν•˜μ§€λ§Œ λ°°μ—΄ λŒ€μ‹  car.position 을 μ‚¬μš©ν•˜λ©΄ μ½”λ“œλ„ 깔끔해지고 λΆˆν•„μš”ν•œ λ©”λͺ¨λ¦¬ λ‚­λΉ„λ₯Ό 막을 수 μžˆλ‹€.
good πŸ‘
moveCar(car) // moceCar ν•¨μˆ˜λŠ” car객체의 position만 μ˜¬λ €μ£ΌλŠ” μ—­ν• λ§Œ μˆ˜ν–‰! // getWinner ν•¨μˆ˜λŠ” car객체듀이 λ“€μ–΄μžˆλŠ” cars λ°°μ—΄λ§Œ λ°›κ³  position λΉ„κ΅λ§Œ ν•΄μ£ΌλŠ” μ—­ν• λ§Œ μˆ˜ν–‰! const getWinner = cars => { let max = 0 cars.map(car => { if (car.position > max) max = car.position }) let winner = [] cars.map(car => { if (max === car.position) winner.push(car.name) }) return winner.join() }
μ“Έλ°μ—†λŠ” 쀑볡은 κΌ­ ν”Όν•˜κ³  각각의 ν•¨μˆ˜λŠ” μ •λ§Β μžκΈ°μ˜ κΈ°λŠ₯만 μˆ˜ν–‰ν•  수 μžˆλ„λ‘Β μ§œλŠ” 것이 μ’‹λ‹€.

5. 각각의 ν•¨μˆ˜λŠ” 자기 κΈ°λŠ₯만 μˆ˜ν–‰ν•˜κΈ°

μŠ€ν„°λ”” λ―Έμ…˜ ν”„λ‘œκ·Έλž˜λ° μš”κ΅¬μ‚¬ν•­μ—μ„œλŠ” 항상 μ΄λŸ¬ν•œ 쑰건이 λ“€μ–΄μžˆμ—ˆλ‹€.
쑰건
μ΅œλŒ€ν•œ indent(인덴트, λ“€μ—¬μ“°κΈ°) depthλ₯Ό 2이상 쓰지 μ•Šμ•„μ•Όν•¨
ν•¨μˆ˜ (λ˜λŠ” λ©”μ†Œλ“œ)의 길이가 10라인을 λ„˜μ–΄κ°€μ§€ μ•Šλ„λ‘ κ΅¬ν˜„
μ΄λŠ” ν•¨μˆ˜λ₯Ό μ΅œλŒ€ν•œ 자기 κΈ°λŠ₯만 μˆ˜ν–‰ν•  수 μžˆλ„λ‘ μͺΌκ°œλŠ” 것을 μš”κ΅¬ν•œλ‹€. ν•¨μˆ˜λ₯Ό μͺΌκ°œλ―€λ‘œμ„œ, 가독성과 μœ μ§€λ³΄μˆ˜κ°€ 쒋아지며, μ•ˆμ •μ„±μ€ μ˜¬λΌκ°„λ‹€.
OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. -Michael Feathers
객체지ν–₯은 λ™μž‘ν•˜λŠ” 뢀뢄을 μΊ‘μŠν™”ν•΄μ„œ 이해할 수 있게 ν•˜κ³ , ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ€ λ™μž‘ν•˜λŠ” 뢀뢄을 μ΅œμ†Œν™”ν•΄μ„œ μ½”λ“œ 이해λ₯Ό λ•λŠ”λ‹€. - 마이클 νŽ˜λ”μŠ€
μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” λ‹€λ₯Έ 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ° 언어듀에 λΉ„ν•΄ 더 μœ μ—°ν•œ νŠΉμ§•μ„ κ°€μ§€μ§€λ§Œ 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ„ μ§€μ›ν•˜λ©°, λ˜ν•œ ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°λ„ μ§€μ›ν•œλ‹€. μ—¬κΈ°μ„œΒ μ§€μ›ν•œλ‹€λŠ” μ˜λ―ΈλŠ” μ™„λ²½ν•œ μ–Έμ–΄μ˜ νŠΉμ„±μ€ μ•„λ‹ˆμ§€λ§Œ λΉ„μŠ·ν•˜κ²Œ κ΅¬ν˜„μ€ κ°€λŠ₯ν•˜λ‹€λŠ” 말이닀. μ™„λ²½ν•˜κ²Œ 객체지ν–₯적으둜 μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ³  λͺ¨λ“  ν•¨μˆ˜λ₯Ό ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μœΌλ‘œ μž‘μ„±ν•˜λŠ” 것은 μ–΄λ ΅κ² μ§€λ§Œ 지ν–₯점을 잘 νŒŒμ•…ν•˜κ³  μ΅œλŒ€ν•œΒ μ’‹μ€ μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ³ μž ν•˜λŠ” 것이 μ€‘μš”ν•˜μ§€ μ•Šμ„κΉŒ? μƒκ°ν•΄λ³΄μ•˜λ‹€.

6. 반볡문 λŠμ–΄μ£ΌκΈ° (break, return)

λ‹€μŒ ν•¨μˆ˜λŠ” μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μžλ™μ°¨μ΄λ¦„μ΄ 5κΈ€μž μ΄ˆκ³ΌμΈμ§€ κ²€μ‚¬ν•˜λŠ”Β ν•¨μˆ˜μ΄λ‹€.
ν•΄λ‹Ή ν•¨μˆ˜λŠ” μž…λ ₯ν•œ κ°’μ˜ 길이만큼 λ°˜λ³΅λ¬Έμ„ λŒλ©΄μ„œ 쑰건을 λ§Œμ‘±ν•˜λ©΄ errorCheck λ³€μˆ˜λ₯Ό 바꾸도둝 μ§œμ„œ 쀑간에 쑰건이 λ§Œμ‘±ν•˜μ—¬λ„ λΆˆν•„μš”ν•˜κ²Œ λͺ¨λ“  λ°˜λ³΅λ¬Έμ„ μˆ˜ν–‰ν–ˆλ‹€.
bad…!
const checkCarsNameLength = carNames => { const MAX_CARNAME_LENGTH = 5 let errorCheck = true for (let i = 0; i < carNames.length; i++) { if (carNames[i].length > MAX_CARNAME_LENGTH) errorCheck = false } // 이름이 5초과인 것이 있으면 falseλ₯Ό, μ—†μœΌλ©΄ trueλ₯Ό 리턴. return errorCheck }
ν•˜μ§€λ§Œ 쑰건을 λ§Œμ‘±ν–ˆμ„ λ•Œ λ°”λ‘œ 값을 return ν•˜λ„λ‘ λ°”κΏ¨λ”λ‹ˆ λ³€μˆ˜λ₯Ό μ“°μ§€μ•Šμ•„λ„ 되고, 쑰건 λ§Œμ‘±μ‹œ, λ°˜λ³΅λ¬Έλ„ μ „λΆ€ νƒμƒ‰ν•˜μ§€ μ•Šμ•„λ„ λ˜μ—ˆλ‹€.
good!!πŸ‘
const checkCarsNameLength = carNames => { const MAX_CARNAME_LENGTH = 5 for (let i = 0; i < carNames.length; i++) { if (carNames[i].length > MAX_CARNAME_LENGTH) return false } return true }
μ΄λ ‡κ²Œ λΆˆν•„μš”ν•œ λ³€μˆ˜λ₯Ό 쀄이고,Β returnΒ κ³ΌΒ break 둜 κ°€λŠ₯ν•˜λ©΄ λ°˜λ³΅λ¬Έμ„ λΉ λ₯΄κ²Œ λΉ μ Έλ‚˜μ˜¬ 수 μžˆλ„λ‘ μ§œλŠ” 것이 μ’‹λ‹€.

7. νƒ€μž…μŠ€ν¬λ¦½νŠΈ μ‚¬μš©ν•˜κΈ°

java μŠ€ν„°λ””μ—¬μ„œ 주어진 λ―Έμ…˜μ€ 객체지ν–₯적인 class Type을 주둜 μ‚¬μš©ν–ˆλ‹€. μžλ°”μŠ€ν¬λ¦½νŠΈμ—λ„ class 문법이 μžˆμ–΄μ„œ 코딩은 κ°€λŠ₯ν–ˆμ§€λ§Œ, νƒ€μž…μ΄ μ§€μ •λ˜μ–΄μžˆμ§€ μ•Šμ•„μ„œ μ½”λ“œλ₯Ό μ§œλ©΄μ„œ ν˜„μž¬ λ³€μˆ˜κ°€ μ–΄λ–€ νƒ€μž…μΈμ§€κ°€ 맀우 ν—·κ°ˆλ Έλ‹€.
Car객체 배열을 인자둜 λ°›λŠ”Β ν•¨μˆ˜μ˜ 경우
// javascript : 배열인데, λ‚΄λΆ€ μš”μ†Œλ“€μ˜ νƒ€μž…μ΄ 뢈λͺ…확함 makeCars(cars, carNames) // typescript : λ°°μ—΄ λ‚΄λΆ€ νƒ€μž… λͺ…μ‹œλ‘œ μ—λŸ¬λ°©μ§€ + ν—·κ°ˆλ¦Όλ°©μ§€! makeCars(cars: Car[], carNames: string[])
λ‹€μŒΒ μ½”λ“œλŠ” 둜또λ₯Ό κ΅¬λ§€ν•˜κ³  λ‹Ήμ²¨λ‘œλ˜μ™€ λΉ„κ΅ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ΄λ©°, 둜또 ν•œμž₯을 μ˜λ―Έν•˜λŠ”Β LottoΒ νƒ€μž…κ³Ό μ‹€μ œ λ‹Ήμ²¨λ‘œλ˜λ₯Ό μ˜λ―Έν•˜λŠ”Β WinningLottoΒ νƒ€μž…μ΄ λ”°λ‘œ μ •μ˜λ˜μ–΄μžˆμ–΄ λ”μš± ν—·κ°ˆλ Έλ‹€.
/** * 둜또 ν•œμž₯을 μ˜λ―Έν•˜λŠ” 객체 */ class Lotto { numbers = [] constructor(numbers) { this.numbers = numbers } } /** * 당첨 번호λ₯Ό λ‹΄λ‹Ήν•˜λŠ” 객체 */ class WinningLotto { lotto = null bonusNo = 0 constructor(lotto, bonusNo) { this.lotto = lotto this.bonusNo = bonusNo } match(userLotto) { let count = 0 let bonusCount = 0 this.lotto.numbers.map(number => { userLotto.numbers.indexOf(number) >= 0 && count++ }) userLotto.numbers.indexOf(this.bonusNo) >= 0 && bonusCount++ if (count === 6) return 'FIRST' if (count === 5 && bonusCount) return 'SECOND' if (count + bonusCount === 5) return 'THIRD' if (count + bonusCount === 4) return 'FOURTH' if (count + bonusCount === 3) return 'FIFTH' return 'MISS' } } // javascript : ν•¨μˆ˜μ˜ 인자둜 λ“€μ–΄κ°€λŠ” λ³€μˆ˜λͺ…듀이 맀우 ν—·κ°ˆλ¦Ό!! matchLottos(myLottos, winningLotto) // typescript : λ‚΄λΆ€ νƒ€μž… λͺ…μ‹œλ‘œ μ—λŸ¬λ°©μ§€ + ν—·κ°ˆλ¦Όλ°©μ§€! matchLottos(myLottos: Lotto[], winningLotto: WinningLotto): void
아직 νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” μ΅μˆ™μΉ˜ μ•Šμ§€λ§Œ, 이 κΈ°νšŒμ— 같이 ν•™μŠ΅ν•˜λ©΄ 쒋을 것 κ°™λ‹€.

8. ν…ŒμŠ€νŠΈμ½”λ“œ μž‘μ„±ν•˜κΈ°

2020λ…„ 4μ›” 18일, μž μ‹€ μ˜€ν”„λΌμΈ μŠ€ν„°λ””μ—μ„œΒ ν…ŒμŠ€νŠΈΒ μ— λŒ€ν•΄μ„œ λ°°μ› λ‹€.
μŠ€ν„°λ””μ—μ„œλŠ” java ν…ŒμŠ€νŒ… ν”„λ ˆμž„μ›Œν¬ JUnit λ₯Ό μ‚¬μš©ν–ˆλŠ”λ°, λ‚˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ΄κΈ° λ•Œλ¬Έμ— jestλΌλŠ” ν”„λ ˆμž„μ›Œν¬λ₯Ό μ„ νƒν•΄μ„œ ν•œλ²ˆ ν…ŒμŠ€νŠΈ ν•΄λ³΄μ•˜λ‹€.
μ‚¬μš©λ°©μ‹μ€ λΉ„μŠ·ν–ˆλ‹€.
λ¨Όμ € jest ν”„λ ˆμž„μ›Œν¬λ₯Ό μ„€μΉ˜ν•˜κ³ ,
npm i -D jest
package.json 파일의 script에 jest λͺ…λ Ήμ–΄λ₯Ό μΆ”κ°€μ‹œμΌœμ£Όλ©΄ λœλ‹€. μ‹€ν–‰μ‹œ :Β npm run test
"scripts": { "test": "jest" }
그리고 test 디렉토리λ₯Ό λ§Œλ“€μ–΄ ν•΄λ‹Ή ν…ŒμŠ€νŒ… 파일.js 을 생성해주면 λ˜μ—ˆλ‹€.
// calculatorTest.js class Calculator { add(i, j) { return i + j } subtract(i, j) { return i - j } multiple(i, j) { return i * j } divide(i, j) { return i / j } } test('λ§μ…ˆ', () => { let calculator = new Calculator() expect(calculator.add(1, 2)).toBe(3) }) test('λΊ„μ…ˆ', () => { let calculator = new Calculator() expect(calculator.subtract(1, 2)).toBe(-1) }) test('κ³±μ…ˆ', () => { let calculator = new Calculator() expect(calculator.multiple(1, 2)).toBe(2) }) test('λ‚˜λˆ—μ…ˆ', () => { let calculator = new Calculator() expect(calculator.divide(2, 1)).toBe(2) })
jest API의 testλΌλŠ” λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν–ˆλŠ”λ°, jest API λ©”μ†Œλ“œλ„ λ‹€μ–‘ν•˜κ²Œ μžˆλŠ” 것 κ°™λ‹€. μ‹œκ°„λ‚  λ•ŒΒ jest κ³΅μ‹λ¬Έμ„œλ₯Ό ν•œλ²ˆ μ­‰ λ³Ό 것.
test("ν…ŒμŠ€νŠΈ 이름", function, timeout)

9. ν…ŒμŠ€νŠΈμ½”λ“œ μž‘μ„±κ·œμΉ™ & 팁

1️⃣ ν…ŒμŠ€νŠΈ μ΄λ¦„μ€Β ν•œκΈ€λ‘œ μž‘μ„±ν•˜λŠ” 것이 μ’‹λ‹€. μœ„μ˜ ν…ŒμŠ€νŠΈλŠ” 맀우 κ°„λ‹¨ν•˜μ§€λ§Œ ν”„λ‘œκ·Έλž¨μ΄ λ³΅μž‘ν•΄μ§€λ©΄ ν…ŒμŠ€νŠΈλͺ…도 κΈΈμ–΄μ§ˆ 것이고 각각 ν…ŒμŠ€νŠΈλ§ˆλ‹€ μ΄ν•΄ν•˜κΈ° 쉽고 의미λ₯Ό 잘 전달해주어야 ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
2️⃣ ν…ŒμŠ€νŠΈλŠ” κ°€μž₯ μž‘μ€ λ‹¨μœ„μ˜ κΈ°λŠ₯λΆ€ν„° ν…ŒμŠ€νŠΈν•œλ‹€. 예λ₯Όλ“€μ–΄, A,Bκ°€ CλΌλŠ” κΈ°λŠ₯에 의쑴될 경우, A,Bλ₯Ό ν…ŒμŠ€νŠΈν•˜κ³  검증이 μ™„λ£Œλ˜λ©΄ Cμ—μ„œ A,Bκ°€ ν¬ν•¨λ˜μ§€ μ•Šμ€ κΈ°λŠ₯만 ν…ŒμŠ€νŠΈν•˜λ©΄ λœλ‹€.
3️⃣ ν•˜λ‚˜μ˜ ν…ŒμŠ€νŠΈλŠ” 독립적이어야 ν•œλ‹€. ν…ŒμŠ€νŠΈλΌλ¦¬ μ˜μ‘΄ν•΄μ„œλŠ” μ•ˆλœλ‹€.

10. μ°Έκ³ 

11. 마치며.

쒋은 κΈ°νšŒκ°€ μžˆμ–΄μ„œ OOP μŠ€ν„°λ””λ₯Ό ν•˜κ²Œ λ˜μ—ˆλ‹€. 비둝 java μŠ€ν„°λ””μ§€λ§Œ μŠ€ν„°λ””λ₯Ό ν•˜λ©΄μ„œ λ§Žμ€ 것듀을 λ°°μš°λŠ” 것 κ°™λ‹€. ν‰μ†Œμ— κΈ°λŠ₯κ΅¬ν˜„ μœ„μ£Όμ˜ 곡뢀λ₯Ό ν–ˆλ‹€λ©΄ 이번 μŠ€ν„°λ””λ₯Ό ν†΅ν•΄μ„œλŠ”Β μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” 방법에 λŒ€ν•΄μ„œ 많이 생각해볼 수 μžˆμ–΄μ„œ μ’‹μ•˜λ‹€. ν˜„μž¬λŠ” ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±μ— κ΄€ν•œ λ―Έμ…˜μ„ 진행 쀑에 μžˆλŠ”λ° μ•žμœΌλ‘œ 더 μ—΄μ‹¬νžˆ μž„ν•΄μ•Όκ² λ‹€!!

Loading Comments...