Design by contract by example

Design by contract, by exampleの6つの原則。

Principle 1: Separate queries from commands.
Principle 2: Separate basic queries from derived queries.
Principle 3: For each derived query, write a postcondition that specifies what result will be returned in terms of one or more basic queries.
Principle 4: For each command, write a postcondition that specifies the value of every basic query.
Principle 5: For every query and command, decide on a suitable precondition.
Principle 6: Write invariants to define unchanging properties of objects.


最初の4つが特に大事。

  • オブジェクトの状態を取得するときに状態を変えない。
  • オブジェクトの「基底ベクトル」は何かを意識する。

design by contractを使うかどうかは別として
unit testを書くときにこれを分かっているかどうかで、
テストの質が全然違うはず。