Study: Code Cleanliness Impacts AI Coding Agents

New minimal-pair research shows cleaner codebases improve autonomous coding agents' performance over those with static analysis violations.

Study: Code Cleanliness Impacts AI Coding Agents

Research Design and Findings

Researchers Priyansh Trivedi and Olivier Schmitt from SonarSource released a paper on May 19, 2026, examining whether code cleanliness changes how coding agents perform. They built minimal-pair repositories that shared the same architecture, dependencies, and external behavior but differed in static-analysis violations and cognitive complexity. One version in each pair was cleaned or degraded through automated pipelines. The team created 33 tasks across six pairs and ran 660 trials using Claude Code, measuring results against hidden tests at the public API surface. Pass rates stayed identical regardless of cleanliness level.

Code Cleanliness Effects on Coding Agents

Clean code produced measurable differences in how the agent operated. Token consumption dropped 7 to 8 percent on the cleaner variants. File revisitations fell by 34 percent. These reductions occurred because the agent encountered fewer rule violations and lower cognitive complexity, which limited the need to backtrack or re-examine sections during edits. The study isolated cleanliness from model capability by holding the agent and harness fixed across pairs.

The unchanged pass rate shows that task completion alone does not capture all relevant behavior. Agents still reached the same functional outcomes, yet they followed shorter paths on clean code. This pattern held across both directions of the minimal pairs: degrading a clean repository increased footprint, and cleaning a messy one reduced it.

Implications for Existing Codebases

Teams that integrate coding agents into daily work now face a direct cost variable tied to maintainability metrics. Lower token counts translate to reduced API spend when running agents at scale. Fewer file revisits shorten session duration and decrease the chance of introducing secondary bugs during navigation. Static-analysis tools already flag the same violations the study used, so existing linters and complexity checks can serve as proxies for expected agent efficiency.

Cognitive complexity scores proved more predictive than raw line counts or dependency graphs in the reported trials. Repositories that kept functions under common thresholds required less re-reading by the agent. This aligns with how current agent harnesses parse context windows: shorter, flatter call graphs fit more relevant code in a single pass.

Practical Steps for Maintainers

Run the same static-analysis suite used in the paper before feeding a repository to an agent. Target the rule categories that produced the largest footprint deltas, such as duplicated logic and high cyclomatic branches. Refactor only those hotspots rather than attempting full repository rewrites. Measure token usage and edit traces on a small task set before and after changes to confirm the expected savings.

For new projects, enforce the same thresholds in CI so the baseline stays consistent. Agents will inherit the efficiency gains without additional prompting changes. The study positions cleanliness alongside model selection and prompt design as a controllable factor in agent performance.

---

📖 Related articles

Need a consultation?

I help companies and startups build software, automate workflows, and integrate AI. Let's talk.

Get in touch
← Back to blog