Generated banner for Refactor Method Complexity Reduce
Generated logo for Refactor Method Complexity Reduce

Refactor Method Complexity Reduce

GitHubOfficialOriginally contributed by @tinkeringDev as prompts/refactor-method-complexity-reduce.prompt.md; moved into a skill by pull request #761Distributed by GitHub

  • Skill
  • Workflow
  • Software development
  • MIT
  • No issues found

From the author

Refactor given method `${input:methodName}` to reduce its cognitive complexity to `${input:complexityThreshold}` or below, by extracting helper methods.
— the description by tinkeringDev

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a systematic approach to refactoring complex methods. It guides the user through identifying sources of high cognitive complexity, such as deep nesting or complex conditionals, and extracting them into focused, maintainable helper methods. The process emphasizes strict validation through testing to ensure that the refactoring preserves original functionality and meets the target complexity threshold.

What it does

  • Analyzes methods for high cognitive complexity indicators like deep nesting or complex boolean logic.
  • Guides the extraction of logic into focused, single-responsibility helper methods.
  • Provides a checklist for maintaining functional parity and code quality.
  • Mandates a rigorous verification process to ensure zero test failures after refactoring.
  • Ensures the final code meets a specific cognitive complexity threshold.

When to use it

  • When a method has become too complex to read or maintain easily.
  • When you need to lower the cognitive complexity score of a specific code block.
  • When preparing code for easier unit testing by breaking down large functions.
  • When you want to improve code readability by replacing massive if-else chains with clear, high-level flows.

Advantages

  • Improves overall code maintainability and readability.
  • Reduces the risk of introducing bugs during refactoring through mandatory test verification.
  • Promotes clean code principles by enforcing single-responsibility helper methods.
  • Provides a clear, repeatable process for technical debt reduction.

Usage examples

“Refactor the method processOrder to reduce its cognitive complexity to 5 or below.”
The agent will analyze the processOrder method, identify complex logic blocks, extract them into private helper methods, and verify the changes with tests.
“Use refactor-method-complexity-reduce on calculateTaxDetails with a threshold of 3.”
The agent will break down the calculateTaxDetails method into smaller, simpler functions until the complexity metric reaches 3 or less, ensuring all tests pass.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/github/refactor-method-complexity-reduce/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip refactor-method-complexity-reduce.zip -d .agents/skills

Documentation by tinkeringDev

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

Files

Requirements and compatibility

Written for: GitHub Copilot

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Originally contributed by
GitHub avatar of tinkeringDev@tinkeringDevas prompts/refactor-method-complexity-reduce.prompt.mdmoved into a skill by pull request #761
Publisher
GitHub (Official)
Distributed by
GitHub
Source
github/awesome-copilot · skills/refactor-method-complexity-reduce · commit 797337f
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Contributors
tinkeringDev

AutoGineer is not affiliated with or endorsed by tinkeringDev or GitHub.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download refactor-method-complexity-reduce.zip