Automated Refactoring Patterns
Common code patterns that can be safely automated and how to implement them with Autohand.
Understanding technical debt
Technical debt accumulates when teams prioritize speed over code quality. Autohand helps identify and resolve common debt patterns systematically.
- Code duplication across modules
- Outdated dependencies with security vulnerabilities
- Complex functions exceeding maintainability thresholds
- Missing or outdated documentation
Detecting technical debt
Run a debt analysis to identify problem areas:
# Full codebase analysis
autohand -p "Analyze this codebase for technical debt; write a report to reports/debt-report.md grouped by severity with estimated remediation effort"
# Focus on specific patterns (read-only)
autohand --plan -p "Find code duplication and overly complex functions in src/"
# Integrate with CI
autohand -p "Review this branch for new technical debt compared to main" --restricted --output-format stream-json
Ask for the report to categorize issues by severity and estimate remediation effort, then review it before acting on it.
Eliminating duplication
Autohand identifies and consolidates duplicate code:
# Find duplicate patterns
autohand --plan -p "List duplicated code blocks of 10 or more lines in src/"
# Extract common functions
autohand --worktree extract-common \
-p "Extract the duplicated helpers in src/modules/ into shared functions under src/shared/ and update the callers"
# Preview changes
autohand -p "Extract the duplicated helpers in src/modules/ into src/shared/" --dry-run
Reducing complexity
Break down complex functions into maintainable units:
# Find functions exceeding complexity threshold
autohand --plan -p "List functions in src/ with cyclomatic complexity above 15"
# Suggest decomposition
autohand --plan -p "Suggest how to decompose the largest functions in src/handlers/payment.ts"
# Apply with review (interactive session; approve each change)
autohand
Autohand reads the code and suggests logical boundaries for function extraction; you review and approve each change.
Dependency management
Keep dependencies updated and secure:
# Audit dependencies
npm audit
# Update with compatibility checks
autohand --worktree deps-update -p "Update dependencies to the latest compatible minor versions and run npm test after each change"
# Fix security vulnerabilities
autohand -p "Fix the high-severity vulnerabilities reported by npm audit, then run npm test"
# Generate upgrade plan
autohand -p "Plan upgrades of all dependencies to their latest major versions; write the plan to docs/upgrade-plan.md"
Continuous debt reduction
Integrate debt reduction into your workflow:
# .github/workflows/debt-reduction.yml
name: Technical Debt Check
on: [pull_request]
jobs:
debt-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run debt analysis
run: |
autohand -p "Compare this pull request with main and report any new technical debt" \
--restricted \
--output-format stream-json
Running this on every pull request surfaces new debt for reviewers before it lands, while allowing gradual improvement.
Prioritization framework
Focus on high-impact improvements:
- Security: Fix vulnerabilities immediately
- Stability: Address code causing production issues
- Velocity: Refactor areas slowing development
- Maintainability: Improve code for long-term health