Design by Contract (DBC) (1,2)
Design by Contract (DBC) (1,2)
● The Design by Contract (DbC) methodology has entered software development due
to Bertrand Meyer, along with the Eiffel language
● It proposes a contractual approach to the development of object-oriented software
components, based on the use of assertions
● The approach has been aimed at increasing the reliability of object-oriented software
components - a critical requirement in the context of large-scale software reuse, as
promoted by the object-oriented paradigm
– reliability = correctness (software's ability to behave according to the specification) +
robustness (the ability to properly handle situations outside of the specification)
● Expected to positively contribute to
– the develoment of correct and robust OO systems
– a deeper understanding of inheritance and related concepts (overriding, polymorphism,
dynamic bynding), by means of the subcontracting concept
– a systematic approach to exception handling
1
Software Contracts. Assertions
● A <pre, post> pair for a method expresses the software contract that the method in
question (provider of some service) publishes for its callers (clients of the service)
● Characteristics of software contracts
– the precondition defines the situation when a call is legitimate - obligation for the client
(caller) and benefit for the provider (method)
– the postcondition states which properties should be fulfilled once the execution has ended -
obligation for the provider (method) and benefit for the client (caller)
● The major contribution brought by DBC in the field of software reliability: precondition =
benefit of the service provider
● DBC non-redundancy principle: The body of a mehod should never check for a
precondition (as opposed to defensive programming)
● In Eiffel, assertions are part of the language, allowing for runtime monitoring
– precondition violation = bug in the client, postcondition violation = bug in the
supplier
4
Invariant assertions
● An assertion I is a correct invariant for a class C if and only if the following conditions
hold
– each constructor of C, apply to arguments that fulfill its precondition, in a state in which the
class attributes have default values, leads to a state in which I is fulfilled
– each public method of C, applied to a set of arguments and to a state satisfying both I and
the method precondition, leads to a state satisfying I
5
Correctness of a class
● Informally, a class is said to be correct with respect to its specification if and only if
its implementation, as given by the method bodies, is consistent with its
preconditions, postconditions and invariant
● Definition: The class C is said to be correct with respect to its assertions (pre/post-
conditions and invariant) if and only if the following conditions hold:
(1) [default_C and pre_p(x_p)] body_p [post_p(x_p) and INV]
for each class constructor p and each set of valid arguments of p – x_p and
(2) [pre_r(x_r) and INV] body_r [post_r(x_r) and INV]
for each public class method r and each set of valid arguments of r – x_r, where
default_C is an assertion stating that the attributes of C have default values for their
type, INV is the invariant of C, pre_m, post_m, body_m are the precondition,
postcondition and body of an arbitrary method m of C.
6
The purpose of using assertions
● Support in writting correct software, including the means to formally define correctness
– The writing of explicit contracts comes as a prerequisite of their enforcement in software
● Support for a better software documentation
– Essential when it comes to reusable assets, see the case of Ariane!
● Support for testing, debugging and quality assurance
– Levels of runtime assertion monitoring:
● 1.preconditions only
● 2.preconditions and postconditions
● 3.all assertions
– While testing, enforce level 3, in production, there is a tradeoff between trust in the code,
efficiency level desired and critical nature of the application
● Support for the development of fault tolerant systems
7
Defensive Programming [3]