Skip to content

Files

Latest commit

64f8d38 Β· Feb 6, 2024

History

History
231 lines (142 loc) Β· 5.4 KB

File metadata and controls

231 lines (142 loc) Β· 5.4 KB

ν΄λ¦°μ½”λ“œμ™€ λ¦¬νŒ©ν† λ§


ν΄λ¦°μ½”λ“œμ™€ λ¦¬νŒ©ν† λ§μ€ 의미만 보면 λΉ„μŠ·ν•˜λ‹€κ³  λŠκ»΄μ§„λ‹€. μ–΄λ–€ 차이점이 μžˆμ„μ§€ μƒκ°ν•΄λ³΄μž


ν΄λ¦°μ½”λ“œ

ν΄λ¦°μ½”λ“œλž€, 가독성이 높은 μ½”λ“œλ₯Ό λ§ν•œλ‹€.

가독성을 높이렀면 λ‹€μŒκ³Ό 같이 κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.

  • 넀이밍이 잘 λ˜μ–΄μ•Ό 함
  • 였λ₯˜κ°€ μ—†μ–΄μ•Ό 함
  • 쀑볡이 μ—†μ–΄μ•Ό 함
  • μ˜μ‘΄μ„±μ„ μ΅œλŒ€ν•œ 쀄여야 함
  • 클래슀 ν˜Ήμ€ λ©”μ†Œλ“œκ°€ ν•œκ°€μ§€ 일만 μ²˜λ¦¬ν•΄μ•Ό 함

μ–Όλ§ˆλ‚˜ μ½”λ“œκ°€ 잘 μ½νžˆλŠ” 지, μ½”λ“œκ°€ μ§€μ €λΆ„ν•˜μ§€ μ•Šκ³  μ •λ¦¬λœ μ½”λ“œμΈμ§€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 것이 λ°”λ‘œ '클린 μ½”λ“œ'

public int AAA(int a, int b){
    return a+b;
}
public int BBB(int a, int b){
    return a-b;
}

두 가지 문제점이 μžˆλ‹€.


public int sum(int a, int b){
    return a+b;
}

public int sub(int a, int b){
    return a-b;
}

μ²«μ§ΈλŠ” ν•¨μˆ˜ 넀이밍이닀. λ‹€λ₯Έ μ‚¬λžŒλ“€μ΄ 봐도 무슨 역할을 ν•˜λŠ” ν•¨μˆ˜μΈ 지 μ•Œ 수 μžˆλŠ” 이름을 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

λ‘˜μ§ΈλŠ” ν•¨μˆ˜μ™€ ν•¨μˆ˜ μ‚¬μ΄μ˜ 간격이닀. μ—¬λŸ¬ ν•¨μˆ˜κ°€ μ‘΄μž¬ν•  λ•Œ 간격을 λ‚˜λˆ„μ§€ μ•ŠμœΌλ©΄ μ‹œμž‘κ³Ό 끝을 κ΅¬λΆ„ν•˜λŠ” 것이 맀우 νž˜λ“€λ‹€.



λ¦¬νŒ©ν† λ§

ν”„λ‘œκ·Έλž¨μ˜ μ™ΈλΆ€ λ™μž‘μ€ κ·ΈλŒ€λ‘œ λ‘” 채, λ‚΄λΆ€μ˜ μ½”λ“œλ₯Ό μ •λ¦¬ν•˜λ©΄μ„œ κ°œμ„ ν•˜λŠ” 것을 말함

이미 곡사가 λλ‚œ μ§‘μ΄μ§€λ§Œ, 더 νŠΌνŠΌν•˜κ³  멋진 집을 λ§Œλ“€κΈ° μœ„ν•΄ λ‚΄λΆ€ ꡬ쑰λ₯Ό κ°œμ„ ν•˜λŠ” 리λͺ¨λΈλ§ μž‘μ—…

ν”„λ‘œμ νŠΈκ°€ λλ‚˜λ©΄, μ§€μ €λΆ„ν•œ μ½”λ“œλ₯Ό λ³Ό λ•Œ 가독성이 λ–¨μ–΄μ§€λŠ” 뢀뢄이 μ‘΄μž¬ν•œλ‹€. 이 뢀뢄을 κ°œμ„ μ‹œν‚€κΈ° μœ„ν•΄ ν•„μš”ν•œ 것이 λ°”λ‘œ 'λ¦¬νŒ©ν† λ§ 기법'

λ¦¬νŒ©ν† λ§ μž‘μ—…μ€ μ½”λ“œμ˜ 가독성을 높이고, ν–₯ν›„ μ΄λ£¨μ–΄μ§ˆ μœ μ§€λ³΄μˆ˜μ— 큰 도움이 λœλ‹€.


λ¦¬νŒ©ν† λ§μ΄ ν•„μš”ν•œ μ½”λ“œλŠ”?
  • 쀑볡 μ½”λ“œ
  • κΈ΄ λ©”μ†Œλ“œ
  • κ±°λŒ€ν•œ 클래슀
  • Switch λ¬Έ
  • μ ˆμ°¨μ§€ν–₯으둜 κ΅¬ν˜„ν•œ μ½”λ“œ

λ¦¬νŒ©ν† λ§μ˜ λͺ©μ μ€, μ†Œν”„νŠΈμ›¨μ–΄λ₯Ό 더 μ΄ν•΄ν•˜κΈ° 쉽고 μˆ˜μ •ν•˜κΈ° μ‰½κ²Œ λ§Œλ“œλŠ” 것

λ¦¬νŒ©ν† λ§μ€ μ„±λŠ₯을 μ΅œμ ν™”μ‹œν‚€λŠ” 것이 μ•„λ‹ˆλ‹€.
μ½”λ“œλ₯Ό μ‹ μ†ν•˜κ²Œ κ°œλ°œν•  수 있게 λ§Œλ“€μ–΄μ£Όκ³ , μ½”λ“œ ν’ˆμ§ˆμ„ μ’‹κ²Œ λ§Œλ“€μ–΄μ€€λ‹€.

μ΄ν•΄ν•˜κΈ° 쉽고, μˆ˜μ •ν•˜κΈ° μ‰¬μš°λ©΄? β†’ 개발 속도가 증가!


λ¦¬νŒ©ν† λ§μ΄ ν•„μš”ν•œ 상황

μ†Œν”„νŠΈμ›¨μ–΄μ— μƒˆλ‘œμš΄ κΈ°λŠ₯을 μΆ”κ°€ν•΄μ•Ό ν•  λ•Œ

λͺ…심해야할 것은, μš°μ„  μ½”λ“œκ°€ μ œλŒ€λ‘œ λŒμ•„κ°€μ•Ό ν•œλ‹€λŠ” 것. λ¦¬νŒ©ν† λ§μ€ μš°μ„ μ μœΌλ‘œ ν•΄μ•Ό ν•  일이 μ•„λ‹˜μ„ λͺ…μ‹¬ν•˜μž

객체지ν–₯ νŠΉμ§•μ„ 살리렀면, switch-case 문을 적게 μ‚¬μš©ν•΄μ•Ό 함

(switch문은 μ˜€λ²„λΌμ΄λ“œλ‘œ λ‹€ λ°”κΏ”λ²„λ¦¬μž)


λ¦¬νŒ©ν† λ§ 예제

1번

// μˆ˜μ • μ „
public int getFoodPrice(int arg1, int arg2) {
    return arg1 * arg2;
}

ν•¨μˆ˜λͺ… 직관적 μˆ˜μ •, λ³€μˆ˜λͺ…을 μ˜λ―Έμ— 맞게 μˆ˜μ •

// μˆ˜μ • ν›„
public int getTotalFoodPrice(int price, int quantity) {
    return price * quantity;
}

