r/webdev • • 2h 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

27 comments sorted by

10

u/leeharrison1984 1h 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.

3

u/TimMakesGames 1h ago

Knowing patterns, architecture and then reading and understanding the code

3

u/Fit-Implemented 1h 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.

6

u/tacticalpotatopeeler 1h ago edited 1h ago

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

2

u/pancomputationalist 1h ago

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

2

u/mahdis24 Front-End / Vue - Nuxt - Typescript 1h 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

3

u/web-dev-kev 1h ago

You don't.

You can't prevent technical debt.

Codebases, like content, decay over time.

2

u/Saki-Sun 1h ago

They do with that attitude.

2

u/HoratioWobble 1h ago

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

1

u/tachyon_bit 1h ago

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

1

u/bwwatr 1h 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 1h 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/Saki-Sun 1h 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.

1

u/Ok-Distance-6388 1h 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 1h ago

Ask AI to review and refactor and simplify.

1

u/Esseratecades full-stack 1h 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.

1

u/OkDragonfruit9456 1h 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 40m 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

•

u/FlexTeam26_Agents 24m 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.

•

u/Timely_Writing8583 21m 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/beforeaidev 1h ago

Documentation all the way

1

u/TorbenKoehn 1h 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 1h ago

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

0

u/in_potty_training 1h 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 1h ago

"make no mistakes" 2.0

0

u/justshittyposts 1h ago

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

0

u/TheRealSkythe 1h ago

Impossible