PluginBench
Skill
Official
Review
Audit score 70

refactor-method-complexity-reduce

github/awesome-copilot

Reduce method cognitive complexity by extracting focused helper methods.

What is refactor-method-complexity-reduce?

Refactors a specified method to lower its cognitive complexity below a target threshold by identifying and extracting validation logic, repeated patterns, and nested conditionals into separate helper methods. Use this when a method has become difficult to understand or maintain due to nested conditions, multiple branches, or repeated code blocks.

  • Analyzes methods for sources of cognitive complexity (nested conditionals, loops, repeated blocks)
  • Identifies extraction opportunities for validation, type-specific logic, and common patterns
  • Extracts focused helper methods with single, clear responsibilities
  • Simplifies the main method flow by replacing complex nesting with orchestrated helper calls
  • Preserves all original functionality, validation, and error handling
  • Validates refactoring by running tests and confirming cognitive complexity meets target threshold

How to install refactor-method-complexity-reduce

npx skills add https://github.com/github/awesome-copilot --skill refactor-method-complexity-reduce
Claude Code
Cursor
Windsurf
Cline

How to use refactor-method-complexity-reduce

  1. 1.Specify the method name to refactor using the `methodName` input parameter
  2. 2.Specify the target cognitive complexity threshold using the `complexityThreshold` input parameter
  3. 3.The skill will analyze the method and identify nesting, conditionals, and repeated patterns
  4. 4.Review the extracted helper methods to ensure each has a single, clear responsibility
  5. 5.Run all existing tests related to the refactored method to verify no regressions
  6. 6.Confirm test output explicitly shows 'failed=0' before considering the refactoring complete
  7. 7.Verify the final cognitive complexity metric meets or is below the target threshold

Use cases

Good for
  • Refactor a method with deeply nested if-else chains into smaller, testable helper methods
  • Extract validation logic from a complex processing method into dedicated Validate* methods
  • Break down a method handling multiple types or cases into focused handler methods
  • Reduce cognitive complexity of a method that has accumulated repeated code patterns over time
  • Simplify a method with multiple loops and conditions by extracting transformation logic
Who it's for
  • Backend developers maintaining complex business logic methods
  • Code quality engineers reducing technical debt in legacy codebases
  • Teams adopting cognitive complexity metrics and refactoring to meet standards
  • Developers preparing code for easier testing and debugging

refactor-method-complexity-reduce FAQ

What counts as cognitive complexity?

Nested conditional statements, multiple if-else or switch chains, repeated code blocks, multiple loops with conditions, and complex boolean expressions all increase cognitive complexity.

How do I know if the refactoring is successful?

The refactored method should have cognitive complexity at or below your target threshold, all existing tests must pass (failed=0), and the code should be more readable with clear separation of concerns.

What if tests fail after refactoring?

Analyze each test failure to identify what functionality was broken (common causes: null handling, empty collection checks, condition logic errors), correct the refactored code to restore original behavior, then re-run tests until failed=0.

Should helper methods be static or instance methods?

Make helper methods static when they don't need instance state. Use appropriate access levels (private for internal helpers) and consider using tuples for multiple return values.

How should I name extracted helper methods?

Use meaningful names that describe the extracted responsibility, such as Validate*, Handle*, or Transform* prefixes. Keep extracted methods close to where they're used.

Full instructions (SKILL.md)

Source of truth, from github/awesome-copilot.


name: refactor-method-complexity-reduce description: 'Refactor given method ${input:methodName} to reduce its cognitive complexity to ${input:complexityThreshold} or below, by extracting helper methods.'

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