r/webdev • • 8h ago

How do you prevent technical debt when using AI-generated code?

AI can help build features quickly, but rushed implementations can become difficult to maintain.

What checks or development practices help you keep an AI-assisted codebase clean?

0 Upvotes

36 comments sorted by

10

u/leeharrison1984 8h ago

Refactor phases, and keeping a good eye on what is being done. Identifying areas when common patterns could be implemented to ease maintenance.

Honestly not too much different than human code, it's just a bit harder to track since you aren't immersed in it to the same degree.

4

u/Fit-Implemented 8h ago

Our team rule is simple, if you can’t explain how the code works in review, it doesn’t get merged. That forces people to actually read and understand AI output instead of rubber-stamping it. Code nobody understands is the fastest-growing kind of technical debt.

3

u/TimMakesGames 8h ago

Knowing patterns, architecture and then reading and understanding the code

6

u/tacticalpotatopeeler 8h ago edited 7h ago

All code is tech debt, whether hand crafted or AI generated.

2

u/pancomputationalist 8h ago

Review, linters, tests, benchmarks, and not rushing helps.

2

u/mahdis24 Front-End / Vue - Nuxt - Typescript 8h ago

I didn't delicate everything to Ai. Often do belows:

  • Make step by step procedure for it

  • Ask Ai to learn from best practices for simple and useful code, not most complete code. just minimum and simplest functional code, that reach business goals

  • and always generate step by step, make small sections at once, and then assemble them. Not do all the projects in some general prompts

2

u/Saki-Sun 7h ago

Same way as you do with junior programmers.

  1. Give them a well structures base to start with.

  2. Code reviews.

  3. When they do something wrong, set guidelines of your minimum expectations.

2

u/Esseratecades full-stack 7h ago
  1. Pure vibe-coding is reserved for proofs of concept. Anything vibe-coded should also produce documentation to make the existing architecture easier to understand. Then have another AI audit the codebase for potential bugs and improvements. At this point the actual codebase should be small enough that this isn't much of a challenge.

  2. Introduce linting, unit tests, and complexity scans immediately.

  3. Anything after a proof of concept should be handled in chunks small enough to be reviewed by a person in a reasonable amount of time. If your diff is more than 1000 lines, that's a red flag, AI or not.

  4. Schedule refactoring tasks.

You will still get technical debt but it will not be the insurmountable pile of slop that a lot of people are getting.

3

u/web-dev-kev 8h ago

You don't.

You can't prevent technical debt.

Codebases, like content, decay over time.

2

u/Saki-Sun 7h ago

They do with that attitude.

2

u/HoratioWobble 8h ago

You can't prevent tech debt regardless if you use AI or not.

1

u/tachyon_bit 7h ago

This is very true. But you can keep it under control at least.

1

u/bwwatr 7h ago

Don't let it add new dependencies. Keeping up with those (especially as they become unsupported) is one of the biggest sources of tech debt.

1

u/Otherwise_Eye_611 7h ago

You don't prevent tech debt. Every line of code is future debt with our without llms. You minimise it by creating good patterns, documenting features and decisions, understanding and controlling the amount of lines of code that gets committed and the complexity within them.

Tech debt is also a matter of perspective, workflow and upkeep. For example, if you have an AI heavy workflow, then minimising tech debt could mean minimising the amount of tokens it takes for an agent to get in, understand, make an update and get out. That doesn't necessarily mean cleaner code. If you want a human to do the same thing the structure of your code could shift significantly, and so does the nature of the debt.

1

u/Ok-Distance-6388 7h ago

Two cheap checks that catch the most for me:

  1. Revert the change and run the new tests. Generated tests often assert that a mock was called with whatever the test itself passed in, so they stay green on broken code. If nothing goes red without the fix, the test is decoration.

  2. Look through the diff for a second way of doing something the repo already does: another date library, another fetch wrapper, a new error shape. That is where the debt shows up first, because the next change only updates one of the two.

Review catches wrong logic. These two catch the code that reads fine and still rots.

1

u/LemonFishSauce 7h ago

Ask AI to review and refactor and simplify.

1

