---
title: "Automated Refactoring Patterns"
description: "Common code patterns that can be safely automated and how to implement them with Autohand."
sidebar:
  label: "Technical Debt"
seo:
  description: "Identify and reduce technical debt with Autohand agents for automated refactoring and code quality improvement."
---

:::tip
**Related Skills:** Reduce your technical debt with specialized skills from [Skilled](https://skilled.autohand.ai). Browse [code quality and refactoring skills](https://skilled.autohand.ai/categories/quality), [debt reduction workflows](https://skilled.autohand.ai/categories/workflows), and [language-specific cleanup skills](https://skilled.autohand.ai/categories/languages).
:::

## Understanding technical debt [#overview]

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 [#detection]

Run a debt analysis to identify problem areas:

```bash
# 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 [#duplication]

Autohand identifies and consolidates duplicate code:

```bash
# 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 [#complexity]

Break down complex functions into maintainable units:

```bash
# 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 [#dependencies]

Keep dependencies updated and secure:

```bash
# 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 [#automation]

Integrate debt reduction into your workflow:

```yaml
# .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 [#prioritization]

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
