Re: precondition and postcondition



On Mon, 22 Aug 2005 16:41:38 GMT, "Tony Johansson"
<johansson.andersson@xxxxxxxxx> wrote:

>Hello Experts!!
>
>Why is it so important that you following this rules stated by Mayer:
>"when redefining a routine [in a deriative] you may only replace its
>precondition by a weaker one, and its postcondition by a stronger one. Which
>consequences should there be if I don't follow this rules."

Violating these rules will result in downcasts and typecases (switch
statements in which the cases correspond to object types).

>What happens if I override a function and replace the precondition with a
>stronger one?

Then callers who think they are using the base class, but who are
really using the derived class, will get failures. To put it another
way, polymorphism will fail, and you'll have to put explicit 'if'
statements (i.e. typecases) in your code.

>There are certain rules that should be adhered to when Design by Contract is
>used with inheritance. Eiffel has language support to enforce these but in
>C++ we must rely on the programmer. The rules are, in a derived class (Meyer
>Chapter 16):
>
> 1.. An overriding method may [only] weaken the precondition. This means
>that the overriding precondition should be logically "or-ed" with the
>overridden precondition.
> 2.. An overriding method may [only] strengthen the postcondition. This
>means that the overriding postcondition should be logically "and-ed" with
>the overridden postcondition.
>
>Can you add some example just to understand it better.

class Rectangle {
protected:
double height;
double width;
public:
virtual ~Rectangle() {}

Rectangle(double h, double w)
: height(h)
, width(w)
{
assert(h>0 && w>0);
}

virtual void setHeight(double h) {
assert(h>0); // precondition
Rectangle old = *this;
height = h;
assert(width == old.width); // postcondition
}

virtual void setWidth(double w) {
assert(w>0); // precondition
Rectangle old = *this;
width = w;
assert(height == old.height); // postcondition
}
}

class Square : public Rectangle {
public:
virtual ~Square() {}

Square(double s)
: Rectangle(s,s)
{}

virtual void setHeight(double h) {
assert(h>0); // same precondition
height = h;
width = h;
// note: inherited postcondition is violated
}

virtual void setWidth(double w) {
assert(w>0); // same precondition
width = w;
height = w;
// note: inherited postcondition is violated
}
}
-----
Robert C. Martin (Uncle Bob) | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716


"The aim of science is not to open the door to infinite wisdom,
but to set a limit to infinite error."
-- Bertolt Brecht, Life of Galileo
.



Relevant Pages

  • Re: Best forum for OOD/Design by Contract discussions?
    ... relates to Design by Contract? ... DbC is a tool that was originally developed in R-T/E back in the '60s because it provides a rigorous approach to designing interactions among multiple state machines. ... The precondition includes algorithmic sequencing defined in requirements and the state of the overall solution as expressed in terms of state data values. ... Whatever that other behavior did can be expressed in terms of a postcondition on executing that other behavior. ...
    (comp.object)
  • Re: Client/Service relationships & Flow of Requirements.
    ... 'Observer' mean in the GoF pattern. ... Thus the precondition for executing the ... Observer action is not a precondition on a specific Subject object; ... The called must guarantee the postcondition is true *to the caller*, so it can't change the postcondition to suit its own purposes. ...
    (comp.object)
  • Re: Liskov Substitution Principle and Abstract Factories
    ... LSP tells that B's precondition should be the ... and postcondition should be the subset of A's. ... So if we want to substitute B for A (with seperate no contract enforcment), ...
    (comp.object)
  • Re: before after methods and call-next-method
    ... then the postcondition will be true if the function returns. ... let's assume that all the elements of the precondition ... Now suppose we implement a primary method that implements the generic ... method specializing on C. ...
    (comp.lang.lisp)