Design By Contract - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Design By Contract

Description:

Design By Contract. Formal Methods se304/cs407. Thanks to Gary Leavens and Yoonsik Cheon ... null; //_at_ public invariant !subject1.equals(subject2); Quantifiers ... – PowerPoint PPT presentation

Number of Views:132
Avg rating:3.0/5.0
Slides: 15
Provided by: csM8
Category:
Tags: contract | design | gary | null

less

Transcript and Presenter's Notes

Title: Design By Contract


1
Design By Contract
  • Formal Methods se304/cs407
  • Thanks to Gary Leavens and Yoonsik Cheon

2
Recap
  • Pre-condition
  • Benefits supplier
  • Obligation of client
  • Usually based on arguments to method
  • Must be true before method begins
  • Keyword requires

3
Recap
  • Post-condition (normal and exceptional)
  • Benefits client
  • Obligation of supplier
  • Usually something to do with return value of
    method
  • Must be true after method is completed
  • Keyword ensures or signals

4
Advantages
  • Assign blame
  • Avoid certain subtle errors
  • binarySearch(int list, int a)
  • Automatic documentation, test generation
  • Encourages thorough, formal thought processes

5
New DBC concept - invariant
  • Always true of object in stable states
  • After constructor
  • Before and after method calls
  • Allows you to define
  • Acceptable states for object
  • What must remain consistant
  • /_at_ public invariant !name.equals()
  • _at_ weight gt 0 /

6
Example from lab
  • What would be the invariants for Student?
  • Class Student
  • private String name
  • private String subject1, subject2
  • //_at_ public invariant name ! null
  • //_at_ public invariant !subject1.equals(subject2)

7
Quantifiers
  • Sometimes you need more complexity
  • What if you wanted to say
  • All the Students in the Admin object must do
    Maths
  • The minimum marks of all this Students subjects
    must be greater than or equal to 40
  • ?

8
Quantifiers
  • For all ?
  • \forall
  • There exists ?
  • \exists
  • Sum, Product
  • \sum, \product
  • Minimum, Maximum
  • \min, \max
  • Number of
  • \num_of

9
Quantifiers - syntax
  • /_at_ invariant/requires/ensures
  • _at_ (quantifier declarations
  • _at_ restrictions
  • _at_ spec_expression) /

10
Quantifiers - example
  • All the Students in the Admin object must do
    maths
  • /_at_ invariant (\forall Student s
  • _at_ this.contains(s)
  • _at_ s.doesSubject(Maths) ) /

11
Quantifiers - example
  • The minimum marks for a Student must be greater
    than or equal to 40

12
Student example
  • class Student
  • private String name
  • private Subject subjects
  • Class Subject
  • int getMarks()

13
Quantifiers - example
  • /_at_ requires ( list is sorted in
  • _at_ ascending order ) /
  • binarySearch(int list, int a)

14
What do you need to know about DBC?
  • For the final exam
  • You will have to be able to add JML to Java
  • And understand JML given to you
  • Pre-conditions
  • Post-conditions
  • Invariants
  • Quantifiers
Write a Comment
User Comments (0)
About PowerShow.com