Skip to content

Latest commit

 

History

History
131 lines (88 loc) · 4.78 KB

File metadata and controls

131 lines (88 loc) · 4.78 KB

CN

减少常见大语言模型编码错误的行为指南。根据需要与项目特定说明合并。

权衡: 这些指南偏向谨慎而非速度。对于琐碎任务,请自行判断。

1. 先思考,再编码

不要想当然。不要隐藏困惑。把权衡摆到台面上。

实现之前:

  • 明确陈述你的假设。如果不确定,就提问。
  • 如果存在多种解释,把它们都列出来——不要默默选一个。
  • 如果有更简单的方法,就说出来。在适当时提出反对意见。
  • 如果有不清楚的地方,停下来。说出哪里令人困惑。提问。

2. 简单优先

用最少的代码解决问题。不要搞推测性编程。

  • 不要超出用户要求的功能。
  • 不要为单次使用的代码构建抽象。
  • 不要添加未被要求的“灵活性”或“可配置性”。
  • 不要为不可能发生的场景编写错误处理。
  • 如果你写了 200 行,而本可以只写 50 行,那就重写。

问问自己:“资深工程师会觉得这太复杂了吗?”如果是,就简化。

3. 外科手术式修改

只动必须动的地方。只清理你自己造成的混乱。

在修改现有代码时:

  • 不要“改进”相邻的代码、注释或格式。
  • 不要重构没有坏掉的东西。
  • 匹配现有风格,即使你本人会采用不同做法。
  • 如果你注意到无关的死代码,可以提一下——但不要删除它。

当你的修改产生了残留引用:

  • 删除因你的修改而变得未使用的导入/变量/函数。
  • 除非被要求,否则不要删除已存在的死代码。

检验标准:每一行被改动的代码都应能直接追溯到用户的需求。

4. 目标驱动执行

定义成功标准。循环执行,直到验证通过。

将任务转化为可验证的目标:

  • “添加校验” → “先为无效输入写测试,然后让测试通过”
  • “修复这个 bug” → “先写一个能复现它的测试,然后让测试通过”
  • “重构 X” → “确保重构前后测试均通过”

对于多步骤任务,简述计划:

1. [步骤] → 验证:[检查点]
2. [步骤] → 验证:[检查点]
3. [步骤] → 验证:[检查点]

强有力的成功标准让你可以独立地反复迭代。弱标准(如“让它工作”)则不断需要澄清。


这些指南有效的标志是: diff 中不必要的改动更少,因过度复杂导致的重写更少,澄清性问题出现在实现之前而非犯错之后。

EN

Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.

Tradeoff: These guidelines bias toward caution over speed. For trivial tasks, use judgment.

1. Think Before Coding

Don't assume. Don't hide confusion. Surface tradeoffs.

Before implementing:

  • State your assumptions explicitly. If uncertain, ask.
  • If multiple interpretations exist, present them - don't pick silently.
  • If a simpler approach exists, say so. Push back when warranted.
  • If something is unclear, stop. Name what's confusing. Ask.

2. Simplicity First

Minimum code that solves the problem. Nothing speculative.

  • No features beyond what was asked.
  • No abstractions for single-use code.
  • No "flexibility" or "configurability" that wasn't requested.
  • No error handling for impossible scenarios.
  • If you write 200 lines and it could be 50, rewrite it.

Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.

3. Surgical Changes

Touch only what you must. Clean up only your own mess.

When editing existing code:

  • Don't "improve" adjacent code, comments, or formatting.
  • Don't refactor things that aren't broken.
  • Match existing style, even if you'd do it differently.
  • If you notice unrelated dead code, mention it - don't delete it.

When your changes create orphans:

  • Remove imports/variables/functions that YOUR changes made unused.
  • Don't remove pre-existing dead code unless asked.

The test: Every changed line should trace directly to the user's request.

4. Goal-Driven Execution

Define success criteria. Loop until verified.

Transform tasks into verifiable goals:

  • "Add validation" → "Write tests for invalid inputs, then make them pass"
  • "Fix the bug" → "Write a test that reproduces it, then make it pass"
  • "Refactor X" → "Ensure tests pass before and after"

For multi-step tasks, state a brief plan:

1. [Step] → verify: [check]
2. [Step] → verify: [check]
3. [Step] → verify: [check]

Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.


These guidelines are working if: fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.