Design by Contract, DbC
一种软件设计方法,由 Bertrand Meyer 为 Eiffel 编程语言所提出。核心思想是在软件组件之间定义明确的契约,描述组件行为,实现供应者 (Supplier) 和客户 (Client) 之间的责任。
这些契约通常由以下内容组成,共同确保软件在调用时的正确使用和结果。
- 前置条件(Preconditions),定义了每个组件或函数可以被调用的前提条件。
- If a precondition is violated, then the bugis in the client’s code.
- 后置条件(Postconditions),规定了组件执行完后所保证的结果和状态。
- If a postcondition is violated, then the bugis in the supplier’s code.
契约式设计与 “防御性编程 (Defensive programming)” 的原则相反

- 即不需要为不满足前置条件的情况编写测试用例,这些情况在契约中已经被明确排除了。