$npx -y skills add github/awesome-copilot --skill refactor-method-complexity-reduceRefactor given method ${input:methodName} to reduce its cognitive complexity to ${input:complexityThreshold} or below, by extracting helper methods.
| 1 | # Refactor Method to Reduce Cognitive Complexity |
| 2 | |
| 3 | ## Objective |
| 4 | Refactor the method `${input:methodName}`, to reduce its cognitive complexity to `${input:complexityThreshold}` or below, by extracting logic into focused helper methods. |
| 5 | |
| 6 | ## Instructions |
| 7 | |
| 8 | 1. **Analyze the current method** to identify sources of cognitive complexity: |
| 9 | - Nested conditional statements |
| 10 | - Multiple if-else or switch chains |
| 11 | - Repeated code blocks |
| 12 | - Multiple loops with conditions |
| 13 | - Complex boolean expressions |
| 14 | |
| 15 | 2. **Identify extraction opportunities**: |
| 16 | - Validation logic that can be extracted into a separate method |
| 17 | - Type-specific or case-specific processing that repeats |
| 18 | - Complex transformations or calculations |
| 19 | - Common patterns that appear multiple times |
| 20 | |
| 21 | 3. **Extract focused helper methods**: |
| 22 | - Each helper should have a single, clear responsibility |
| 23 | - Extract validation into separate `Validate*` methods |
| 24 | - Extract type-specific logic into handler methods |
| 25 | - Create utility methods for common operations |
| 26 | - Use appropriate access levels (static, private, async) |
| 27 | |
| 28 | 4. **Simplify the main method**: |
| 29 | - Reduce nesting depth |
| 30 | - Replace massive if-else chains with smaller orchestrated calls |
| 31 | - Use switch statements where appropriate for cleaner dispatch |
| 32 | - Ensure the main method reads as a high-level flow |
| 33 | |
| 34 | 5. **Preserve functionality**: |
| 35 | - Maintain the same input/output behavior |
| 36 | - Keep all validation and error handling |
| 37 | - Preserve exception types and error messages |
| 38 | - Ensure all parameters are properly passed to helpers |
| 39 | |
| 40 | 6. **Best practices**: |
| 41 | - Make helper methods static when they don't need instance state |
| 42 | - Use null checks and guard clauses early |
| 43 | - Avoid creating unnecessary local variables |
| 44 | - Consider using tuples for multiple return values |
| 45 | - Group related helper methods together |
| 46 | |
| 47 | ## Implementation Approach |
| 48 | |
| 49 | - Extract helper methods before refactoring the main flow |
| 50 | - Test incrementally to ensure no regressions |
| 51 | - Use meaningful names that describe the extracted responsibility |
| 52 | - Keep extracted methods close to where they're used |
| 53 | - Consider making repeated code patterns into generic methods |
| 54 | |
| 55 | ## Result |
| 56 | |
| 57 | The refactored method should: |
| 58 | - Have cognitive complexity reduced to the target threshold of `${input:complexityThreshold}` or below |
| 59 | - Be more readable and maintainable |
| 60 | - Have clear separation of concerns |
| 61 | - Be easier to test and debug |
| 62 | - Retain all original functionality |
| 63 | |
| 64 | ## Testing and Validation |
| 65 | |
| 66 | **CRITICAL: After completing the refactoring, you MUST:** |
| 67 | |
| 68 | 1. **Run all existing tests** related to the refactored method and its surrounding functionality |
| 69 | 2. **MANDATORY: Explicitly verify test results show "failed=0"** |
| 70 | - **NEVER assume tests passed** - always examine the actual test output |
| 71 | - Search for the summary line containing pass/fail counts (e.g., "passed=X failed=Y") |
| 72 | - **If the summary shows any number other than "failed=0", tests have FAILED** |
| 73 | - If test output is in a file, read the entire file to locate and verify the failure count |
| 74 | - Running tests is NOT the same as verifying tests passed |
| 75 | - **Do not proceed** until you have explicitly confirmed zero failures |
| 76 | 3. **If any tests fail (failed > 0):** |
| 77 | - State clearly how many tests failed |
| 78 | - Analyze each failure to understand what functionality was broken |
| 79 | - Common causes: null handling, empty collection checks, condition logic errors |
| 80 | - Identify the root cause in the refactored code |
| 81 | - Correct the refactored code to restore the original behavior |
| 82 | - Re-run tests and verify "failed=0" in the output |
| 83 | - Repeat until all tests pass (failed=0) |
| 84 | 4. **Verify compilation** - Ensure there are no compilation errors |
| 85 | 5. **Check cognitive complexity** - Confirm the metric is at or below the target threshold of `${input:complexityThreshold}` |
| 86 | |
| 87 | ## Confirmation Checklist |
| 88 | - [ ] Code compiles without errors |
| 89 | - [ ] **Test results explicitly state "failed=0"** (verified by reading the output) |
| 90 | - [ ] All test failures analyzed and corrected (if any occurred) |
| 91 | - [ ] Cognitive complexity is at or below the target threshold of `${input:complexityThreshold}` |
| 92 | - [ ] All original functionality is preserved |
| 93 | - [ ] Code follows project conventions and standards |