编码代理时代的代码整洁性
Clean code in the age of coding agents

原始链接: https://www.yanist.com/clean-code-in-the-age-of-coding-agents/

## 清洁代码很重要——即使在人工智能时代 清洁代码不仅仅是为了人类开发者;它也显著影响编码代理的效率。代码的*价值*(功能)显而易见,但其*结构*(组织方式)往往被忽视,导致长期成本,如更高的错误率和更慢的功能开发速度。清洁代码的关键特征包括可读性、简洁性、模块化和可测试性——所有这些都使其更易于修改。 尽管大型语言模型(LLM)正在兴起,但清洁代码仍然至关重要。当前的人工智能代理受到“上下文窗口”大小的限制——本质上是它们的工作记忆。结构糟糕的代码迫使代理处理更多文件,超出这些限制并增加成本。组织良好的代码允许代理专注于相关部分,就像对人类开发者一样。 为了有效利用人工智能,优先考虑代码组织*并*明确指示代理所需的结构。始终审查代理生成的更改,因为除非提示,否则它们不会优先考虑良好的结构。现在投资于清洁代码可以节省时间、金钱和认知负荷——对于人类和机器而言。

## 人工智能代理时代的代码整洁度:摘要 一篇Hacker News讨论探讨了随着人工智能编码代理的兴起,“代码整洁”原则是否仍然重要。 结论是细致的。 一些人认为代码整洁度变得不那么重要了——认为重写更容易,并且“无限”的资源降低了对优化的需求——但许多人强调其持续的重要性。 主要观点包括:LLM擅长构建现有模式,但难以处理复杂、混乱的代码和边缘情况。 “垃圾进,垃圾出”仍然适用;结构不良的代码会导致进一步恶化。 清晰的架构和可维护性对于快速修复错误和长期系统健康至关重要。 一些评论员建议关注*如何*指导人工智能,为“完美”代码提供明确的指导,而不是依赖现有且可能不一致的代码库。 另一些人则提倡诸如SOLID和DRY之类的既定工程原则。 最终,争论的中心在于人工智能是否会放大现有的代码质量(或缺乏质量),以及快速迭代的好处是否超过技术债务的风险。
相关文章

原文

TL;DR: Keeping code clean helps both humans and agents.

I was thinking the other day how coding agents despite being very different from human developers, still share some similarities - their productivity is affected by the state of the codebase.

Robert Martin in Clean Architecture talks about code as having two properties: value (it works, it's fast, etc.) and structure (how code is organised).

While value is obvious for all the stakeholders, the structure is somewhat less clear.

Poor structure means it's harder to introduce new features, and bugs are going to multiply. Eventually it costs your company momentum and money. It's just not immediate. It's a long-term thing.

What makes clean code?

It has several characteristics (but also note how all of those are affected by each other).

  1. Readability: any other developer can easily understand what's going on.

  2. Simplicity: it gets the job done as simply as possible (but not simpler).

  3. Modularity: sensible code splitting into modules whatever those might be (classes, functions, files, directories, or micro-services).

  4. Testability: writing tests is easy and fun.

Naturally, code is easy to change if it has these characteristics.

Clean code and LLMs

But hey, these days we don't need to care about the structure anymore, right? We have LLMs to do the work for us. We don't even need to read the code anymore - we can focus on testing the outcomes. We don't read the compiled binary code after all, do we?

Well, no.

Coding agents are limited by context (excuse me, dear future reader, I'm writing this from April 2026). Longer contexts mean degraded performance, the tokens will multiply - and so will the cost.

Context is basically how many things a machine can keep in its operational memory - it's not so different from the very human cognitive load.

As with humans, poorly organized code means agents need to read, "understand", and make changes to more files than necessary - polluting their context and costing you tokens.

If your code is properly organized, implementing a feature might require a model to read just a couple of small files, and make changes, instead of having to read dozens going around in circles.

Just like you and me.

So what are some practical steps?

I don't really have a silver bullet, but here are some things from my experience.

  1. When you give a task to your agent, make sure you also explain how the code should be organized. Not only value, but also structure.

  2. LLMs are pretty good at picking up the style in your repo. So keeping it clean and organized already helps.

  3. Review the outcomes. I don't think we can skip this step for now. In my opinion, an agent does not care about the structure, unless you specifically ask it to. But even in this case you have to review the changes.

#article #programming #llms #public

联系我们 contact @ memedata.com