Complexity Reducer
This loop iteratively analyzes and reduces code complexity by generating complexity reports, identifying high-complexity areas, and applying refactoring techniques until the specified threshold is met.
Goal
Lower code complexity to meet specified thresholds through automated analysis and refactoring
How to Run
Run this loop in your coding agent to automatically reduce code complexity through iterative analysis and refactoring
- 01
Set Target Threshold
Define the acceptable complexity threshold (e.g., average cyclomatic complexity < 10)
- 02
Select Target File(s)
Specify the file(s) or directory to analyze for complexity reduction
- 03
Execute Loop
Run the loop in your coding agent - it will automatically analyze, report, and refactor until the exit condition is met
- 04
Review Changes
After loop completion, review the complexity reductions and ensure functionality remains intact
Workflow Steps
- 01
Analyze code structure and generate initial complexity report
Use static analysis tools to measure cyclomatic complexity, cognitive complexity, and other metrics across the codebase
- 02
Identify high-complexity functions and modules
Flag functions with cyclomatic complexity > 10, nesting levels > 4, or excessive conditional branches
- 03
Generate detailed complexity breakdown
Create per-function complexity scores and highlight areas contributing most to overall complexity
- 04
Apply targeted refactoring strategies
Break down large functions, extract methods, simplify conditionals, reduce nesting, and eliminate duplicate logic
- 05
Re-analyze refactored code
Run complexity report again to measure improvement and identify remaining hotspots
- 06
Check exit condition
If thresholds are met or max iterations reached, stop. Otherwise, continue to next iteration
Kickoff Prompt
Start the "Complexity Reducer" loop. Goal: Lower code complexity to meet specified thresholds through automated analysis and refactoring Max iterations: 10 Between iterations run: complexity report Exit when: Average cyclomatic complexity below target threshold (e.g., 10) OR all functions under individual complexity threshold (e.g., 20) Please analyze the complexity of my code and iteratively reduce it. Start by running a comprehensive complexity report on the target codebase. Once you have the baseline metrics, identify the highest-complexity functions and apply appropriate refactoring techniques. Continue this analyze-refactor cycle until the average cyclomatic complexity is below 10 or no further meaningful reductions are possible. Focus on breaking down large functions, simplifying conditional logic, and improving overall maintainability without changing the program's behavior. Self-pace this loop. After each iteration, run `complexity report` and evaluate the output, and only continue if the exit condition is not met (Average cyclomatic complexity below target threshold (e.g., 10) OR all functions under individual complexity threshold (e.g., 20)). Stop when the exit condition passes or 10 iterations are reached. Give a short status update each pass.
Guardrails
hardcoded- ·Preserve all existing functionality - only refactor complexity, don't change behavior
- ·Do not modify third-party libraries or external dependencies
- ·Maintain backward compatibility and API contracts
- ·Only refactor code that exceeds complexity thresholds
- ·Ensure all tests pass after each refactoring iteration
- ·Document any non-obvious complexity reductions
- ·Avoid introducing new dependencies or frameworks
- ·Do not remove necessary domain complexity - focus on accidental complexity
Flow Diagram
Related loops — Quality
Quality
Dead Code Eliminator
Automatically identifies and removes unused code to improve codebase quality and maintainability. This loop performs iterative scans for dead code, removes it gradually, and ensures no regressions are introduced.
Quality
Lint Until Clean
Automatically iterates through lint error detection and resolution until the codebase passes linting with zero issues. This loop ensures code quality and consistency by having the agent fix problems as they are identified.
Quality
Refactor Safely
This loop systematically refactors your codebase to improve structure while maintaining test coverage. The agent identifies opportunities for cleaner code without breaking existing functionality.