refactor-method-complexity-reduce

软件研发 架构与规范 1 文件 EN 原文

'Refactor given method `${input:methodName}` to

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

说明语言:英文

来源githubawesome-copilot ↗ 查看原文件 ↗版权归原作者,请遵循原仓库许可
安装
npx skills add https://github.com/github/awesome-copilot --skill refactor-method-complexity-reduce

需要已安装 skills CLI。也可以直接从源仓库 clone。

在星云中查看 ↗
SKILL.md 原文

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:
  2. Nested conditional statements
  3. Multiple if-else or switch chains
  4. Repeated code blocks
  5. Multiple loops with conditions
  6. Complex boolean expressions
  1. Identify extraction opportunities:
  2. Validation logic that can be extracted into a separate method
  3. Type-specific or case-specific processing that repeats
  4. Complex transformations or calculations
  5. Common patterns that appear multiple times
  1. Extract focused helper methods:
  2. Each helper should have a single, clear responsibility
  3. Extract validation into separate Validate* methods
  4. Extract type-specific logic into handler methods
  5. Create utility methods for common operations
  6. Use appropriate access levels (static, private, async)
  1. Simplify the main method:
  2. Reduce nesting depth
  3. Replace massive if-else chains with smaller orchestrated calls
  4. Use switch statements where appropriate for cleaner dispatch
  5. Ensure the main method reads as a high-level flow
  1. Preserve functionality:
  2. Maintain the same input/output behavior
  3. Keep all validation and error handling
  4. Preserve exception types and error messages
  5. Ensure all parameters are properly passed to helpers
  1. Best practices:
  2. Make helper methods static when they don't need instance state
  3. Use null checks and guard clauses early
  4. Avoid creating unnecessary local variables
  5. Consider using tuples for multiple return values
  6. Group related helper methods together

Implementation Approach

Result

The refactored method should:

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"
  3. NEVER assume tests passed - always examine the actual test output
  4. Search for the summary line containing pass/fail counts (e.g., "passed=X failed=Y")
  5. If the summary shows any number other than "failed=0", tests have FAILED
  6. If test output is in a file, read the entire file to locate and verify the failure count
  7. Running tests is NOT the same as verifying tests passed
  8. Do not proceed until you have explicitly confirmed zero failures
  9. If any tests fail (failed > 0):
  10. State clearly how many tests failed
  11. Analyze each failure to understand what functionality was broken
  12. Common causes: null handling, empty collection checks, condition logic errors
  13. Identify the root cause in the refactored code
  14. Correct the refactored code to restore the original behavior
  15. Re-run tests and verify "failed=0" in the output
  16. Repeat until all tests pass (failed=0)
  17. Verify compilation - Ensure there are no compilation errors
  18. Check cognitive complexity - Confirm the metric is at or below the target threshold of ${input:complexityThreshold}

Confirmation Checklist

同类技能
architecture-blueprint-generatorarchitecture-patternsasync-python-patternsbash-defensive-patternsbinary-analysis-patternscqrs-implementationcreate-readmecreate-specification
本页内容来自开源项目 github, 版权归原作者所有。
本站仅作技能索引与分享,所有 Skill 版权归其原作者所有。