Скачать презентацию cs 205 engineering software university ofvirginia Specifying Procedures Скачать презентацию cs 205 engineering software university ofvirginia Specifying Procedures

670101a82a85a4ab9625617cafec1a2a.ppt

  • Количество слайдов: 23

cs 205: engineering software university ofvirginia Specifying Procedures David Evans www. cs. virginia. edu/cs cs 205: engineering software university ofvirginia Specifying Procedures David Evans www. cs. virginia. edu/cs 205 fall 2006

Procedural Specifications • Specification for a procedure describes: – What its inputs are – Procedural Specifications • Specification for a procedure describes: – What its inputs are – What the mapping between inputs and outputs are – What it can do the state of the world 2

Requires and Effects • Header: name of procedure, types of parameters and return value Requires and Effects • Header: name of procedure, types of parameters and return value – Java declaration • Clauses (comments in Java) – REQUIRES - precondition the client must satisfy before calling – EFFECTS – postcondition the implementation satisfy at return 3

Contract • Client promises to satisfy the precondition in the requires clause • Implementer Contract • Client promises to satisfy the precondition in the requires clause • Implementer promises if client satisfies the precondition, the return value and state when the function returns will satisfy the postcondition. 4

Specification Contract f () REQUIRES: precondition EFFECTS: postcondition precondition { f (); } postcondition Specification Contract f () REQUIRES: precondition EFFECTS: postcondition precondition { f (); } postcondition If the precondition is true, after we call f (), the postcondition is true. 5

Specification Example public String best. Stock () // REQUIRES: false // EFFECTS: Returns the Specification Example public String best. Stock () // REQUIRES: false // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification? Yes, any implementation will satisfy this specification! If the precondition in the requires clause is not satisfied, the procedure can do anything and still satisfy its specification! 6

Specification Example public String best. Stock () // REQUIRES: true // EFFECTS: Returns the Specification Example public String best. Stock () // REQUIRES: true // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification? 7

Requires Clauses • The weaker (more easy to make true) the requires clause: – Requires Clauses • The weaker (more easy to make true) the requires clause: – The more useful a procedure is for clients – The more difficult it is to implement correctly • Avoid requires clauses unless there is a good reason to have one – Default requires clause is: REQUIRES true – Client doesn’t need to satisfy anything before calling 8

Specification Example public static int biggest (int [ ] a) // REQUIRES: true // Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: Returns the value of the // biggest element of a. Is this a reasonable specification? No, what should client expect to happen if a is empty. 9

Specification Example public static int biggest (int [ ] a) // REQUIRES: a has Specification Example public static int biggest (int [ ] a) // REQUIRES: a has at least one element. // EFFECTS: Returns the value of the // biggest element of a. Is this a good specification? Maybe, depends on the client. Its risky… 10

Specification Example public static int biggest (int [ ] a) // REQUIRES: true // Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one // element, returns the value biggest // element of a. Otherwise, returns // Integer. MIN_VALUE (smallest int // value). Better, but client has to deal with special case now. Best would probably be to use an exception… 11

Bad Use of Requires Clause • Bug discovered in Microsoft Outlook that treats messages Bad Use of Requires Clause • Bug discovered in Microsoft Outlook that treats messages that start with “begin ” as empty attachments (can be exploited by viruses) To workaround this problem: • Do not start messages with the word "begin" followed by two spaces. • Use only one space between the word "begin" and the following data. • Capitalize the word "begin" so that it is reads "Begin. " • Use a different word such as "start" or "commence". from http: //support. microsoft. com/default. aspx? scid=KB; EN-US; Q 265230& (this is no longer available, was “revoked” by Microsoft) 12

Modifies • How does a client know a is the same after biggest returns? Modifies • How does a client know a is the same after biggest returns? public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer. MIN_VALUE // (smallest int value). Reading the effects clause is enough – if biggest modifies anything, it should describe it. But, that’s a lot of work. 13

Modifies • Modifies clause: any state not listed in the modifies clause may not Modifies • Modifies clause: any state not listed in the modifies clause may not be changed by the procedure. public static int biggest (int [ ] a) // REQUIRES: true // MODIFIES: nothing // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer. MIN_VALUE // (smallest int value). 14

Modifies Example public static int replace. Biggest (int [ ] a, int [] b) Modifies Example public static int replace. Biggest (int [ ] a, int [] b) // REQUIRES: a and b both have at least one // element // MODIFIES: a // EFFECTS: Replaces the value of the biggest // element in a with the value of the biggest // element in b. 15

Defaults • What should it mean when there is no requires clause? REQUIRES: true Defaults • What should it mean when there is no requires clause? REQUIRES: true • What should it mean when there is no modifies clause? MODIFIES: nothing • What should it mean when there is no effects clause? Meaningless. 16

PS 1 • Question 2 was very ambiguous: Modify the program so that instead PS 1 • Question 2 was very ambiguous: Modify the program so that instead of dying instantly, cells go through a dying state where they are displayed in a different color for one step before they die. How should the life rules deal with dying cells? 17

Dealing with Bad Specs • When a specification is ambiguous: – Ask the provider Dealing with Bad Specs • When a specification is ambiguous: – Ask the provider to figure out what it means – Or, state very clearly what additional assumptions you make • Good (easy to implement) assumption here: dying cells are not alive (as far as neighbors are concerned), and don’t become alive (whatever their neighbor states are) 18

Avoid Changing Interfaces boolean is. Alive() // EFFECTS: Returns true if the cell is Avoid Changing Interfaces boolean is. Alive() // EFFECTS: Returns true if the cell is alive, false otherwise. • If we change this, need to examine all code that uses is. Alive • If we just change its implementation, don’t need to examine anything else 19

enum (added to Java 1. 5) public enum State. Value { dead, dying, alive enum (added to Java 1. 5) public enum State. Value { dead, dying, alive }; . . . public Color get. Color() { switch (alive) { case alive: return Color. green; case dying: return Color. red; case dead: return Color. white; } } 20

PS 1 Grading • Full credit even for inelegant code • Later assignments: need PS 1 Grading • Full credit even for inelegant code • Later assignments: need clear, elegant, economical code for full credit 21

PS 2 • You will need to write a fair bit of code yourself PS 2 • You will need to write a fair bit of code yourself • Challenge is to make your specification and implementation cover all possible inputs • Next class: some hints on PS 2 22

Charge • Specifications in CS 205 – Will be informal: written in English (aided Charge • Specifications in CS 205 – Will be informal: written in English (aided by common mathematical notations) –. . . but must be precise and clear – REQUIRES/MODIFIES/EFFECTS style 23