How to Keep AI-Generated Software Code Clean

153.0K views
•
August 19, 2026
by
Matt Pocock
YouTube video player
How to Keep AI-Generated Software Code Clean

TL;DR

Coding agents work best when automated checks constrain their output and continuously remove defects and unnecessary complexity. Robert C. Martin uses coverage, complexity scoring, mutation testing, spot checks, and other tests to assess generated code, because accumulated mess eventually causes agents to slow down, break existing behavior, repeat failed fixes, or give up.

Transcript

I've got a treat for you today. I've got someone who I've been wanting to speak to for a while. Someone who has been really into um I mean software for as long as I've been a developer, much longer than I've been a developer. And someone who's now making his mark on the agent space. We have uh Uncle Bob Martin live and in his bathrobe ready to thro... Read More

Key Insights

  • Coding agents are fast, but their initial output can contain enough mess that cleaning it manually makes the human programmer slower overall. Martin's approach is to exploit their speed while requiring the agents themselves to perform repetitive quality checks and repairs after generating code.
  • The CRAP approach combines test coverage with the cyclomatic complexity of each function to produce a score indicating problematic code. Martin previously found the technique informative but impractical because examining and repairing every flagged function, along with rewriting tests, required too much time.
  • Mutation testing works by changing meaningful source-code operators, such as negative to positive, less than to greater than, or equal to not equal. The complete test suite runs after each mutation, and any alteration that does not cause failure is a surviving mutant requiring attention.
  • AI speed makes previously expensive analysis more practical because an agent can perform boring, repetitive work without objecting. Martin had agents run quality scoring, clean the resulting code, execute mutation tests, close test gaps, and repeat these activities as part of their development process.
  • Martin's goal is to avoid imposing human coding speed on agents when automated evidence can constrain their work. He aims to let agents handle code while he evaluates surrounding indicators, including quality scores, test results, occasional spot checks, and a broader collection of tests.
  • Clean code remains important for agents because accumulated disorder eventually reduces their ability to make safe changes. Martin observed agents slowing down, struggling with modifications, and entering cycles where fixing one problem inadvertently broke another part of the software.
  • Software mess compounds when new work proceeds without cleaning earlier output. Martin found that each requested change could leave additional clutter, eventually pushing an agent beyond the level of complexity it could manage effectively, even when the agent had initially worked quickly.
  • Automated constraints provide a path toward reduced human review, but Martin still uses verification rather than relying on speed alone. He checks whether CRAP scores remain low, performs occasional code spot checks, and runs additional tests to determine whether the generated software remains acceptable.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How can coding agents produce cleaner software?

Coding agents can produce cleaner software when they are required to evaluate and repair their own output with automated quality tools. Martin directs agents to run coverage and complexity analysis, clean problematic functions, perform mutation testing, add tests that kill surviving mutants, and repeat the process. He supplements these checks with low-score expectations, occasional code spot checks, and other tests.

Q: Why does clean code still matter when AI writes it?

Clean code matters because coding agents can become confused by accumulated disorder just as human programmers can. Martin observed agents slowing down, changing one element while accidentally breaking another, repairing the new failure while causing a different one, and eventually moving in circles. In at least one case, an agent effectively gave up because it could no longer manage the code.

Q: What is the CRAP score used for in software development?

The CRAP approach is used to identify functions that combine inadequate test coverage with excessive cyclomatic complexity. It measures those two properties and merges them through a formula into a score representing how problematic a function is. Martin uses the resulting scores as quality evidence and asks agents to clean their code when the analysis reveals unacceptable functions.

Q: How does mutation testing evaluate a test suite?

Mutation testing alters source code in small but meaningful ways, such as changing negative signs to positive signs, less-than comparisons to greater-than comparisons, or equality checks to inequality checks. The entire test suite runs after each alteration. A strong suite should fail, while a passing suite reveals a surviving mutant and therefore a behavior that existing tests may not adequately protect.

Q: Why did mutation testing once seem impractical?

Mutation testing seemed impractical because every source-code alteration required another complete execution of the test suite. In Martin's earlier experiment, the suite took four minutes and had to run perhaps several hundred times, so he left the process running overnight. Although it exposed surviving mutants that he could fix, it was not practical as part of a normal build scenario.

Q: How does AI make older software-quality techniques useful?

AI makes older quality techniques more useful by rapidly performing repetitive work that previously demanded too much human time or computing time within a normal workflow. Martin had agents run CRAP analysis, clean flagged code, execute mutation tests, repair weaknesses, and add missing tests. A mutation-testing task that once required an overnight run could take the agent about 30 minutes.

Q: Can automated checks replace detailed human code review?

Martin's stated goal is to build enough automated scaffolding that he does not need to inspect generated code in detail. He does not describe abandoning verification. Instead, he examines CRAP scores, expects them to stay low, conducts spot checks from time to time, and runs many other tests. The proposed replacement for constant review is measurable constraint, not unsupported trust.

Q: What happens when an agent keeps adding code without cleanup?

When an agent repeatedly adds features without cleaning earlier output, the mess compounds and eventually affects its performance. Martin saw agents become slower and less capable of managing changes. They began breaking unrelated behavior while fixing individual problems, then repairing those failures only to create others. The resulting cycle could make continued progress difficult or cause the agent to stop.

Summary & Key Takeaways

  • Robert C. Martin began programming at age 12 with a three-bit finite state machine and later studied programming languages through books before gaining regular programming work. After more than half a century as a programmer, he approached coding agents with extensive experience in software quality and the principles associated with clean code.

  • His first serious experiments with a coding agent were frustrating because it produced code quickly but repeatedly left messes that required cleanup. Martin realized that an agent's speed could make previously impractical quality techniques useful, especially when the work involved repetitive analysis, test execution, and correction rather than continuous human intervention.

  • Martin's goal is to surround coding agents with automated constraints strong enough that detailed human code review becomes less necessary. Complexity and coverage scores, mutation testing, spot checks, and additional tests provide evidence about quality. Cleanliness remains important because agents also lose effectiveness when code becomes sufficiently disorganized and difficult to change safely.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from Matt Pocock 📚