23
An important An important topic: topic: preconditions preconditions and and postconditions postconditions . . They are a They are a method of method of specifying what specifying what a function a function accomplishes. accomplishes. Illinois State University Department of Applied Computer Science ACS 169 Data Structures Data Structures and Other Objects and Other Objects Using C++ Using C++

P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Embed Size (px)

Citation preview

Page 1: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

An important topic: An important topic: preconditionspreconditions and and postconditionspostconditions..

They are a method of They are a method of specifying what a specifying what a function accomplishes.function accomplishes.

Illinois State UniversityDepartment of Applied Computer ScienceACS 169

Illinois State UniversityDepartment of Applied Computer ScienceACS 169

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing C++Using C++

Page 2: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Preconditions and PostconditionsPreconditions and Postconditions

Frequently a programmer must communicate Frequently a programmer must communicate precisely precisely whatwhat a function accomplishes, a function accomplishes, without any indication of without any indication of howhow the function the function does its work.does its work.

Can you think of a situationCan you think of a situationwhere this would occur ?where this would occur ?

Page 3: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

You are the head of a You are the head of a programming team programming team and you want one of and you want one of your programmers to your programmers to write a function for write a function for part of a project.part of a project.

HERE ARETHE REQUIREMENTS

FOR A FUNCTION THAT IWANT YOU TO

WRITE.

I DON'T CAREWHAT METHOD THE

FUNCTION USES,AS LONG AS THESE

REQUIREMENTSARE MET.

Page 4: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

What are Preconditions and Postconditions?What are Preconditions and Postconditions?

One way to specify such requirements is One way to specify such requirements is with a pair of statements about the function.with a pair of statements about the function.

The The preconditionprecondition statement indicates what statement indicates what must be true before the function is called.must be true before the function is called.

The The postconditionpostcondition statement indicates what statement indicates what will be true when the function finishes its will be true when the function finishes its work.work.

Page 5: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...

Page 6: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

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 The precondition and postcondition appear as comments in your appear as comments in your program.program.

They are usually placed after the They are usually placed after the function’s parameter list.function’s parameter list.

Page 7: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

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 In this example, the precondition requires thatrequires that

x >= 0x >= 0

be true whenever the function is called.be true whenever the function is called.

Page 8: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

Page 9: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

The second and third calls are fine, sinceThe second and third calls are fine, sincethe argument is greater than or equal to zero.the argument is greater than or equal to zero.

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Page 10: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

But the first call violates the precondition,But the first call violates the precondition,since the argument is less than zero.since the argument is less than zero.

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Page 11: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

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 The postcondition always indicates what work the function has what work the function has accomplished. In this case, when the accomplished. In this case, when the function returns the square root of function returns the square root of xx has been written. has been written.

Page 12: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Another ExampleAnother 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.

...

Page 13: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Another ExampleAnother Example

is_vowel( 'A' );is_vowel(' Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

Page 14: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Another ExampleAnother Example

is_vowel( 'A' );is_vowel(' Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ? truetrue

falsefalse

Nobody knows, because theNobody knows, because theprecondition has been violated.precondition has been violated.

Page 15: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Another ExampleAnother Example

is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

Violating the preconditionViolating the preconditionmight even crash the computer.might even crash the computer.

Page 16: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Always make sure the precondition is valid . . .Always make sure the precondition is valid . . .

The programmer who The programmer who calls the function is calls the function is responsible for responsible for ensuring that the ensuring that the precondition is valid precondition is valid when the function is when the function is called.called.

AT THIS POINT, MYPROGRAM CALLS YOURFUNCTION, AND I MAKE

SURE THAT THEPRECONDITION IS

VALID.

Page 17: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

. . . so the postcondition becomes true at the function’s end. . . . so the postcondition becomes true at the function’s end.

The programmer who The programmer who writes the function counts writes the function counts on the precondition being on the precondition being valid, and valid, and ensures that the ensures that the postcondition becomes postcondition becomes true true at the function’s end.at the function’s end.

THEN MY FUNCTIONWILL EXECUTE, AND WHEN

IT IS DONE, THEPOSTCONDITION WILL BE

TRUE.I GUARANTEE IT.

Page 18: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

On the other hand, careful programmers also follow these rules:On the other hand, careful programmers also follow these rules:

When you write a function, you should When you write a function, you should make every effort to detect when a make every effort to detect when a precondition has been violated.precondition has been violated.

If you detect that a precondition has been If you detect that a precondition has been violated, then print an error message and violated, then print an error message and halt the program.halt the program.

Page 19: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

On the other hand, careful programmers also follow these rules:On the other hand, careful programmers also follow these rules:

When you write a function, you should make When you write a function, you should make every effort to detect when a precondition every effort to detect when a precondition has been violated.has been violated.

If you detect that a precondition has been If you detect that a precondition has been violated, then print an error message and halt violated, then print an error message and halt the program...the program...

...rather than causing...rather than causing

a disaster.a disaster.

Page 20: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

ExampleExample

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 The assert function (described in Section 1.1) is (described in Section 1.1) is useful for detecting violations useful for detecting violations of a precondition.of a precondition.

Page 21: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

Advantages of Using Preconditions and PostconditionsAdvantages of Using Preconditions and Postconditions

Succinctly describes the behavior of a function...Succinctly describes the behavior of a function... ... without cluttering up your thinking with details ... without cluttering up your thinking with details

of how the function works.of how the function works. At a later point, you may reimplement the At a later point, you may reimplement the

function in a new way ...function in a new way ... ... but programs (which only depend on the ... but programs (which only depend on the

precondition/postcondition) will still work with precondition/postcondition) will still work with no changes.no changes.

Page 22: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

PreconditionPrecondition The programmer who calls The programmer who calls

a function ensures that the a function ensures that the precondition is valid.precondition is valid.

The programmer who The programmer who writes a function can bank writes a function can bank on the precondition being on the precondition being true when the function true when the function begins execution.begins execution.

PostconditionPostcondition The programmer The programmer

who writes a who writes a function ensures function ensures that the that the postcondition is postcondition is true when the true when the function finishes function finishes executing.executing.

SummarySummary

Page 23: P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department

THE ENDTHE END

Presentation copyright 1997, Addison Wesley LongmanPresentation copyright 1997, Addison Wesley LongmanFor use with For use with Data Structures and Other Objects Using C++Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task ForceSome artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubGraphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).

Students and instructors who use Students and instructors who use Data Structures and Other ObjectsData Structures and Other Objects Using C++ Using C++ arearewelcome to use this presentation however they see fit, so long as this copyright notice welcome to use this presentation however they see fit, so long as this copyright notice remains intact.remains intact.