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

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가 스킬을 읽고 지침을 따릅니다.

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

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

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

  1. 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
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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:

  1. Run all existing tests related to the refactored method and its surrounding functionality
  2. 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
  3. 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)
  4. Verify compilation - Ensure there are no compilation errors
  5. 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.

이 스킬이 유용한가요?

0

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

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