Skip to content

Commit 7cd4d95

Browse files
authored
Merge pull request #729 from tinkeringDev/refactor-method-cognitive-reduce
Add prompt to refactor a given method to a specified complexity threshold
2 parents 7d2dab7 + ad89e38 commit 7cd4d95

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

docs/README.prompts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Ready-to-use prompt templates for specific development scenarios and tasks, defi
121121
| [Project Workflow Documentation Generator](../prompts/project-workflow-analysis-blueprint-generator.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fproject-workflow-analysis-blueprint-generator.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fproject-workflow-analysis-blueprint-generator.prompt.md) | Comprehensive technology-agnostic prompt generator for documenting end-to-end application workflows. Automatically detects project architecture patterns, technology stacks, and data flow patterns to generate detailed implementation blueprints covering entry points, service layers, data access, error handling, and testing approaches across multiple technologies including .NET, Java/Spring, React, and microservices architectures. |
122122
| [Pytest Coverage](../prompts/pytest-coverage.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fpytest-coverage.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fpytest-coverage.prompt.md) | Run pytest tests with coverage, discover lines missing coverage, and increase coverage to 100%. |
123123
| [README Generator Prompt](../prompts/readme-blueprint-generator.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freadme-blueprint-generator.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freadme-blueprint-generator.prompt.md) | Intelligent README.md generation prompt that analyzes project documentation structure and creates comprehensive repository documentation. Scans .github/copilot directory files and copilot-instructions.md to extract project information, technology stack, architecture, development workflow, coding standards, and testing approaches while generating well-structured markdown documentation with proper formatting, cross-references, and developer-focused content. |
124+
| [Refactor Method Complexity Reduce](../prompts/refactor-method-complexity-reduce.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frefactor-method-complexity-reduce.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frefactor-method-complexity-reduce.prompt.md) | Refactor given method `${input:methodName}` to reduce its cognitive complexity to `${input:complexityThreshold}` or below, by extracting helper methods. |
124125
| [Refactor Plan](../prompts/refactor-plan.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frefactor-plan.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frefactor-plan.prompt.md) | Plan a multi-file refactor with proper sequencing and rollback steps |
125126
| [Refactoring Java Methods with Extract Method](../prompts/java-refactoring-extract-method.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-extract-method.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-extract-method.prompt.md) | Refactoring using Extract Methods in Java Language |
126127
| [Refactoring Java Methods with Remove Parameter](../prompts/java-refactoring-remove-parameter.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-remove-parameter.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-remove-parameter.prompt.md) | Refactoring using Remove Parameter in Java Language |
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
argument-hint: methodName=..., complexityThreshold=15
5+
agent: agent
6+
model: Auto (copilot)
7+
tools: ['search/changes', 'search/codebase', 'edit/editFiles', 'read/problems', 'execute/runTests']
8+
---
9+
10+
# Refactor Method to Reduce Cognitive Complexity
11+
12+
## Objective
13+
Refactor the method `${input:methodName}`, to reduce its cognitive complexity to `${input:complexityThreshold}` or below, by extracting logic into focused helper methods.
14+
15+
## Instructions
16+
17+
1. **Analyze the current method** to identify sources of cognitive complexity:
18+
- Nested conditional statements
19+
- Multiple if-else or switch chains
20+
- Repeated code blocks
21+
- Multiple loops with conditions
22+
- Complex boolean expressions
23+
24+
2. **Identify extraction opportunities**:
25+
- Validation logic that can be extracted into a separate method
26+
- Type-specific or case-specific processing that repeats
27+
- Complex transformations or calculations
28+
- Common patterns that appear multiple times
29+
30+
3. **Extract focused helper methods**:
31+
- Each helper should have a single, clear responsibility
32+
- Extract validation into separate `Validate*` methods
33+
- Extract type-specific logic into handler methods
34+
- Create utility methods for common operations
35+
- Use appropriate access levels (static, private, async)
36+
37+
4. **Simplify the main method**:
38+
- Reduce nesting depth
39+
- Replace massive if-else chains with smaller orchestrated calls
40+
- Use switch statements where appropriate for cleaner dispatch
41+
- Ensure the main method reads as a high-level flow
42+
43+
5. **Preserve functionality**:
44+
- Maintain the same input/output behavior
45+
- Keep all validation and error handling
46+
- Preserve exception types and error messages
47+
- Ensure all parameters are properly passed to helpers
48+
49+
6. **Best practices**:
50+
- Make helper methods static when they don't need instance state
51+
- Use null checks and guard clauses early
52+
- Avoid creating unnecessary local variables
53+
- Consider using tuples for multiple return values
54+
- Group related helper methods together
55+
56+
## Implementation Approach
57+
58+
- Extract helper methods before refactoring the main flow
59+
- Test incrementally to ensure no regressions
60+
- Use meaningful names that describe the extracted responsibility
61+
- Keep extracted methods close to where they're used
62+
- Consider making repeated code patterns into generic methods
63+
64+
## Result
65+
66+
The refactored method should:
67+
- Have cognitive complexity reduced to the target threshold of `${input:complexityThreshold}` or below
68+
- Be more readable and maintainable
69+
- Have clear separation of concerns
70+
- Be easier to test and debug
71+
- Retain all original functionality
72+
73+
## Testing and Validation
74+
75+
**CRITICAL: After completing the refactoring, you MUST:**
76+
77+
1. **Run all existing tests** related to the refactored method and its surrounding functionality
78+
2. **MANDATORY: Explicitly verify test results show "failed=0"**
79+
- **NEVER assume tests passed** - always examine the actual test output
80+
- Search for the summary line containing pass/fail counts (e.g., "passed=X failed=Y")
81+
- **If the summary shows any number other than "failed=0", tests have FAILED**
82+
- If test output is in a file, read the entire file to locate and verify the failure count
83+
- Running tests is NOT the same as verifying tests passed
84+
- **Do not proceed** until you have explicitly confirmed zero failures
85+
3. **If any tests fail (failed > 0):**
86+
- State clearly how many tests failed
87+
- Analyze each failure to understand what functionality was broken
88+
- Common causes: null handling, empty collection checks, condition logic errors
89+
- Identify the root cause in the refactored code
90+
- Correct the refactored code to restore the original behavior
91+
- Re-run tests and verify "failed=0" in the output
92+
- Repeat until all tests pass (failed=0)
93+
4. **Verify compilation** - Ensure there are no compilation errors
94+
5. **Check cognitive complexity** - Confirm the metric is at or below the target threshold of `${input:complexityThreshold}`
95+
96+
## Confirmation Checklist
97+
- [ ] Code compiles without errors
98+
- [ ] **Test results explicitly state "failed=0"** (verified by reading the output)
99+
- [ ] All test failures analyzed and corrected (if any occurred)
100+
- [ ] Cognitive complexity is at or below the target threshold of `${input:complexityThreshold}`
101+
- [ ] All original functionality is preserved
102+
- [ ] Code follows project conventions and standards

0 commit comments

Comments
 (0)