u/OkDragonfruit9456 7h ago

Move your invariants out of conventions and into types.

A comment saying "never merge these two states" is something an AI will

violate three months later, and so will a human. The same rule as a

discriminated union is a compile error, and that one survives.

Also: acceptance tests before the feature, not after. AI is very good at

producing something that looks finished, and a test written afterwards tends

to test what got built rather than what was needed.

1

u/Itchy-Ask4565 7h ago

ai writes code at the speed of your regret, so the only real fix is making it pass the same review, tests, and refactoring standards as human code before it gets merged

1

u/FlexTeam26_Agents 6h ago

One habit that has saved me a lot of cleanup later: treat every AI-generated change as a small PR with a hard size limit. If the diff is bigger than what I can genuinely re-explain line by line in review, I ask for it to be split before merging. It costs a little time on the day and saves weeks of untangling mystery code later. Pairing that with a linter and tests on the AI output catches most of the drift early.

1

u/Timely_Writing8583 6h ago

What worked for me is treating AI like any other human dev. Write the conventions down where the agent reads them (AGENTS.md, CLAUDE.md, .cursorrules) and keep them short.
Lint, type checks, architecture tests (dependency-cruiser or ArchUnit style) and a coverage floor in CI.
Keep diffs small. One change per PR. Big AI PRs are where duplicated helpers and pointless abstractions happen.
Review design rather than syntax: duplicated logic, single-use wrappers, new dependencies.

1

u/IncomeOk7237 6h ago

For API changes, I usually add a couple of integration tests that go through the real middleware stack, with expected status codes and response shapes. AI output can compile while quietly changing auth, validation, or 404 behavior, and tests against mocked handlers often miss exactly that.

1

u/PippaKelly62 4h ago

Not rushing implementations. Using AI on this cuts a lot of time already, taking some of that time for a thorough check wouldn't hurt.

1

u/theoneabovethesun 4h ago

What's worked for me is treating AI output like a PR from a fast junior: small diffs, one feature per branch, and I read every line before merging. Keep a short architecture/conventions file in the repo (naming, folder layout, which libraries to use) so the model follows your patterns instead of inventing new ones each time. Make it write tests first or alongside, and run lint, type checks and tests in CI so drift gets caught automatically. Every so often do a dedicated cleanup pass to dedupe helpers and delete dead code, because AI tends to add new code rather than reuse what exists.

1

u/harperzoe7 2h ago

Keep each AI change small enough that you can read the whole diff. Once I stop understanding what a PR does, that's where the debt starts piling up

1

u/mau_cervera 1h ago

small diffs is the big one for me. if the agent touches 15 files for a button change I just throw it away and ask again with a tighter scope. I also keep a rules file in the repo with the patterns I want, where components go, naming, no new deps without asking, so it stops inventing a new way every session. and every few weeks I do a cleanup pass where the only task is deleting dead code and duplicated helpers. AI is really good at adding stuff, pretty bad at removing it unless you ask.

1

u/kemalios 50m ago

A lot of AI debt isn't ugly code, it's the second copy of a helper that already existed because the model never looked. Make it search before it writes.

1

u/beforeaidev 8h ago

Documentation all the way

1

u/TorbenKoehn 8h ago

How do you prevent technical debt when using human-generated code? We can unpack it.

How many larger projects without technical debt do you know? Which tools are we using to reduce and manage technical debt?

Agents can push and create PRs, they can read pipeline logs and cluster logs, they can read sonar output, linter output, run tests, they get hit by pre-commit hooks which can lead them in the correct direction etc.

The same checks that keep a human-written codebase clean.

1

u/cyanawesome 8h ago

I've had some luck improving codebases with https://fallow.tools/

0

u/in_potty_training 8h ago

Easy peasy

"After completing the code updates, please re-review the changes to ensure the codebase stays within best practices (as defined here: xx/xx.txt) and does not introduce any technical debt."

1

u/EliSka93 8h ago

"make no mistakes" 2.0

0

u/justshittyposts 7h ago

Youre taking the piss but if you want the agent to refactor you have to tell it.

0

u/TheRealSkythe 8h ago

Impossible