principle-boundary-discipline

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Boundary Discipline

边界纪律

Place validation, type narrowing, and error handling at system boundaries. Trust internal code unconditionally. Business logic lives in pure functions. The shell is thin and mechanical.
Why: Scattered validation is noisy, redundant, and gives a false sense of safety. Keep logic out of framework wiring so it can be tested without the framework.
The pattern:
  • At boundaries (CLI args, config files, external APIs, network protocols): validate, return errors, handle defensively.
  • Inside the system: typed data, error propagation, no re-validation. Trust the types.
  • Across the boundary. Expose domain concepts, not the boundary's private representation. Keep general-purpose mechanism inside and special-purpose policy at the edge.
Applications:
Validation and error handling:
  • Validate config at parse time (the boundary), not inside business logic
  • Parse raw data into domain types at the boundary
  • Do not re-export transport, storage, framework, or wire types through the public surface
  • No redundant nil checks deep in call chains if the boundary already validated
Code organization:
  • Business logic in pure functions with no framework dependencies
  • Parse functions: pure transforms from raw bytes to typed state
  • Prompt construction: structured state in, string out
  • Scoring and assessment: pure transforms from state to results
The tests:
  • "Is this data crossing a system boundary right now?" If not, validation is redundant.
  • "Can this be a pure function that the shell just calls?" If yes, extract it.
将校验、类型收窄和错误处理放在系统边界。无条件信任内部代码。业务逻辑应存在于纯函数中。外壳层应保持轻量且机械化。
原因: 分散的校验会显得嘈杂、冗余,并营造一种虚假的安全感。应将逻辑排除在框架连接之外,使其无需依赖框架即可测试。
模式:
  • 在边界处(CLI 参数、配置文件、外部 API、网络协议):进行校验、返回错误并采取防御性处理。
  • 在系统内部: 使用类型化数据和错误传播,不重复校验。信任类型。
  • 跨越边界时。 暴露领域概念,而不是边界的私有表示。通用机制保留在内部,专用策略放在边缘。
应用场景:
校验与错误处理:
  • 在解析时(边界处)校验配置,而不是在业务逻辑内部校验
  • 在边界处将原始数据解析为领域类型
  • 不要通过公共接口重新导出传输、存储、框架或线路类型
  • 如果边界已经完成校验,深层调用链中就不需要进行冗余的 nil 检查
代码组织:
  • 业务逻辑应位于不依赖框架的纯函数中
  • 解析函数:从原始字节到类型化状态的纯转换
  • Prompt 构建:输入结构化状态,输出字符串
  • 评分与评估:从状态到结果的纯转换
测试问题:
  • “这些数据现在是否正在跨越系统边界?”如果不是,校验就是冗余的。
  • “这是否可以是一个由外壳直接调用的纯函数?”如果可以,就将其提取出来。