逻辑原型
Logic Prototype
Section titled “Logic Prototype”一个独立的单文件 HTML(可分享的演示),任何人都能通过点击按钮来操作状态模型。当问题涉及业务逻辑、状态转换或数据结构时,就用它:这类问题在纸面上看起来合理,只有用真实案例跑一遍才会觉得不对劲。
因为它只有一个文件,不需要安装任何东西,你可以把它交给非开发者(设计师、产品经理、领域专家),让他们亲自感受这个模型。所以演示要用他们的语言,而不是代码的语言。
适合使用的情况
Section titled “适合使用的情况”- “我不确定这个状态机能不能处理先发生 X、再发生 Y 这种边界情况。”
- “这个数据模型真的能表示……这种情况吗?”
- “我想在动笔写代码之前,先试试 API 应该是什么样子。”
- 任何有人想要按下按钮并观察状态变化的情况。
如果问题是“界面应该是什么样子”,那就不该走这条路。请用 UI.md。
1. 明确问题
Section titled “1. 明确问题”写代码前,先记下你要原型化的状态模型和问题。用一段话写在演示顶部的可见介绍里,不要只写成注释。回答错问题的逻辑 prototype 毫无价值,所以要明确写出问题,方便之后检查,无论用户此时正在查看,还是暂时离开后再回来。
2. 把逻辑隔离到可移植模块中
Section titled “2. 把逻辑隔离到可移植模块中”把真正回答问题的逻辑放在一个 <script> 块里,写成小型纯模块,以后可以单独取出并放进真实代码库。周围的页面可以丢弃,这个模块不能丢。
合适的形式取决于问题:
- 纯 reducer:
(state, action) => state。适用于 action 是离散事件、state 是单个值的情况。 - 状态机:显式定义状态和转换。适用于“当前允许执行哪些 action”本身就是问题的一部分。
- 一小组纯函数:作用于简单数据类型。适用于没有隐式当前状态、只有数据转换的情况。
- 类或模块,带有清晰的方法接口:适用于逻辑确实需要维护持续存在的内部状态。
选择最符合问题的形式,不要只选最容易接到页面上的形式。保持纯粹:不要操作 DOM,不要使用 document,也不要让按钮处理函数直接访问模块内部。页面调用模块,数据不要反向流动。这样原型在完成使命后仍然有用:问题得到回答后,经过验证的 reducer、状态机或函数集可以直接迁移到真实模块中。
3. 构建可分享的 HTML 文件
Section titled “3. 构建可分享的 HTML 文件”只用一个文件,采用原生 HTML/CSS/JS:不使用框架、打包工具或服务器。所有内容都写在文件里,双击就能打开,也方便通过邮件分享。任何人打开文件就能运行。
面向非开发者来写。所有标签都用领域语言,不要用代码术语:按钮和状态要符合业务表达,而不是 reducer 的表达。用通俗的话解释正在发生什么。
按清晰的层级从上到下布局:
- 标题和一句话说明,介绍这个演示能让人探索什么(也就是第 1 步中的问题)。
- 当前状态:用易读的面板展示完整的相关状态(使用带标签的字段,不要直接输出 JSON),每次点击后都重新渲染,让变化清晰可见。适合帮助非开发者理解时,指出刚刚发生了什么变化。
- 自由探索按钮:每个 action 都对应一个始终可用的按钮,让任何人都能按任意顺序试用模型。每次点击都会分派对应的 action 并重新渲染状态。
- 引导式演练:设置一组场景,每个场景对应一个标签页。每个标签页都要简短、通俗地描述场景(要设置的情境以及需要留意的地方),下方列出该场景中要按的按钮,顺序排列。每一步都是一个真实按钮:点击后执行对应 action,并进入下一步。开始演练时,重置到已知的初始状态,让场景每次都以相同方式运行。
选择能展示棘手情况的场景,也就是那些难以只靠纸面推理的情况:正常流程、复杂的边界情况,以及尝试执行不应允许的操作。
外观精致但克制:排版清晰、留白充足,只用一种强调色。不要动画或花哨效果,避免让状态和按钮分心。
4. 交付给对方
Section titled “4. 交付给对方”把文件发给他们,或直接为他们打开。他们会在方便时点击演练和自由探索按钮。最有意思的时刻,是他们说“等等,这不应该能做到”或“咦,我以为 X 会不一样”的时候;这些暴露的是想法本身的 bug,而这正是重点。如果他们想增加 action 或新场景,就加上。原型会不断演进。
5. 记录答案和原型
Section titled “5. 记录答案和原型”原型回答问题后,先记录答案,再按照 SKILL 中的说明记录原型。逻辑方面的对应方式是:经过验证的 reducer、状态机或函数集迁移到真实模块中(决策随之落实);HTML 外壳则放在用于保留原型这一主要信息源的临时分支中。因为它是独立的单文件,在那里也能轻松重新运行。
- 不要添加测试。 一个需要测试的原型就不再是原型了。
- 不要接入真实数据库。 除非问题明确涉及持久化,否则使用内存状态。
- 不要泛化。 不要考虑“以后如果要支持 X 怎么办”。原型只回答一个问题。
- 不要把逻辑和页面混在一起。 如果纯模块引用了 DOM、
document或按钮处理函数,它就无法再被单独迁移。让页面保持为纯模块外的一层薄壳。 - 不要使用框架、打包工具或服务器。 接收者应该能双击打开单文件;React 应用或开发服务器都会破坏“可分享”这一点。
- 不要把 HTML 外壳部署到生产环境。 这个页面是为手动点击体验优化的。值得保留的是它背后的逻辑模块。
原文链接和 commit
Section titled “原文链接和 commit”英文 LOGIC.md
# Logic Prototype
A single, self-contained HTML file (a **shareable demo**) that lets anyone drive a state model by clicking buttons. Use this when the question is about **business logic, state transitions, or data shape**: the kind of thing that looks reasonable on paper but only feels wrong once you push it through real cases.
Because it's one file with nothing to install, you can hand it to a non-developer (a designer, a PM, a domain expert) and let them feel the model for themselves. So it speaks their language, not the code's.
## When this is the right shape
- "I'm not sure if this state machine handles the edge case where X then Y."- "Does this data model actually let me represent the case where..."- "I want to feel out what the API should look like before writing it."- Anything where someone wants to **press buttons and watch state change**.
If the question is "what should this look like," this is the wrong branch. Use [UI.md](UI.md).
## Process
### 1. State the question
Before writing code, write down what state model and what question you're prototyping. One paragraph, at the top of the demo (in a visible intro, not just a comment). A logic prototype that answers the wrong question is pure waste, so make the question explicit so it can be checked later, whether the user is watching now or returning to it AFK.
### 2. Isolate the logic in a portable module
Put the actual logic (the bit that's answering the question) in a single `<script>` block written as a small, pure module that could be lifted out and dropped into the real codebase later. The page around it is throwaway; this module isn't.
The right shape depends on the question:
- **A pure reducer**: `(state, action) => state`. Good when actions are discrete events and state is a single value.- **A state machine**: explicit states and transitions. Good when "which actions are even legal right now" is part of the question.- **A small set of pure functions** over a plain data type. Good when there's no implicit current state, just transformations.- **A class or module with a clear method surface** when the logic genuinely owns ongoing internal state.
Pick whichever shape best fits the question being asked, *not* whichever is easiest to wire to a page. Keep it pure: no DOM, no `document`, no button handlers reaching inside it. The page calls into it; nothing flows the other direction. This is what makes the prototype useful past its own lifetime: once the question's answered, the validated reducer / machine / function set lifts into the real module on its own.
### 3. Build the shareable HTML file
One file, plain HTML/CSS/JS: no framework, no bundler, no server, everything inline so it opens by double-click and survives being emailed around. Anyone should be able to run it by opening it.
Write it for a non-developer. Every label is in **domain language**, not code: buttons and state read like the business, not the reducer. Explain in plain words what's happening.
Lay it out with a clean hierarchy, top to bottom:
1. **Title and one-line explanation** of what this demo lets you explore (the question from step 1).2. **Current state**: the full relevant state, rendered as a readable panel (labelled fields, not a raw JSON dump), re-rendered after every click so the change is visible. Where it helps a non-developer follow, call out what just changed.3. **Free-play buttons**: one button per action, always available, so anyone can poke at the model in any order. Each click dispatches its action and re-renders the state.4. **Guided walkthroughs**: a set of **scenarios**, one per tab. Each tab holds a short plain-language description of the scenario (the situation it sets up and what to watch for) and underneath it, the ordered **buttons to press** for that scenario. Each step is a real button: clicking it performs that action and moves to the next step. Starting a walkthrough resets to a known initial state so the scenario runs the same way every time.
Choose scenarios that demonstrate the awkward cases, the ones hard to reason about on paper: the happy path, a tricky edge case, an attempt at something that should be illegal.
Keep it beautiful but restrained: clean typography, generous spacing, one accent colour. No animations, no gimmicks: nothing that competes with the state and the buttons.
### 4. Hand it over
Send them the file, or open it for them. They'll click through the walkthroughs and free-play whenever they get to it; the interesting moments are when they say "wait, that shouldn't be possible" or "huh, I assumed X would be different"; those are the bugs in the _idea_, which is the whole point. If they want new actions or a new scenario, add them. Prototypes evolve.
### 5. Capture the answer and the prototype
Once the prototype has answered its question, capture the answer, then capture the prototype the way the [SKILL](SKILL.md) describes. The logic-specific mapping: the validated reducer / machine / function set lifts into the real module (the decision, absorbed); the HTML shell rides along to the throwaway branch that keeps the prototype as a primary source, and being one self-contained file, it stays trivially re-runnable there.
## Anti-patterns
- **Don't add tests.** A prototype that needs tests is no longer a prototype.- **Don't wire it to the real database.** Use in-memory state unless the question is specifically about persistence.- **Don't generalise.** No "what if we wanted to support X later." The prototype answers one question.- **Don't blur the logic and the page together.** If the pure module references the DOM, `document`, or button handlers, it's no longer liftable. Keep the page as a thin shell over a pure module.- **Don't reach for a framework, bundler, or server.** One file the recipient double-clicks; a React app or a dev server defeats "shareable".- **Don't ship the HTML shell into production.** The page is optimised for being clicked through by hand. The logic module behind it is the bit worth keeping.非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。
技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock