Back to The Times of Claw

AI for Technical Debt: Identify and Prioritize Refactors

Use AI to audit, prioritize, and tackle technical debt systematically. How DenchClaw and gstack help engineering teams manage debt without stalling.

Mark Rachapoom
Mark Rachapoom
·7 min read
AI for Technical Debt: Identify and Prioritize Refactors

AI for Technical Debt: Identify and Prioritize Refactors

Technical debt is the mortgage on your codebase. Every shortcut you take, every "we'll clean this up later," every temporary solution that became permanent — these are the interest payments that slow down every future feature and compound over time.

Most teams handle technical debt in one of two dysfunctional ways: ignore it until the system becomes unmaintainable, or declare a "tech debt sprint" that tries to fix everything at once and achieves very little. Neither works.

AI provides a third way: systematic, continuous debt identification and prioritized, incremental reduction.

What Technical Debt Actually Looks Like#

Technical debt isn't just "bad code." It manifests in multiple forms:

Design debt: Architectural decisions that made sense early but constrain the system now. A monolith that needs to be split. A data model that doesn't fit current requirements.

Code debt: Functions that are too long, overly complex, poorly named, or duplicated. Code that works but is hard to understand or modify.

Dependency debt: Outdated dependencies with known vulnerabilities or that have been superseded by better alternatives.

Test debt: Missing test coverage, fragile tests that break on minor changes, or tests that test implementation rather than behavior.

Documentation debt: Code without documentation, outdated comments, missing ADRs.

Infrastructure debt: Configuration management that doesn't scale, manual processes that should be automated, monitoring gaps.

AI can identify and quantify each of these.

AI-Powered Technical Debt Audit#

The first step: understand what you have. For an existing codebase, run a technical debt audit.

DenchClaw can run a systematic audit across the codebase:

Complexity Analysis#

# Identify functions with high cyclomatic complexity
denchclaw-ci audit complexity \
  --threshold 15 \
  --output complexity-report.json

Output:

High Complexity Functions (cyclomatic complexity > 15):
────────────────────────────────────────────────────
src/api/contacts.ts:createContact() — complexity: 34
  Description: 34 branches suggests this function is doing too much.
  Refactor: Split into validateContact(), enrichContact(), and persistContact()
  Risk: HIGH (core API endpoint)
  
src/utils/import.ts:processImportRow() — complexity: 28
  Description: Handles 28 different conditions for data normalization.
  Refactor: Extract a validation strategy per field type
  Risk: MEDIUM (import is a background operation)

Duplication Analysis#

denchclaw-ci audit duplication \
  --min-lines 10 \
  --similarity 0.85

Output:

Duplicated Code Blocks:
──────────────────────
Group 1 (3 occurrences, 28 lines each):
  src/api/contacts.ts:47
  src/api/companies.ts:52
  src/api/deals.ts:38
  Pattern: Pagination and sorting logic
  Refactor: Extract to shared utils/pagination.ts

Dependency Audit#

denchclaw-ci audit dependencies

Output:

Outdated/Risky Dependencies:
────────────────────────────
lodash@4.17.11 — 3 versions behind (current: 4.17.21)
  Reason: Multiple security patches in newer versions
  Action: npm update lodash

moment@2.29.1 — deprecated
  Reason: Maintainers recommend migrating to date-fns or day.js
  Action: Migrate to date-fns (estimated 4 hours)
  Benefit: 67KB bundle size reduction

@types/react@17.0.0 — major version behind
  Action: npm update @types/react

Test Coverage Gaps#

Beyond coverage percentage, AI identifies which uncovered code is high risk:

Critical Test Coverage Gaps:
─────────────────────────────
src/services/billing.ts — 0% coverage
  Risk: CRITICAL (handles payment processing)
  Recommended tests: createSubscription, processPayment, handleWebhook, cancelSubscription
  
src/api/contacts.ts:47-89 — Not covered
  Risk: HIGH (bulk delete operation)
  Recommended tests: bulk delete with empty array, single record, 1000+ records

Building a Technical Debt Register#

Once you have the audit, store the debt items in DenchClaw's database:

Technical Debt Object:

  • Debt type (enum: design, code, dependency, test, documentation, infrastructure)
  • Description and location
  • Complexity score (estimated effort to fix)
  • Risk score (impact if not fixed — security vulnerability is higher than naming inconsistency)
  • Priority score (risk/effort ratio)
  • Owner
  • Status (identified, planned, in-progress, resolved)
