refactor-method-complexity-reduce
하는 일
이 도구는 복잡한 코드를 더 작고 이해하기 쉬운 부분으로 나눠 단순하게 만드는 데 도움을 줍니다. 목표는 코드를 더 읽기 쉽고 유지보수하기 쉽게 만드는 것입니다.
사용하는 경우
- ✓코드 조각이 너무 길고 읽기 어려울 때.
- ✓다른 사람이 쓴 코드를 이해하거나 수정해야 할 때.
- ✓프로젝트에 추가하기 전에 전체 코드 품질을 개선하고 싶을 때.
스킬 링크
https://github.com/github/awesome-copilot/blob/main/skills/refactor-method-complexity-reduce/SKILL.md복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.
사용 방법
가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.
바로 붙여넣을 수 있는 메시지
Claude Code, Codex, Cursor용입니다. 프로젝트 폴더에서 실행하면 스킬이 설치됩니다.
SKILL.md를 다운로드해 에이전트의 스킬 폴더에 넣으세요.
AI에게 요청하는 예시
“'processUserData'라는 이 메서드를 복잡도가 10 이하가 되도록 단순하게 만들어 주세요.”
매주 새 스킬 5개. 읽는 데 1분.
작동 방식
도구는 코드를 분석해 조건이 많거나 반복되는 등 복잡한 부분을 찾습니다. 그다음 각 복잡한 부분을 위한 더 작은 새 함수를 만들고, 원래 코드가 이 새 함수를 호출하도록 고칩니다. 이때 모든 것이 이전과 같이 작동하도록 유지합니다.
기술 내용 (원본 SKILL.md)
작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.
Refactor Method to Reduce Cognitive Complexity
Objective
Refactor the method ${input:methodName}, to reduce its cognitive complexity to ${input:complexityThreshold} or below, by extracting logic into focused helper methods.
Instructions
Analyze the current method to identify sources of cognitive complexity:
- Nested conditional statements
- Multiple if-else or switch chains
- Repeated code blocks
- Multiple loops with conditions
- Complex boolean expressions
Identify extraction opportunities:
- Validation logic that can be extracted into a separate method
- Type-specific or case-specific processing that repeats
- Complex transformations or calculations
- Common patterns that appear multiple times
Extract focused helper methods:
- Each helper should have a single, clear responsibility
- Extract validation into separate
Validate*methods - Extract type-specific logic into handler methods
- Create utility methods for common operations
- Use appropriate access levels (static, private, async)
Simplify the main method:
- Reduce nesting depth
- Replace massive if-else chains with smaller orchestrated calls
- Use switch statements where appropriate for cleaner dispatch
- Ensure the main method reads as a high-level flow
Preserve functionality:
- Maintain the same input/output behavior
- Keep all validation and error handling
- Preserve exception types and error messages
- Ensure all parameters are properly passed to helpers
Best practices:
- Make helper methods static when they don't need instance state
- Use null checks and guard clauses early
- Avoid creating unnecessary local variables
- Consider using tuples for multiple return values
- Group related helper methods together
Implementation Approach
- Extract helper methods before refactoring the main flow
- Test incrementally to ensure no regressions
- Use meaningful names that describe the extracted responsibility
- Keep extracted methods close to where they're used
- Consider making repeated code patterns into generic methods
Result
The refactored method should:
- Have cognitive complexity reduced to the target threshold of
${input:complexityThreshold}or below - Be more readable and maintainable
- Have clear separation of concerns
- Be easier to test and debug
- Retain all original functionality
Testing and Validation
CRITICAL: After completing the refactoring, you MUST:
- Run all existing tests related to the refactored method and its surrounding functionality
- MANDATORY: Explicitly verify test results show "failed=0"
- NEVER assume tests passed - always examine the actual test output
- Search for the summary line containing pass/fail counts (e.g., "passed=X failed=Y")
- If the summary shows any number other than "failed=0", tests have FAILED
- If test output is in a file, read the entire file to locate and verify the failure count
- Running tests is NOT the same as verifying tests passed
- Do not proceed until you have explicitly confirmed zero failures
- If any tests fail (failed > 0):
- State clearly how many tests failed
- Analyze each failure to understand what functionality was broken
- Common causes: null handling, empty collection checks, condition logic errors
- Identify the root cause in the refactored code
- Correct the refactored code to restore the original behavior
- Re-run tests and verify "failed=0" in the output
- Repeat until all tests pass (failed=0)
- Verify compilation - Ensure there are no compilation errors
- Check cognitive complexity - Confirm the metric is at or below the target threshold of
${input:complexityThreshold}
Confirmation Checklist
- Code compiles without errors
- Test results explicitly state "failed=0" (verified by reading the output)
- All test failures analyzed and corrected (if any occurred)
- Cognitive complexity is at or below the target threshold of
${input:complexityThreshold} - All original functionality is preserved
- Code follows project conventions and standards
자주 묻는 질문
'인지 복잡도(cognitive complexity)'는 무엇인가요?
사람이 코드를 이해하기가 얼마나 어려운지를 나타내는 지표입니다. 숫자가 낮을수록 읽고 이해하기 쉽습니다.
코드가 작동을 멈추지는 않나요?
아니요. 도구는 코드가 정확히 같은 일을 계속하도록 보장합니다. 또한 수정 후에도 기존 테스트가 모두 통과하는지 확인합니다.
'도우미 메서드(helper methods)'는 무엇인가요?
도구가 메인 함수를 단순하게 만들기 위해 특정 작업을 맡도록 만든 작은 함수입니다.
상세 정보
작성자의 원본 설명
Refactor given method `${input:methodName}` to reduce its cognitive complexity to `${input:complexityThreshold}` or below, by extracting helper methods.
repo: github/awesome-copilot
인기도
- GitHub 스타
- 40k
- 순위
- #7394
- 라이선스
- MIT
- 업데이트
- 2026. 10. 15.
댓글
- …
이 저장소의 다른 스킬
acquire-codebase-knowledge
40k에이전트가 기존 코드 프로젝트를 이해하고 문서화하도록 돕습니다. 새 구성원을 맞이하거나 아키텍처를 개괄적으로 파악할 때 유용합니다.
acreadiness-assess
40k에이전트가 프로젝트의 AI 준비 상태를 평가하고 보고서를 만들도록 합니다. 프로젝트가 AI 도구를 쓸 준비가 얼마나 되어 있는지 이해하는 데 유용합니다.
acreadiness-generate-instructions
40k에이전트가 평가 결과를 바탕으로 AI 도구를 위한 지침을 만들도록 돕습니다. AI가 코드와 상호작용하는 방식을 개선하고 확인된 공백을 메울 때 유용합니다.
acreadiness-policy
40k에이전트가 AI 프로젝트 평가를 위한 정책을 정의하거나 적용하도록 돕습니다. 프로젝트 평가 방식을 표준화하고 규정 준수를 보장할 때 유용합니다.