본문으로 건너뛰기
Skillpedia
일반 코딩40k · #7258

java-refactoring-extract-method

하는 일

이 도구는 Java 코드를 정리하도록 돕습니다. 반복적이거나 너무 긴 코드 조각을 더 작고 이해하기 쉬운 함수로 바꿉니다.

사용하는 경우

  • ✓메서드에 코드 줄이 너무 많을 때.
  • ✓메서드가 한 가지 이상의 일을 할 때.
  • ✓코드를 읽고 재사용하기 쉽게 만들고 싶을 때.

스킬 링크

https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“이 긴 메서드를 더 작고 명확한 부분으로 나눠 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

AI는 분리할 수 있는 코드 부분을 찾습니다. 이 부분들을 설명적인 이름을 가진 새 함수로 만들고, 원래 코드를 그 새 함수 호출로 바꿉니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Refactoring Java Methods with Extract Method

Role

You are an expert in refactoring Java methods.

Below are 2 examples (with titles code before and code after refactoring) that represents Extract Method.

Code Before Refactoring 1:

public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
	assertNotBuild();
	if (bpartnerId > 0) {
		setC_BPartner_ID(bpartnerId);
	}
	return this;
}

Code After Refactoring 1:

public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) {
	if (bpartnerId != null) {
		return bpartnerId(bpartnerId);
	} else {
		return this;
	}
}
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) {
	return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId));
}

Code Before Refactoring 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return new DefaultExpander(newTypes, newDirections);
}

Code After Refactoring 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return (DefaultExpander) newExpander(newTypes, newDirections);
}
protected RelationshipExpander newExpander(RelationshipType[] types,
          Map<String, Direction> directions) {
     return new DefaultExpander(types, directions);
}

Task

Apply Extract Method to improve readability, testability, maintainability, reusability, modularity, cohesion, low coupling, and consistency.

Always return a complete and compilable method (Java 17).

Perform intermediate steps internally:

  • First, analyze each method and identify those exceeding thresholds:
    • LOC (Lines of Code) > 15
    • NOM (Number of Statements) > 10
    • CC (Cyclomatic Complexity) > 10
  • For each qualifying method, identify code blocks that can be extracted into separate methods.
  • Extract at least one new method with a descriptive name.
  • Output only the refactored code inside a single java block.
  • Do not remove any functionality from the original method.
  • Include a one-line comment above each new method describing its purpose.

Code to be Refactored:

Now, assess all methods with high complexity and refactor them using Extract Method

자주 묻는 질문

왜 코드를 정리해야 하나요?

정리된 코드는 읽기 쉽고, 오류를 찾기 쉬우며, 나중에 새 기능을 추가하기도 쉽습니다.

'리팩터링'이란 무엇인가요?

외부에서 보이는 동작은 바꾸지 않고 코드의 내부 구조를 개선하는 것입니다.

'메서드 추출(Extract Method)'이란 무엇인가요?

코드 블록을 새 메서드로 떼어 내는 특정 리팩터링 기법입니다.

상세 정보

작성자의 원본 설명

Refactoring using Extract Methods in Java Language

repo: github/awesome-copilot

인기도

GitHub 스타
40k
순위
#7258
라이선스
MIT
업데이트
2026. 10. 15.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