poka-yoke
它能做什么
防错法(poka-yoke)是一种让流程或代码“不容易出错”的技术。它不依赖人去记住每一步都要做对,而是把系统设计成要么根本不可能出错,要么出错时能立刻被发现。
适用场景
- ✓当你想防止流程或代码中出现错误时。
- ✓当设计的系统一旦出错就可能造成严重后果(比如涉及金钱或重要数据)时。
- ✓在某个错误反复出现之后,想确保它不会再发生时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/poka-yoke/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样才能让这个注册表单防错,让用户无法提交不合规的信息?”
每周 5 个新技能,1 分钟读完。
工作原理
防错法的做法,是设计一些能阻止错误发生的“装置”。这可以是限定特定的数据类型,可以是一条规则(比如有问题时不允许继续),也可以是在出错的那一刻给出清晰的提醒。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Poka-Yoke: Mistake-Proofing for Software
Shigeo Shingo's insight, from the Toyota Production System: people will always make mistakes; that is not the problem worth solving. The problem is letting a mistake become a defect. So you stop trying to make humans more careful and start redesigning the work so the mistake either cannot physically happen or announces itself immediately.
A poka-yoke ("poh-kah yoh-kay", ポカヨケ) is a device: a jig, a shape, a counter: not an instruction. In software: a type, a constraint, a hook, a schema, a state machine. The single most important consequence:
A comment, a docstring, a wiki page, a code review checklist, or a line in CLAUDE.md saying "don't do X" is not a poka-yoke. It is training. Training degrades. A device does not. If your proposed fix relies on someone remembering something, keep going.
When to Use This Skill
- Use when the user says "poka-yoke this", "mistake-proof it", or "make this harder to get wrong".
- Use when designing an interface, schema or state machine and the ask is "make invalid states unrepresentable" or "so callers cannot screw it up".
- Use when auditing existing code for footguns: "what could bite us here", "what is easy to misuse".
- Use after an incident, when the fix must close the class rather than the case: "make sure this never happens again", "this is the third time".
- Especially for money, auth, permissions, deletion, migrations and pipelines, where failure is silent.
The two axes
Every real poka-yoke answers two questions. Use both when you classify a hazard or propose a device. They are the difference between this method and generic code review.
Axis 1, Regulatory function: what happens when the mistake occurs?
This is a strict preference ladder. Always reach for the highest rung you can afford.
| Rung | Name | What it does | Software examples |
|---|---|---|---|
| 1 | Control | The mistake is impossible. The work cannot proceed. | Type won't compile · NOT NULL / CHECK / unique constraint · required function argument · private constructor + smart constructor · PreToolUse hook returns deny · protected branch |
| 2 | Warning | The mistake is possible but announced at the moment it happens. | Lint error in the editor · failing CI gate · runtime assertion that throws · confirmation prompt naming the exact thing being destroyed |
| 3 | Detection | The mistake ships, and something finds it afterward. | Tests · monitoring · alerting · reconciliation job |
| 0 | (not a poka-yoke) | Relies on a human remembering. | Docs · comments · training · "be careful" · review checklists |
Shingo's rule: prefer control over warning, always, and only settle for warning when control is genuinely too expensive, then say why out loud. In software the honest reason is usually "the language can't express it" or "it would break every existing caller," and both are worth stating explicitly so the tradeoff is visible.
Axis 2, Setting function: how does the device notice?
Shingo's three detection methods map cleanly onto software. These are your inspection lenses, run all three over any interface and you will find hazards that a general code review misses.
| Method | Factory floor | The question to ask code | Software devices |
|---|---|---|---|
| Contact | The part physically won't seat unless it's the right shape and orientation | Can the wrong thing fit? | Distinct types instead of shared primitives · branded/newtype IDs · parse-don't-validate at boundaries · units in the type · discriminated unions instead of bags of optionals |
| Fixed-value | A counter says all 6 screws were fitted | Can the wrong count or an incomplete set pass? | Exhaustive match/switch over an enum · required fields · "all migrations applied" check · row-count guard on a bulk write · checksums · config validated as a whole at boot |
| Motion-step | A sensor confirms step 3 happened before step 4 | Can the steps happen in the wrong order, or be skipped? | Typestate · builder that cannot .build() until required steps run · state machines with illegal transitions unrepresentable · idempotency keys · RAII / defer / context managers · transactions |
The third principle: inspect at the source
Shingo separated source inspection from informative inspection, which finds the defect
…
常见问题
什么不算防错法?
代码里的注释、文档里的说明、检查清单,都不算防错法。这些东西都依赖人的记忆,很容易被忘记。真正的防错法是系统本身内置的规则或结构。
防错法有哪几个层级?
一共三个层级:控制(让错误不可能发生)、警告(在出错的当下发出提示)、检测(事后由某种机制找出错误)。理想情况下,应该尽量追求“控制”。
防错法如何识别错误?
主要有三种方法:接触型(错误的部件装不进去)、固定值型(数量或设定值不对)、动作步骤型(步骤顺序错误或被跳过)。
详情
作者的原始描述
Mistake-proof code, config and process: make the wrong action impossible or self-announcing rather than documented.
热度
- GitHub 星标
- 48k
- 排名
- #5461
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。