设计契约 (DbC) 将方法与其调用者之间的关系视为一项正式协议,由三个要素定义:preconditions(调用者必须保证的条件)、postconditions(方法作为回报必须保证的条件)和 invariants(对象始终保持为真的条件)。
三项义务
text
PRECONDITION → caller's duty: inputs/state the method requires to run correctly
POSTCONDITION → method's duty: what it promises on return (if precondition held)
INVARIANT → always-true property of the object, before and after every method
在代码中
python
:
():
._balance = balance
._check_invariant()
():
amount > ,
amount <= ._balance,
old = ._balance
._balance -= amount
._balance == old - amount,
._check_invariant()
():
._balance >= ,
