desbycon s

Upload: akbisoi1

Post on 03-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 DesByCon S

    1/16

  • 8/12/2019 DesByCon S

    2/16

    2 Wolfgang Pelz 2000-04Design by Contract

    Basic Premise

    To improve software reliability, the first and

    perhaps most difficult problem is to define

    as precisely as possible, for each softwareelement, what it is supposed to do.

  • 8/12/2019 DesByCon S

    3/16

    3 Wolfgang Pelz 2000-04Design by Contract

    Design by Contract

    associate a specification with every

    software element

    these specifications (or contracts) govern

    the interaction of the element with the rest

    of the world.

  • 8/12/2019 DesByCon S

    4/16

    4 Wolfgang Pelz 2000-04Design by Contract

    Benefits

    A better understanding of the object-

    oriented method and, more generally, of

    software construction.

    A systematic approach to building bug-free

    object-oriented systems.

    An effective framework for debugging,testing and, more generally, quality

    assurance.

  • 8/12/2019 DesByCon S

    5/16

    5 Wolfgang Pelz 2000-04Design by Contract

    More Benefits

    A method for documenting software

    components.

    Better understanding and control of the

    inheritance mechanism.

    A technique for dealing with abnormal

    cases, leading to a safe and effectivelanguage construct for exception handling.

  • 8/12/2019 DesByCon S

    6/16

    6 Wolfgang Pelz 2000-04Design by Contract

    Tabular Form of Contract

    Party Obligations Benefits

    Client Provide letter or package Get package delivered

    of no more than 5 kgs, each in 4 hours or less

    dimension < 2 meters.

    Pay 100 francs.

    UPS2 Deliver package to recipient No need to deal with

    in 4 hours or less. deliveries too big,

    too heavy, or unpaid.

  • 8/12/2019 DesByCon S

    7/16

    7 Wolfgang Pelz 2000-04Design by Contract

    Rationale

    a contract document protects both the client,

    by specifying how much should be done,

    and the supplier, by stating that the supplieris not liable for failing to carry out tasks

    outside of the specified scope

    the obligations of the supplier become thebenefits to the client

  • 8/12/2019 DesByCon S

    8/16

    8 Wolfgang Pelz 2000-04Design by Contract

    Rationale restated

    a contract protects both sides:

    protects the client by specifying how much

    should be done; the client is entitled to

    receive a certain result

    protects the contractor by specifying how

    littleis acceptable; the contractor must notbe liable for failing to carry out tasks

    outside of the specified scope

  • 8/12/2019 DesByCon S

    9/16

    9 Wolfgang Pelz 2000-04Design by Contract

    Assertions

    preconditions and postconditions

    routine_name (argument declarations) is

    require

    Precondition

    do

    Routine body (instructions)

    ensure

    Postcondition

    end

  • 8/12/2019 DesByCon S

    10/16

    10 Wolfgang Pelz 2000-04Design by Contract

    Violation of an Assertion

    a precondition violation indicates a bug in

    the client (caller); the caller did not observe

    the conditions imposed on correct calls

    a postcondition violation is a bug in the

    supplier (called routine); the routine failedto deliver on its promises

  • 8/12/2019 DesByCon S

    11/16

    11 Wolfgang Pelz 2000-04Design by Contract

    Defensive Programming

    requires redundant checks in both the client

    and the supplier

    not necessary if assertions are used in

    writing the software to spell out the

    consistency conditions which could go

    wrong at runtime

  • 8/12/2019 DesByCon S

    12/16

    12 Wolfgang Pelz 2000-04Design by Contract

    Effect on Software

    strong preconditions

    heavier burden on the client

    lighter burden on the supplier

    dealing with abnormal values is a pragmatic

    decision about division of labor

    in many existing programs, one searches for

    islands of useful processing midst oceans of

    error-checking code due to redundancy

  • 8/12/2019 DesByCon S

    13/16

    13 Wolfgang Pelz 2000-04Design by Contract

    Assertion in VC++

    http://www.codeproject.com/cpp/assertisyourfr

    iend.asp

    assert(condition);

    // fail if the condition is not true.

    void CMyClass::MyFunc(char * szStringPtr)

    { if (szStringPtr[0] == '7') DoSomething(); }

    http://www.codeproject.com/cpp/assertisyourfriend.asphttp://www.codeproject.com/cpp/assertisyourfriend.asphttp://www.codeproject.com/cpp/assertisyourfriend.asphttp://www.codeproject.com/cpp/assertisyourfriend.asp
  • 8/12/2019 DesByCon S

    14/16

    14 Wolfgang Pelz 2000-04Design by Contract

    Assertion in VC++

    void CMyClass:: MyFunc(char *szStringPtr) {

    ASSERT(szStringPtr);

    if (szStringPtr[0] == '7') DoSomething();

    }

    http://www.thescripts.com/
  • 8/12/2019 DesByCon S

    15/16

    15 Wolfgang Pelz 2000-04Design by Contract

    Assertion in VC++

    http://www.thescripts.com/forum/thread61056.html

    Debug assertion failure

    When I close my program and call:

    delete *iter2;

    I get a "Debug Assertion Failed!" message saying:

    File: dbgheap.c

    Line: 1017

    Expression:

    _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

    What does this mean?

    http://www.thescripts.com/
  • 8/12/2019 DesByCon S

    16/16

    16 Wolfgang Pelz 2000-04Design by Contract

    Documenting a Contract

    assertions express the purpose of thesoftware elements (e.g., routines) without

    reference to implementation details still a research subject

    for the contract to work properly, the client

    programmers must have a properdescription of the interface properties of aclass and its routines