-- Technical debt prioritization view
SELECT 
  td.title,
  td.debt_type,
  td.file_path,
  td.risk_score,
  td.complexity_score,
  ROUND(td.risk_score::FLOAT / NULLIF(td.complexity_score, 0), 2) as priority_ratio,
  td.status
FROM v_technical_debt td
WHERE td.status != 'resolved'
ORDER BY priority_ratio DESC
LIMIT 20;

This gives you your top 20 debt items by risk-to-effort ratio. These are the items to tackle first.

The "Boy Scout Rule" Applied with AI#

The boy scout rule: always leave the codebase cleaner than you found it. Every PR should include a small cleanup in the area of code you touched.

AI makes this practical:

When you open a PR for a feature change, gstack's Engineering Review identifies nearby debt: "While you're in this module, here are two things you could clean up with minimal risk: [duplicate function at line 34, missing null check at line 67]."

The developer makes the small cleanup as part of the same PR. Debt reduction happens continuously rather than in big disruptive sprints.

Incremental Refactoring Strategy#

Large refactors fail. They take too long, touch too much, and accumulate risk. AI helps break debt reduction into safe, incremental steps.

For a major refactor (e.g., extracting a complex function):

  1. AI analyzes the current implementation and all call sites
  2. AI generates an incremental plan:
    • Step 1: Extract the pure logic into a new function (no behavior change)
    • Step 2: Add tests for the new function
    • Step 3: Update call sites one at a time
    • Step 4: Delete the old implementation
  3. Each step is a separate PR: small, reviewable, rollbackable

The refactor that would have been a 3-day branch becomes 4 small PRs over 2 weeks. Lower risk, easier to review, easier to roll back if something goes wrong.

Measuring Debt Reduction Progress#

Track debt health over time in DenchClaw:

-- Debt trends over time
SELECT 
  DATE_TRUNC('month', identified_date) as month,
  COUNT(CASE WHEN status = 'identified' THEN 1 END) as identified,
  COUNT(CASE WHEN status = 'resolved' THEN 1 END) as resolved,
  SUM(CASE WHEN status = 'resolved' THEN complexity_score ELSE 0 END) as complexity_resolved,
  COUNT(*) FILTER (WHERE status != 'resolved') as outstanding_debt_items
FROM v_technical_debt
GROUP BY 1
ORDER BY 1 DESC;

Is the outstanding debt count going up or down? Is the team resolving more debt than they're accumulating? These trends tell you whether your debt strategy is working.

Frequently Asked Questions#

How do you convince leadership to invest time in technical debt reduction?#

Quantify the cost of the debt. "This function has complexity 34 and takes an average of 3 hours to modify safely. We touch it 4 times per sprint. That's 12 hours of friction per sprint — $800/sprint at fully-loaded cost. The refactor would take 8 hours and eliminate that ongoing cost." Numbers make the case that arguments don't.

Should you stop feature development to pay down tech debt?#

Rarely. The "stop everything and refactor" approach usually fails because it delays value delivery too long. The better approach: allocate 15-20% of engineering capacity to debt reduction, implemented via the boy scout rule and incremental refactors alongside feature work.

How do you prioritize debt items when everything feels important?#

The risk-to-effort ratio (risk score / complexity score) is your primary sort key. High-risk, low-effort items first (e.g., add null checks to core functions). Low-risk, high-effort items last (e.g., rename variables throughout a large module for consistency).

What's the most dangerous type of technical debt to ignore?#

Security debt (known vulnerabilities) and test debt in critical code paths (billing, authentication, data deletion). These have direct, immediate impact. Other debt types accumulate velocity loss over time; these can cause immediate user harm.

Can AI write the actual refactor, not just identify it?#

Yes. Once a refactor plan is clear, AI can execute it: extract the function, update the call sites, add the tests, update the documentation. This is a good use of gstack's Build phase. Human reviews the plan; AI executes the mechanical work.

Ready to try DenchClaw? Install in one command: npx denchclaw. Full setup guide →

Mark Rachapoom

Written by

Mark Rachapoom

Building the future of AI CRM software.

Continue reading

DENCH

© 2026 DenchHQ · San Francisco, CA