Harness 与 Agent 工程 · AI研发与测试工程 · 2025-12-01

AI正在动摇我曾推崇的最佳实践

AI正在改变我们习以为常的编程实践,让曾经被认为"性价比不高"的工作变得简单高效。

核心内容:

  1. 测试覆盖率追求的反思与AI带来的新可能
  2. 代码注释观念的转变与AI辅助实践
  3. 通过Cursor工具实现自动化文档维护的案例

我越来越感受到一些我曾经恪守的编程原则,以及一些我们推崇的编程实践正在被AI动摇。先先说两件小事。

第一件小事:几年前我写过一篇名为《测试覆盖率治不好你的精神内耗》的文章,想表达是盲目的追求高测试覆盖率并不明智。例如在JavaScript中使用zustand框架创建store的声明基本上都遵循如下模式:

export const useUIStore = create(persist((set) => ({ property: false, setPropertyValue: (value) => set({property: value }),})
这种模式在我的项目中重复了11次,为了追求测试覆盖率,相似的测试代码也不得不重复在测试文件中出现了11次,这完全是没有必要的。除了冗余代码之外,测试代码的性价比也是另一个问题,在提升测试覆盖率的过程中,绝大部分的新增测试代码其实是用于覆盖边缘场景,然而使用实际使用产品的过中有多大概率会触发这类边缘场景呢?成倍投入时间的结果真的是bug在成倍减少吗?

第二件小事:在《整洁架构》一书中,鲍勃马丁对注释的态度算不上欢迎。注释本身就代表着一类“失败”——“噢,也许这段代码过于复杂,我应该用注释解释一下”——通常这只会让情况更糟,要知道写出好的文字说明不比写出好的代码简单,人们总是懒得在编写注释上投入时间,注释也总是在代码更迭中被疏于维护。在这些条件下被糟糕“照料”的注释所产生的负面效果远比正面效应要大,它们会给人带来误导,让人费解——最好的注释其实是代码本身:带有少量注释的整洁而有表达力的代码,要比带有大量注释的零碎而复杂的代码像样的多。所以一般在编码的过程中,我们其实并不鼓励给代码添加注释,优先把代码写好就足够了。

说到底,驱动我们给出如上结论的动力不是我们不具备这种能力,而是我们觉得把它们做好的性价比不高。如维护文档、写端到端测试、补全注释等等,没有人能算得清我们需要投入多少而它们又能换回来多少回报。团队不愿意做看不到目标而又遥遥无期的事情,程序员本身也反感这类琐碎而又没有挑战工作。

而现在AI可以帮助我们完成这些琐碎的工作,这也是最近我在维护的我的个人AI工具时所尝试的实践,它们简单有效。

多快好省的Agent

以Cursor为例,迫使AI能够实时更新文档最简单粗暴的方式其实是通过Cursor Rules。Cursor rules是cursor编辑器的自有功能,它允许我们为项目定制代码的生成规则或者应该遵守的规范,Cursor rule通常定义在以.mdc为扩展的文件中,并保存在项目根目录的.cursor/rules 目录下,例如:

---description: React Components Rulesglobs: *.jsxalwaysApply: true---- When you completed coding the component, generate a document file to describe the usage of the component

但在实际编码的过程中,在每个里程碑时刻(例如在GitHub上创建release,合并pull request等)再重新为组件生成或者修改文档其实更为实际,因为此时的组件通常处于完整和稳定的状态。在我的个人项目(项目地址请访问原文中的链接)中,我创建了一个用户在PR合并之后会被触发的流水线(GitHub Action),每当PR被合并之后流水线即被触发,其中的Cursor Agent便开始创建与该分支有关的文档。该流水线运行分为两类步骤,首先安装Git、Cursor CLI以及GitHub CLI:


- name: Install Cursor CLI run: | curl https://cursor.com/install -fsS | bash echo "$HOME/.cursor/bin" >> $GITHUB_PATH- name: Configure git run: | git config user.name "Cursor Agent" git config user.email "cursoragent@cursor.com"- name: Authenticate GitHub CLI run: | echo "$" | gh auth login --with-token gh auth status最后让cursor agent执行prompt- name: Update docs env: MODEL: gpt-5 CURSOR_API_KEY: $ GH_TOKEN: $ BRANCH_PREFIX: docs run: | cursor-agent -p "You are operating in a GitHub Actions runner. The GitHub CLI is available as `gh` and authenticated via `GH_TOKEN` environment variable. Git is available. You have write access to repository contents and can comment on pull requests, you can also create or edit PRs. # Context: - Repo: $ - Owner: $ - PR Number: $ - Base Ref: $ - Head Ref: $ - Docs Branch Prefix: $ # Goal: - Implement an end-to-end docs update flow driven by incremental changes to the original PR. # Requirements: 1) Determine what changed in the original PR and, if there have been multiple pushes, compute the incremental diffs since the last successful docs update. 2) Update only the relevant docs based on those incremental changes. 3) Maintain the persistent docs branch for this PR head using the Docs Branch Prefix from Context. Create it if missing, update it otherwise, and push changes to origin. 4) If you need to create a new doc, you must follow the same pattern as the existing docs. # Inputs and conventions: - Use `gh pr diff` and git history to detect changes and derive incremental ranges since the last docs update. - Keep changes minimal and consistent with repo style. If no doc updates are necessary, make no changes and post no comment. # Deliverables when updates occur: - Pushed commits to the persistent docs branch for this PR head. - A single natural-language PR comment on the original PR that includes the inline compare link above. Avoid posting duplicates; update a previous bot comment if present. - Create a draft PR from the persistent docs br…
— 以上为试看内容(约 40%),开通会员后阅读全文 —

原文出处:https://www.53ai.com/news/gerentixiao/2025120130791.html