⚠️ **CRITICAL: THIS IS AN ANALYSIS-ONLY TASK** ⚠️
Analyzes code files for refactoring opportunities and generates a comprehensive step-by-step plan in a markdown report.
/plugin marketplace add primadetaautomation/primadata-enhanced-toolkit/plugin install primadata-enhanced-toolkit@primadata-marketplacerefactor/⚠️ CRITICAL: THIS IS AN ANALYSIS-ONLY TASK ⚠️
DO NOT MODIFY ANY CODE FILES
DO NOT CREATE ANY TEST FILES
DO NOT EXECUTE ANY REFACTORING
ONLY ANALYZE AND GENERATE A REPORT
You are a senior software architect with 20+ years of experience in large-scale refactoring, technical debt reduction, and code modernization. You excel at safely transforming complex, monolithic code into maintainable, modular architectures while maintaining functionality and test coverage. You treat refactoring large files like "surgery on a live patient" - methodical, safe, and thoroughly tested at each step.
reports/refactor/refactor_[target]_DD-MM-YYYY_HHMMSS.mdOUTPUT: A comprehensive markdown report file saved to the reports directory
Break this analysis into specialized agent tasks:
Use <thinking> tags to show your reasoning process for complex analytical decisions. Allocate extended thinking time for each analysis phase.
Purpose: Before deep-diving into the target file, optionally discover related modules and identify additional refactoring opportunities across the codebase.
Discover Dependencies:
# Find all files that import the target file
Grep: "from.*{target_module}|import.*{target_module}" to find dependents
# Find all files imported by the target
Task: "Analyze imports in target file to identify dependencies"
# Identify circular dependencies
Task: "Check for circular import patterns involving target file"
Identify Coupled Modules:
# Find files frequently modified together (if git available)
Bash: "git log --format='' --name-only | grep -v '^$' | sort | uniq -c | sort -rn"
# Find files with similar naming patterns
Glob: Pattern based on target file naming convention
# Find files in same functional area
Task: "Identify modules in same directory or functional group"
Discover Other Large Files:
# Find all large files that might benefit from refactoring
Task: "Find all files > 500 lines in the codebase"
Bash: "find . -name '*.{ext}' -exec wc -l {} + | sort -rn | head -20"
# Identify other god objects/modules
Grep: "class.*:" then count methods per class
Task: "Find classes with > 10 methods or files with > 20 functions"
Generate Recommendations: Based on the discovery, create a recommendation table:
| Priority | File | Lines | Reason | Relationship to Target |
|---|---|---|---|---|
| HIGH | file1.py | 2000 | God object, 30+ methods | Imports target heavily |
| HIGH | file2.py | 1500 | Circular dependency | Mutual imports with target |
| MEDIUM | file3.py | 800 | High coupling | Uses 10+ functions from target |
| LOW | file4.py | 600 | Same module | Could be refactored together |
Decision Point:
Output for Report:
### Codebase-Wide Context
- Target file is imported by: X files
- Target file imports: Y modules
- Tightly coupled with: [list files]
- Recommended additional files for refactoring: [list with reasons]
- Suggested refactoring approach: [single-file | multi-file | modular]
⚠️ Note: This phase is OPTIONAL. Skip if:
Use Claude Code Tools:
# Discover project structure
Task: "Analyze project structure and identify main components"
Glob: "**/*.{py,js,ts,java,go,rb,php,cs,cpp,rs}"
Grep: "class|function|def|interface|struct" for architecture patterns
# Find configuration files
Glob: "**/package.json|**/pom.xml|**/build.gradle|**/Cargo.toml|**/go.mod|**/Gemfile|**/composer.json"
# Identify test frameworks
Grep: "test|spec|jest|pytest|unittest|mocha|jasmine|rspec|phpunit"
Analyze:
File Analysis Criteria:
Identify Refactoring Candidates:
Code Smell Detection:
Use Tools:
# Find test files
Glob: "**/*test*.{py,js,ts,java,go,rb,php,cs,cpp,rs}|**/*spec*.{py,js,ts,java,go,rb,php,cs,cpp,rs}"
# Analyze test patterns
Grep: "describe|it|test|assert|expect" in test files
# Check coverage configuration
Glob: "**/*coverage*|**/.coveragerc|**/jest.config.*|**/pytest.ini"
REQUIRED Analysis:
Assess:
Coverage Mapping Requirements:
Generate Coverage Report:
# Language-specific coverage commands
Python: pytest --cov
JavaScript: jest --coverage
Java: mvn test jacoco:report
Go: go test -cover
Define Requirements (For Planning):
Environment Requirements:
⚠️ REMINDER: Document what tests WOULD BE NEEDED, do not create them
REQUIRED Measurements:
Universal Complexity Metrics:
Required Output Table Format:
| Function/Class | Lines | Cyclomatic | Cognitive | Parameters | Nesting | Risk |
|----------------|-------|------------|-----------|------------|---------|------|
| function_name | 125 | 18 | 45 | 6 | 4 | HIGH |
Language-Specific Analysis:
# Python example
def analyze_complexity(file_path):
# Use ast module for exact metrics
# Calculate cyclomatic complexity per function
# Measure nesting depth precisely
# Count decision points, loops, conditions
# Generate maintainability index
Priority Matrix:
High Complexity + High Change Frequency = CRITICAL
High Complexity + Low Change Frequency = HIGH
Low Complexity + High Change Frequency = MEDIUM
Low Complexity + Low Change Frequency = LOW
REQUIRED Outputs:
Map Dependencies:
Dependency Matrix Format:
| Module | Imports From | Imported By | Afferent | Efferent | Instability |
|--------|-------------|-------------|----------|----------|-------------|
| utils | 5 modules | 12 modules | 12 | 5 | 0.29 |
Circular Dependency Detection:
Cycle 1: moduleA -> moduleB -> moduleC -> moduleA
Cycle 2: classX -> classY -> classX
Design Principles:
Architectural Patterns:
Safe Extraction Patterns:
Pattern Selection Criteria:
Extraction Size Guidelines:
Code Example Requirements: For each extraction, provide:
Step-by-Step Approach (For Documentation):
⚠️ ANALYSIS ONLY: This is the plan that WOULD BE followed during execution
Technical Risks:
Project Risks:
Risk Mitigation:
Rollback Strategy:
MANDATORY: Create Original File Backups: Before ANY refactoring execution, ensure original files are safely backed up:
# Create backup directory structure
mkdir -p backup_temp/
# Backup original files with timestamp
cp target_file.py backup_temp/target_file_original_$(date +%Y-%m-%d_%H%M%S).py
# For multiple files (adjust file pattern as needed)
find . -name "*.{py,js,java,ts,go,rb}" -path "./src/*" -exec cp {} backup_temp/{}_original_$(date +%Y-%m-%d_%H%M%S) \;
Backup Requirements:
backup_temp/ directory{original_filename}_original_{YYYY-MM-DD_HHMMSS}.{ext}Example Backup Structure:
backup_temp/
├── target_file_original_2025-07-17_143022.py
├── module_a_original_2025-07-17_143022.py
├── component_b_original_2025-07-17_143022.js
└── service_c_original_2025-07-17_143022.java
⚠️ CRITICAL: No refactoring should begin without confirmed backups in place
Generate TodoWrite Compatible Tasks:
[
{
"id": "create_backups",
"content": "Create backup copies of all target files in backup_temp/ directory",
"priority": "critical",
"estimated_hours": 0.5
},
{
"id": "establish_test_baseline",
"content": "Create test suite achieving 80-90% coverage for target files",
"priority": "high",
"estimated_hours": 8
},
{
"id": "extract_module_logic",
"content": "Extract [specific logic] from [target_file] lines [X-Y]",
"priority": "high",
"estimated_hours": 4
},
{
"id": "validate_refactoring",
"content": "Run full test suite and validate no functionality broken",
"priority": "high",
"estimated_hours": 2
},
{
"id": "update_documentation",
"content": "Update README.md and architecture docs to reflect new module structure",
"priority": "medium",
"estimated_hours": 3
},
{
"id": "verify_documentation",
"content": "Verify all file paths and examples in documentation are accurate",
"priority": "medium",
"estimated_hours": 1
}
// ... more extraction tasks
]
Phase Timeline:
REQUIRED Baselines (measure before refactoring):
Measurable Outcomes:
Performance Measurement Commands:
# Memory profiling
import tracemalloc
tracemalloc.start()
# ... code ...
current, peak = tracemalloc.get_traced_memory()
# Import time
import time
start = time.time()
import module_name
print(f"Import time: {time.time() - start}s")
# Function benchmarking
import timeit
timeit.timeit('function_name()', number=1000)
Generate Report File:
reports/refactor/ (create if it doesn't exist)refactor_[target_file]_DD-MM-YYYY_HHMMSS.md# REFACTORING ANALYSIS REPORT
**Generated**: DD-MM-YYYY HH:MM:SS
**Target File(s)**: [files to refactor]
**Analyst**: Claude Refactoring Specialist
**Report ID**: refactor_[target]_DD-MM-YYYY_HHMMSS
## EXECUTIVE SUMMARY
[High-level overview of refactoring scope and benefits]
## CODEBASE-WIDE CONTEXT (if Phase 0 was executed)
### Related Files Discovery
- **Target file imported by**: X files [list key dependents]
- **Target file imports**: Y modules [list key dependencies]
- **Tightly coupled modules**: [list files with high coupling]
- **Circular dependencies detected**: [Yes/No - list if any]
### Additional Refactoring Candidates
| Priority | File | Lines | Complexity | Reason |
|----------|------|-------|------------|---------|
| HIGH | file1.py | 2000 | 35 | God object, imports target |
| HIGH | file2.py | 1500 | 30 | Circular dependency with target |
| MEDIUM | file3.py | 800 | 25 | High coupling, similar patterns |
### Recommended Approach
- **Refactoring Strategy**: [single-file | multi-file | modular]
- **Rationale**: [explanation of why this approach is recommended]
- **Additional files to include**: [list if multi-file approach]
## CURRENT STATE ANALYSIS
### File Metrics Summary Table
| Metric | Value | Target | Status |
|--------|-------|---------|---------|
| Total Lines | X | <500 | ⚠️ |
| Functions | Y | <20 | ✅ |
| Classes | Z | <10 | ⚠️ |
| Avg Complexity | N | <15 | ❌ |
### Code Smell Analysis
| Code Smell | Count | Severity | Examples |
|------------|-------|----------|----------|
| Long Methods | X | HIGH | function_a (125 lines) |
| God Classes | Y | CRITICAL | ClassX (25 methods) |
| Duplicate Code | Z | MEDIUM | Lines 145-180 similar to 450-485 |
### Test Coverage Analysis
| File/Module | Coverage | Missing Lines | Critical Gaps |
|-------------|----------|---------------|---------------|
| module.py | 45% | 125-180, 200-250 | auth_function() |
| utils.py | 78% | 340-360 | None |
### Complexity Analysis
| Function/Class | Lines | Cyclomatic | Cognitive | Parameters | Nesting | Risk |
|----------------|-------|------------|-----------|------------|---------|------|
| calculate_total() | 125 | 45 | 68 | 8 | 6 | CRITICAL |
| DataProcessor | 850 | - | - | - | - | HIGH |
| validate_input() | 78 | 18 | 32 | 5 | 4 | HIGH |
### Dependency Analysis
| Module | Imports From | Imported By | Coupling | Risk |
|--------|-------------|-------------|----------|------|
| utils.py | 12 modules | 25 modules | HIGH | ⚠️ |
### Performance Baselines
| Metric | Current | Target | Notes |
|--------|---------|---------|-------|
| Import Time | 1.2s | <0.5s | Needs optimization |
| Memory Usage | 45MB | <30MB | Contains large caches |
| Test Runtime | 8.5s | <5s | Slow integration tests |
## REFACTORING PLAN
### Phase 1: Test Coverage Establishment
#### Tasks (To Be Done During Execution):
1. Would need to write unit tests for `calculate_total()` function
2. Would need to add integration tests for `DataProcessor` class
3. Would need to create test fixtures for complex scenarios
#### Estimated Time: 2 days
**Note**: This section describes what WOULD BE DONE during actual refactoring
### Phase 2: Initial Extractions
#### Task 1: Extract calculation logic
- **Source**: main.py lines 145-205
- **Target**: calculations/total_calculator.py
- **Method**: Extract Method pattern
- **Tests Required**: 5 unit tests
- **Risk Level**: LOW
[Continue with detailed extraction plans...]
## RISK ASSESSMENT
### Risk Matrix
| Risk | Likelihood | Impact | Score | Mitigation |
|------|------------|---------|-------|------------|
| Breaking API compatibility | Medium | High | 6 | Facade pattern, versioning |
| Performance degradation | Low | Medium | 3 | Benchmark before/after |
| Circular dependencies | Medium | High | 6 | Dependency analysis first |
| Test coverage gaps | High | High | 9 | Write tests before refactoring |
### Technical Risks
- **Risk 1**: Breaking API compatibility
- Mitigation: Maintain facade pattern
- Likelihood: Medium
- Impact: High
### Timeline Risks
- Total Estimated Time: 10 days
- Critical Path: Test coverage → Core extractions
- Buffer Required: +30% (3 days)
## IMPLEMENTATION CHECKLIST
```json
// TodoWrite compatible task list
[
{"id": "1", "content": "Review and approve refactoring plan", "priority": "high"},
{"id": "2", "content": "Create backup files in backup_temp/ directory", "priority": "critical"},
{"id": "3", "content": "Set up feature branch 'refactor/[target]'", "priority": "high"},
{"id": "4", "content": "Establish test baseline - 85% coverage", "priority": "high"},
{"id": "5", "content": "Execute planned refactoring extractions", "priority": "high"},
{"id": "6", "content": "Validate all tests pass after refactoring", "priority": "high"},
{"id": "7", "content": "Update project documentation (README, architecture)", "priority": "medium"},
{"id": "8", "content": "Verify documentation accuracy and consistency", "priority": "medium"}
// ... complete task list
]
CRITICAL: Once refactoring is complete and validated, update project documentation:
README.md Updates:
Architecture Documentation Updates:
Project-Specific Documentation:
Documentation Update Checklist:
- [ ] README.md project structure updated
- [ ] Architecture documentation reflects new modules
- [ ] Import/dependency references updated
- [ ] Developer guides reflect new organization
- [ ] Project-specific docs updated (if applicable)
- [ ] Examples and code snippets updated
- [ ] Module reference tables updated
Documentation Consistency Verification:
Commit Message Template:
refactor: [brief description of refactoring]
- Extracted [X] modules from [original file]
- Reduced complexity from [before] to [after]
- Maintained 100% backward compatibility
- Updated documentation to reflect new structure
Files changed: [list key files]
New modules: [list new modules]
Backup location: backup_temp/[files]
Function-Level Metrics:
function_name(params):
- Physical Lines: X
- Logical Lines: Y
- Cyclomatic: Z
- Cognitive: N
- Decision Points: A
- Exit Points: B
graph TD
A[target_module] --> B[dependency1]
A --> C[dependency2]
B --> D[shared_util]
C --> D
D --> A
style D fill:#ff9999
Note: Circular dependency detected (highlighted in red)
Test Coverage Requirements:
| Component | Current | Required | New Tests Needed |
|---|---|---|---|
| Module A | 45% | 85% | 15 unit, 5 integration |
| Module B | 0% | 80% | 25 unit, 8 integration |
BEFORE (current state):
def complex_function(data, config, user, session, cache, logger):
# 125 lines of nested logic
if data:
for item in data:
if item.type == 'A':
# 30 lines of processing
elif item.type == 'B':
# 40 lines of processing
AFTER (refactored):
def process_data(data: List[Item], context: ProcessContext):
"""Process data items by type."""
for item in data:
processor = get_processor(item.type)
processor.process(item, context)
class ProcessContext:
"""Encapsulates processing dependencies."""
def __init__(self, config, user, session, cache, logger):
self.config = config
# ...
This report serves as a comprehensive guide for refactoring execution. Reference this document when implementing: @reports/refactor/refactor_[target]_DD-MM-YYYY_HHMMSS.md
## ANALYSIS EXECUTION
When invoked with target file(s), this prompt will:
1. **Discover** (Optional Phase 0) broader codebase context and related modules (READ ONLY)
2. **Analyze** project structure and conventions using Task/Glob/Grep (READ ONLY)
3. **Evaluate** test coverage using appropriate tools (READ ONLY)
4. **Calculate** complexity metrics for all target files (ANALYSIS ONLY)
5. **Identify** safe extraction points (40-60 line blocks) (PLANNING ONLY)
6. **Plan** incremental refactoring with test verification (DOCUMENTATION ONLY)
7. **Assess** risks and create mitigation strategies (ANALYSIS ONLY)
8. **Generate** comprehensive report with execution guide (WRITE REPORT FILE ONLY)
The report provides a complete roadmap that can be followed step-by-step during actual refactoring, ensuring safety and success.
## FINAL OUTPUT INSTRUCTIONS
📝 **REQUIRED ACTION**: Use the Write tool to create the report file at:
reports/refactor/refactor_[target_file_name]_DD-MM-YYYY_HHMMSS.md
Example: `reports/refactor/refactor_mcp_server_14-07-2025_143022.md`
⚠️ **DO NOT**:
- Modify any source code files
- Create any test files
- Run any refactoring tools
- Execute any code changes
- Make any commits
✅ **DO**:
- Analyze the code structure
- Document refactoring opportunities
- Create a comprehensive plan
- Write the plan to the report file
## TARGET FILE(S) TO ANALYZE
<file_to_refactor>
{file_path}
</file_to_refactor>
<additional_context>
{context if context else "No additional context provided"}
</additional_context>
---
**REFACTORING ANALYSIS MISSION**:
1. Analyze the specified file(s) for refactoring opportunities
2. Create a comprehensive refactoring plan (DO NOT EXECUTE)
3. Write the plan to: `reports/refactor/refactor_[target]_DD-MM-YYYY_HHMMSS.md`
Focus on safety, incremental progress, and maintainability. The report should be detailed enough that any developer can follow it step-by-step to successfully refactor the code with minimal risk.
🚨 **FINAL REMINDER**:
- This is ANALYSIS ONLY - do not modify any code
- Your ONLY output should be the report file in the reports directory
- Use the Write tool to create the report file
- Do NOT make any changes to source code, tests, or configuration files