正在加载陈远的个人网站

codebase-design:用删除测试清理代码库冗余抽象

Matt Pocock 分享的 Agent 技能提示词,通过删除测试找出浅模块与冗余抽象,持续精简代码库。

原始内容

mattpocock/skills prompt of the day: /codebase-design Take a look in the repo for shallow modules, applying the deletion test and look for candidates for deletion. Helps kill unnecessary abstractions.

中文译文

机器翻译,保留原文供核对。

mattpocock/skills 今日提示词: /codebase-design 在仓库中查找浅模块,应用删除测试,并寻找可删除的候选对象。 有助于清除不必要的抽象。

深度解析

是什么:Matt Pocock 分享的"每日提示词",实为其 skills 仓库中的 /codebase-design 技能:让 Coding Agent 扫描代码库,寻找浅模块、执行删除测试并定位可删除的候选代码,目标是"杀掉不必要的抽象"。

核心设计:它把 John Ousterhout《A Philosophy of Software Design》中的"浅模块"概念(接口复杂但承载逻辑少)转化为 Agent 可执行的具体指令,并以斜杠命令封装为可复用技能,比"帮我 review 代码"这类泛化提示更可操作、输出更稳定。

关键洞察:删除测试(删掉它会有人在意吗)是极适合交给 AI 的机械化审计——Agent 擅长枚举抽象层、统计引用、评估复杂度收益比,而人类常因沉没成本不愿删代码。把清理动作封装成一条命令,大幅降低了持续简化的执行成本。

边界与风险:Agent 对"该不该删"的判断仍需人工复核——动态调用、反射、预留扩展都可能造成误删;"浅"与否依赖业务语境,不能只看接口与实现的比值。更适合作为重构前的线索生成器,而非自动执行删除的最终裁决者。

精选追评与案例

  1. 给出可直接复用的后续提示词,补强删除测试的可审查性。

    A follow-up prompt: “For each candidate, show two real call sites before and after removing it. List any behavior that would move into the callers.” That makes the deletion test easier to review. A wrapper can disappear while retries, validation or ordering rules get duplicated elsewhere

    中文译文 · 机器翻译

    一个后续提示词:“对于每个候选对象,在移除前后展示两个真实调用点。列出任何会被移入调用方的行为。” 这能让删除测试更容易审查。一个包装层可以消失,而重试、校验或排序规则却会在别处重复

  2. 指出还需同步清理仓库说明,否则抽象会被重新引入。

    @mattpocockuk I'd check the repo instructions in the same pass. If they still tell the agent to put every feature behind a service and a factory, the next feature request may bring those abstractions straight back.

    中文译文 · 机器翻译

    我会在同一轮里检查仓库说明。如果它们仍告诉代理把每个功能都放在一个服务和一个工厂后面,下一次功能请求可能会把这些抽象原样带回来。

  3. 提出更可验证的删除测试做法,并点出提示词需明确要求实操。

    @mattpocockuk Inlining the module and seeing whether its callers get simpler is the version of the deletion test I'd want an agent to run. Does the prompt ask it to actually try the inline?

    中文译文 · 机器翻译

    把模块内联,看看调用方是否变得更简单,这是我希望代理运行的删除测试版本。提示词是否要求它实际尝试内联?

  4. 补充删除前审查包装层隐藏行为的边界。

    @mattpocockuk A useful reminder that improving a codebase can mean removing things. Asking for candidates first also leaves room to check why a wrapper exists before deleting it, sometimes the awkward bit is protecting a behaviour that is easy to miss.

    中文译文 · 机器翻译

    一个有用的提醒:改进代码库也可能意味着移除东西。先要求给出候选对象,也为在删除前检查某个包装层为何存在留出空间;有时那个别扭的部分正是在保护一种容易被忽略的行为。

  5. 提醒删除测试必须覆盖包装层原本处理的副作用。

    @mattpocockuk The deletion test is a solid way to prune dead weight, just ensure you catch any side effects the wrapper was handling

    中文译文 · 机器翻译

    删除测试是修剪冗余负担的可靠方法,只要确保你能捕捉到包装层原本处理的所有副作用。