Search code examples
design-by-contractpreconditionspost-conditions

Weak precondition and strong postcondition problems?


in last exam, I've had question, I was unable to answer sanely.

Question was "what problems might arise from too weak precondition?"

Another question was "what problems might arise from too strong postcondtition?"

How to go about answering that question? My attempt at explaining that weak precondition evaluated as truth, might be not enough as implication to get strong postcondition as true, but seems it is not enough.

Am wondering, what is formally sane answer to this, actually two short questions.

"What problems arise from weak precondition?" "What problems arise from strong postcondition?"


Solution

  • Both a weak precondition and a strong postcondition make a specification stronger. A too-strong specification reduces flexibility for the implementer of a specification (an interface). This results in a more complex and more expensive implementation. Furthermore, not all clients might even need the strength, so the complexity and expense might go to waste.

    As a simple example, a specification might promise to return a sorted list of results. This prevents the implementation from using an efficient representation such as a hash table, and the client might not even need the results sorted.

    Citation: slides from http://courses.cs.washington.edu/courses/cse331/ .