an important topic: preconditions and postconditions. they are a method of specifying what a method...

25
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 method a method accomplishes. accomplishes. Preconditions and Postconditions Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

Upload: michael-stevenson

Post on 05-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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 method accomplishes.method accomplishes.

Preconditions and PostconditionsPreconditions and Postconditions

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Preconditions and PostconditionsPreconditions and Postconditions

Frequently a programmer must communicate Frequently a programmer must communicate precisely precisely whatwhat a method accomplishes, a method accomplishes, without any indication of without any indication of howhow the method the method 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: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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 method forwrite a method for part of a project.part of a project.

HERE ARETHE REQUIREMENTS

FOR A METHOD THAT IWANT YOU TO

WRITE.

I DON'T CAREHOW THE

METHOD WORKS,AS LONG AS THESE

REQUIREMENTSARE MET.

Page 4: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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 method.with a pair of statements about the method.

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

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

Page 5: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.public void writeSqrt( double x)

...

Page 6: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.public void writeSqrt( double x)

...}

The precondition and postcondition The precondition and postcondition appear as comments in your appear as comments in your program.program.

They are usually placed before the They are usually placed before the method’s implementation.method’s implementation.

Page 7: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.public void writeSqrt( double x)

...}

In this example, the precondition In this example, the precondition requires thatrequires that

x >= 0x >= 0

be true whenever the method is called.be true whenever the method is called.

Page 8: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

writeSqrt( -10 );writeSqrt( 0 );writeSqrt( 5.6 );

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

Page 9: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

Which of these method callsWhich of these method 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.

writeSqrt( -10 );writeSqrt( 0 );writeSqrt( 5.6 );

Page 10: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

Which of these method callsWhich of these method 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.

writeSqrt( -10 );writeSqrt( 0 );writeSqrt( 5.6 );

Page 11: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.public void writeSqrt( double x)

...}

The postcondition always indicates The postcondition always indicates what work the method has what work the method has accomplished. In this case, when the accomplished. In this case, when the method returns the square root of method returns the square root of xx has been written. has been written.

Page 12: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Another ExampleAnother Example

// Precondition: letter is an uppercase or// lowercase letter (in the range 'A' ... 'Z' or 'a' ... 'z') .// Postcondition: The value returned by the// method is true if letter is a vowel;// otherwise the value returned by the method is// false. public boolean isVowel( char letter )

...

Page 13: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Another ExampleAnother Example

isVowel( 'A' );isVowel(' Z' );isVowel( '?' );

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

Page 14: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Another ExampleAnother Example

isVowel( 'A' );isVowel(' Z' );isVowel( '?' );

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

falsefalse

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

Page 15: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Another ExampleAnother Example

isVowel( '?' );

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

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

Page 16: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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

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

AT THIS POINT, MYPROGRAM CALLS YOUR

METHOD, AND I MAKESURE THAT THE

PRECONDITION ISVALID.

Page 17: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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

The programmer who The programmer who writes the method counts writes the method 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 method’s end.at the method’s end.

THEN MY METHODWILL EXECUTE, AND WHEN

IT IS DONE, THEPOSTCONDITION WILL BE

TRUE.I GUARANTEE IT.

Page 18: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

A QuizA Quiz

Suppose that you call Suppose that you call a method, and you a method, and you neglect to make sure neglect to make sure that the precondition that the precondition is valid. is valid. Who is responsible if Who is responsible if this inadvertently this inadvertently causes a 40-day flood causes a 40-day flood or other disaster?or other disaster?

YouYou The programmer who The programmer who

wrote that torrential wrote that torrential methodmethod

NoahNoah

Page 19: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

A QuizA Quiz

Suppose that you call Suppose that you call a method, and you a method, and you neglect to make sure neglect to make sure that the precondition that the precondition is valid. is valid. Who is responsible if Who is responsible if this inadvertently this inadvertently causes a 40-day flood causes a 40-day flood or other disaster?or other disaster?

YouYou

The programmer who The programmer who calls a method is calls a method is responsible for responsible for ensuring that the ensuring that the precondition is valid.precondition is valid.

Page 20: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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

When you write a method, you should make When you write a method, 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 violated, then print an error message and halt the program.halt the program.

Page 21: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

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

When you write a method, you should make When you write a method, 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 22: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

ExampleExample

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.public void writeSqrt( double x){ if (x < 0) throw new IllegalArgumentException(“Negative x”);

... Throwing an Throwing an exception(described in exception(described in Section 1.1) is useful.Section 1.1) is useful.

Page 23: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

Advantages of Using Preconditions and PostconditionsAdvantages of Using Preconditions and Postconditions

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

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

method in a new way ...method 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 24: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

PreconditionPrecondition The programmer who calls The programmer who calls

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

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

PostconditionPostcondition The programmer The programmer

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

SummarySummary

Page 25: An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions

THE ENDTHE END

Presentation copyright 1999, Addison Wesley LongmanPresentation copyright 1999, Addison Wesley LongmanFor use with For use with Data Structures and Other Objects Using JavaData Structures and Other Objects Using Javaby Michael Main.by Michael Main.

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 Java Using Java 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.