Preconditions and Postconditions - cs.colorado.edu

Preconditions and Postconditions Data Structures and Other Objects Using C++ An important topic: preconditions and postconditions. They are a method of specifying what a function accomplishes.

Preconditions and Postconditions Frequently a programmer must communicate precisely what a function accomplishes, without any indication of how the function does its work. Can you think of a situatio where this would occur ? Example You are the head of a programming team

and you want one of your programmers to write a function for part of a project. HERE ARE THE REQUIREMENTS FOR A FUNCTION THAT I WANT YOU TO WRITE. I DON'T CARE WHAT METHOD THE FUNCTION USES, AS LONG AS THESE

REQUIREMENTS ARE MET. What are Preconditions and Postconditions? One way to specify such requirements is with a pair of statements about the function. The precondition statement indicates what must be true before the function is called.

The postcondition statement indicates what will be true when the function finishes its work. Example void write_sqrt(double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. ... Example void write_sqrt( double x) // Precondition: x >= 0.

// Postcondition: The square root of x has // been written to the standard output. The precondition and postcondition appear as comments in your program. ... } Example void write_sqrt( double x) // Precondition: x >= 0.

// Postcondition: The square root of x has // been written to the standard output. In this example, the precondition requires that x >= 0 be true whenever the function is called. } ... Example

ich of these function calls et the precondition ? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Example ich of these function calls et the precondition ? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); The second and third calls are fine, since the argument is greater than or equal to

zero. Example ich of these function calls et the precondition ? write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); But the first call violates the precondition, since the argument is less than zero. Example void write_sqrt( double x)

// Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. The postcondition always indicates what work the function has accomplished. In this case, when the function returns the square root of x has been written. ... }

Another Example bool is_vowel( char letter ) // Precondition: letter is an uppercase or // lowercase letter (in the range 'A' ... 'Z' or 'a' ... 'z') . // Postcondition: The value returned by the // function is true if Letter is a vowel; // otherwise the value returned by the function is // false. ... Another Example at values will be returned these function calls ?

is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); Another Example at values will be returned true these function calls ? is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); false

Nobody knows, because the precondition has been violated. Another Example at values will be returned these function calls ? is_vowel( '?' ); Violating the precondition might even crash the computer. Always make sure the precondition is valid . . .

The programmer who calls the function is responsible for ensuring that the precondition is valid when the function is called. AT THIS POINT, MY PROGRAM CALLS YOUR FUNCTION, AND I MAKE SURE THAT THE PRECONDITION IS VALID.

. . . so the postcondition becomes true at the functions end. The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes true at the functions end. THEN MY FUNCTION WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE

TRUE. I GUARANTEE IT. A Quiz Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40-day flood or other disaster? You

The programmer who wrote that torrential function Noah A Quiz Suppose that you call a function, and you neglect to make sure that the precondition is valid. Who is responsible if this inadvertently causes a 40-day flood

or other disaster? You The programmer who calls a function is responsible for ensuring that the precondition is valid. On the other hand, careful programmers also follow these rules:

When you write a function, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program. On the other hand, careful programmers also follow these rules:

When you write a function, you should make every effort to detect when a precondition has been violated. If you detect that a precondition has been violated, then print an error message and halt the program... ...rather than causing a disaster. Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has

// been written to the standard output. { assert(x >= 0); ... The assert function (described in Section 1.1) is useful for detecting violations of a precondition. Advantages of Using Preconditions and Postconditions

Succinctly describes the behavior of a function... ... without cluttering up your thinking with details of how the function works. At a later point, you may reimplement the function in a new way ... ... but programs (which only depend on the

precondition/postcondition) will still work with no changes. Summary Precondition The programmer who calls a function ensures that the precondition is valid. The programmer who writes a function can bank on the precondition being true when the function begins execution. Postcondition

The programmer who writes a function ensures that the postcondition is true when the function finishes executing. Presentation copyright 2010, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club

Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. THE END

Recently Viewed Presentations

  • Not feeling well!! Who do you go to??

    Not feeling well!! Who do you go to??

    shia ithna asheri madressa 2002 mujtahid expert in islamic laws taqlid act of obeying islamic rules muqallid one who follows the orders of the mujtahid who is our present mujtahid? Title Author
  • PowerPoint Presentation

    PowerPoint Presentation

    Who or what gives the final tug that brings up the turnip? author illustrator Look for: Events that happen over and over. Things that are much bigger than they could be in real life. enormous grew planted granddaughter turnip strong...
  • Parallel DB 101 - GitHub Pages

    Parallel DB 101 - GitHub Pages

    Moore's Law. $100/TB storage, $1000 servers, commodity networking. Increasing volumes of "dark" data. Data collected but never analyzed. Widening analysis gap of "traditional" solutions
  • Windows Server 2003 Migration &Hybrid Cloud

    Windows Server 2003 Migration &Hybrid Cloud

    Catalog your software and workloads. 1. Discover what you have. System integrators. Microsoft Services JumpStart for Windows Server 2003 ... Self-service tools. Microsoft Assessment and Planning (MAP) Toolkit. Dell ChangeBASE. Lakeside Software SysTrack. Catalog your software and workloads. 4 ...
  • TOK: Perception - Miami-Dade County Public Schools

    TOK: Perception - Miami-Dade County Public Schools

    TOK: Perception Daniel W. Blackmon Coral Gables Senior High Perceptual Illusions Sensation: Interpretation Perceptual Illusions Context Perceptual Illusions Figure and Ground Perceptual Illusions Visual Grouping Expectations Theories of Reality Common-sense realism Scientific realism Phenomenalism Common-sense realism "What you see is...
  • Meaningful Communication Access for Deaf People In ... - MHIT

    Meaningful Communication Access for Deaf People In ... - MHIT

    Alfred Weinrib died of cancer but never knew his diagnosis after 3 Long Island hospitals allegedly did not have sign language interpreters on staff. ... Assess an individual's relative strengths and weaknesses across a continuum of communication modalities.
  • Lunar Pyroclastic Eruptions - University of Hawaii

    Lunar Pyroclastic Eruptions - University of Hawaii

    Lunar Pyroclastic Eruptions COSMOCHEMISTRY iLLUSTRATED Enrichments in Volatile Elements in Pyroclastic glass deposits Surfaces of pyroclastic glasses enriched in Zn, Cd, Hg, Pb, Cu, F, Cl compared to other lunar materials Probably were in gas phase associated with eruption Implies...
  • Real Estate Development Introduction

    Real Estate Development Introduction

    Real Estate Development Introduction Real Estate Development Introduction Real Estate Development Economic Feasibility Real Estate Development Economic Feasibility ...