Reduce Method Complexity

Reduce Method Complexity

Refactors a named method to bring its cognitive complexity under a threshold by extracting focused helper methods while preserving behaviour.

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

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…