Split a 200-Line Function into Testable Steps
Added 2026-10-28
Split a 200-Line Function into Testable Steps — Chinese original prompt
你是一名做过多次大重构的资深工程师。我有个两百多行的长函数,没人敢动,请帮我把它拆成可测试的小步骤。 [函数代码]:完整粘贴,包含它依赖的全局变量 [调用方]:谁在什么场景调用它 [现有测试]:有几个、覆盖了什么 [不能改变的行为]:对外返回值、日志格式、调用时序 输出结构: 1. 行为快照:输入到输出,以及所有副作用(写库、日志、发请求、读时间、随机数) 2. 拆分方案:每一步的职责、入参、返回、可单独测试的点,最多 5 步 3. 迁移顺序:每步都能独立提交并回滚,标明哪步风险最高 4. 需要新增的最小测试用例:每条写清输入与断言 验收标准:拆分后对外行为完全不变;每步可脱离上下文单独调用;不引入新框架或新依赖;涉及副作用的步骤必须显式传参而不是读全局。无法确认的行为单独列出,不要替我决定。
Site original
Chinese original
Replace these placeholders 4
Swap the bracketed parts for your own details before sending.
[函数代码][调用方][现有测试][不能改变的行为]
How to use
没有现成测试时别直接重构,先让它生成行为快照用例,否则拆完很难证明行为没变。
Try tweaking it
- Have it restate requirements and constraints before writing code.
- Ask for the smallest change plus a rollback path, not a full-file rewrite.
What you should get
- A minimal, runnable change with copy-ready code
- Impact list: which files and call sites must follow
Common mistakes
- Asking it to touch ten files at once, then losing track of what broke what.
- Pasting only the error, with no code or version, and getting an unfixable answer.
How to check the output
- The change runs, can be rolled back, and touches nothing unrelated.
- Edge cases (empty, oversized, concurrent) are covered or explained.
Where it works
- Good for: having it restate requirements and constraints, then propose the smallest runnable change.
- Watch out: include versions and a minimal reproduction with the error, or the fix may look plausible but not fit.