2번

// μˆ˜μ • μ „
public int getTotalPrice(int price, int quantity, double discount) {
    return (int) ((price * quantity) * (price * quantity) * (discount /100));
}

price * quantityκ°€ μ€‘λ³΅λœλ‹€. λ”°λ‘œ λ³€μˆ˜λ‘œ μΆ”μΆœν•˜μž

ν• μΈμœ¨μ„ κ³„μ‚°ν•˜λŠ” 뢀뢄을 λ©”μ†Œλ“œλ‘œ λ”°λ‘œ μΆ”μΆœν•˜μž

ν• μΈμœ¨ ν•¨μˆ˜ 같은 κ²½μš°λŠ” 항상 μΌμ •ν•˜λ―€λ‘œ μ™ΈλΆ€μ—μ„œ κ±΄λ“œλ¦¬μ§€ λͺ»ν•˜λ„둝 private μ„ μ–Έ

// μˆ˜μ • ν›„
public int getTotalFoodPrice(int price, int quantity, double discount) {
    int totalPriceQuantity = price * quantity;
    return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity))
}

private double getDiscountPrice(double discount, int totalPriceQuantity) {
    return totalPriceQuantity * (discount / 100);
}

이 μ½”λ“œλ₯Ό ν•œλ²ˆ 더 λ¦¬νŒ©ν† λ§ 해보면?


3번

// μˆ˜μ • μ „
public int getTotalFoodPrice(int price, int quantity, double discount) {
	
    int totalPriceQuantity = price * quantity;
    return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity))
}

private double getDiscountPrice(double discount, int totalPriceQuantity) {
    return totalPriceQuantity * (discount / 100);
}

totalPriceQuantityλ₯Ό getter λ©”μ†Œλ“œλ‘œ μΆ”μΆœμ΄ κ°€λŠ₯ν•˜λ‹€.

μ§€λΆˆν•œλ‹€λŠ” 의미λ₯Ό μ£ΌκΈ° μœ„ν•΄ λ©”μ†Œλ“œ λͺ…을 μˆ˜μ •ν•΄μ£Όμž


// μˆ˜μ • ν›„
public int getFoodPriceToPay(int price, int quantity, double discount) {
    
    int totalPriceQuantity = getTotalPriceQuantity(price, quantity);
    return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity));
}

private double getDiscountPrice(double discount, int totalPriceQuantity) {
    return totalPriceQuantity * (discount / 100);
}

private int getTotalPriceQuantity(int price, int quantity) {
    return price * quantity;
}


ν΄λ¦°μ½”λ“œμ™€ λ¦¬νŒ©ν† λ§μ˜ 차이?

λ¦¬νŒ©ν† λ§μ΄ 더 큰 의미λ₯Ό 가진 것 κ°™λ‹€. 클린 μ½”λ“œλŠ” λ‹¨μˆœνžˆ 가독성을 높이기 μœ„ν•œ μž‘μ—…μœΌλ‘œ 이루어져 μžˆλ‹€λ©΄, λ¦¬νŒ©ν† λ§μ€ 클린 μ½”λ“œλ₯Ό ν¬ν•¨ν•œ μœ μ§€λ³΄μˆ˜λ₯Ό μœ„ν•œ μ½”λ“œ κ°œμ„ μ΄ 이루어진닀.

ν΄λ¦°μ½”λ“œμ™€ 같은 뢀뢄은 섀계뢀터 잘 이루어져 μžˆλŠ” 것이 μ€‘μš”ν•˜κ³ , λ¦¬νŒ©ν† λ§μ€ 결과물이 λ‚˜μ˜¨ 이후 μˆ˜μ •μ΄λ‚˜ μΆ”κ°€ μž‘μ—…μ΄ 진행될 λ•Œ κ°œμ„ ν•΄λ‚˜κ°€λŠ” 것이 μ˜¬λ°”λ₯Έ λ°©ν–₯이닀.