# 중복 코드

## 단점

* 비슷한지 동일한 코드인지 주의깊게 봐야함
* 코드를 변경할 때, 동일한 모든 곳의 코드를 변경해야함

## 함수 추출하기

* **의도** 와 **구현** 분리하기
* 무슨일을 하는지 파악이 힘든 함수라면 해당 코드를 함수로 분리하고 함수 이름으로 **무슨 일을 하는지** 표현
* 한줄 짜리 함수도 괜찮다.
* 큰 함수 안에 들어있는 주석은 추출할 함수부분을 찾는데 좋은 단서

## 코드 정리하기

* 관련있는 코드끼리 묶기
* 함수에서 사용할 변수를 상단에 미리 정의하지말고 사용하는 코드위에 선언

## 메소드 올리기

* 중복 코드는 미래에 버그를 만들어 낼 빌미를 제공한다.
* 비슷하지만 일부 값만 다른 경우라면 `함수 매개변수화` 적용
* 하위 클래스에 있는 코드가 상위클래스가 아닌 하위 클래스 기능에 의존하고 있다면 `필드 올리기` 적용
* 두 메소드가 비슷한 절차를 따르고 있다면, `템플릿 메소드 패턴` 적용


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nelmm.gitbook.io/til/common/refactoring/duplicate-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
