Software Engineering I (02161) Design by Contract Assoc. Prof. - - PowerPoint PPT Presentation

software engineering i 02161
SMART_READER_LITE
LIVE PREVIEW

Software Engineering I (02161) Design by Contract Assoc. Prof. - - PowerPoint PPT Presentation

Software Engineering I (02161) Design by Contract Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2020 Contracts and Design by Contract Implementation of Contracts Inheritance and Contracts


slide-1
SLIDE 1

Software Engineering I (02161)

Design by Contract

  • Assoc. Prof. Hubert Baumeister

DTU Compute Technical University of Denmark

Spring 2020

slide-2
SLIDE 2

◮ Contracts and Design by Contract ◮ Implementation of Contracts ◮ Inheritance and Contracts ◮ Defensive Programming

slide-3
SLIDE 3

What does this function do?

public List<Integer> f(List<Integer> list) { if (list.size() <= 1) return list; int p = list.elementAt(0); List<Integer> l1 = new ArrayList<Integer>(); List<Integer> l2 = new ArrayList<Integer>(); List<Integer> l3 = new ArrayList<Integer>(); g(p,list,l1,l2,l3); List<Integer> r = f(l1); r.addAll(l2); r.addAll(f(l3)); return r; } public void g(int p, List<Integer> list, List<Integer> l1, List<Integer> l2, List<Integer> l3) { for (int i : list) { if (i < p) l1.add(i); if (i == p) l3.add(i); if (i > p) l2.add(i); } }

slide-4
SLIDE 4

Please stop the video and try to find the answer Continue with the video to see the solution

slide-5
SLIDE 5

What does this function do?

public List<Integer> f(List<Integer> list) { if (list.size() <= 1) return list; int p = list.elementAt(0); List<Integer> l1 = new ArrayList<Integer>(); List<Integer> l2 = new ArrayList<Integer>(); List<Integer> l3 = new ArrayList<Integer>(); g(p,list,l1,l2,l3); List<Integer> r = f(l1); r.addAll(l2); r.addAll(f(l3)); return r; } public void g(int p, List<Integer> list, List<Integer> l1, List<Integer> l2, List<Integer> l3) { for (int i : list) { if (i < p) l1.add(i); if (i == p) l3.add(i); if (i > p) l2.add(i); } }

slide-6
SLIDE 6

What does this function do?

public void testEmpy() { int[] a = {}; List<Integer> r = f(Array.asList(a)); assertTrue(r.isEmpty()); } public void testOneElement() { int[] a = { 3 }; List<Integer> r = f(Array.asList(a)); assertEquals(Array.asList(3),r); } public void testTwoElements() { int[] a = {2, 1}; List<Integer> r = f(Array.asList(a)); assertEquals(Array.asList(1,2),r); } public void testThreeElements() { int[] a = {2, 3, 1}; List<Integer> r = f(Array.asList(a)); assertEquals(Array.asList(1,2,3),r); } ...

slide-7
SLIDE 7

What does this function do?

List<Integer> f(List<Integer> a) Precondition: a is not null Postcondition: For all objects result and a of type List<Integer>: result == f(a) if and only if

p1(result) and p2(a,result)

where

p1(a) if and only if for all 0 ≤ i, j < a.size():

i ≤ j implies a.get(i) ≤ a.get(j)

and p2(a,b) if and only if for all i ∈ Integer: count(a, i) = count(b, i)

slide-8
SLIDE 8

What does this method do? Comparision

Looking at the implementation ◮ Needs to understand the algorithm → Names help → Use intention revealing names

slide-9
SLIDE 9

What does this method do? Comparision

Looking at the implementation ◮ Needs to understand the algorithm → Names help → Use intention revealing names Looking at the tests ◮ Focus on what the system is doing not how → No knowledge of the algorithm is needed ◮ Uses examples (test cases) to describe the behaviour → extract common behaviour from examples

slide-10
SLIDE 10

What does this method do? Comparision

Looking at the implementation ◮ Needs to understand the algorithm → Names help → Use intention revealing names Looking at the tests ◮ Focus on what the system is doing not how → No knowledge of the algorithm is needed ◮ Uses examples (test cases) to describe the behaviour → extract common behaviour from examples Looking at its contract ◮ Focus on what the system is doing not how → No knowledge of the algorithm is needed → Describes the external behaviour in a compact form ◮ Proof that the implementation satisfies the contract

slide-11
SLIDE 11

Design by contract

◮ Pre- and post conditions: Tony Hoare 1969

◮ Semantics of statements/method, like x := 3 + y ◮ {Pre}Statements{Post}: ◮ If Pre is true and Statements are executed, then Post is true. ◮ {y = 4}x := 3 + y{x = 7} ◮ {true}x := 3 + y{x = 3 + y}

slide-12
SLIDE 12

Design by contract

◮ Pre- and post conditions: Tony Hoare 1969

◮ Semantics of statements/method, like x := 3 + y ◮ {Pre}Statements{Post}: ◮ If Pre is true and Statements are executed, then Post is true. ◮ {y = 4}x := 3 + y{x = 7} ◮ {true}x := 3 + y{x = 3 + y}

◮ Design by contract: Bertrand Meyer 1988

◮ Pre- and post conditions in the context of object-orientation ◮ Contract between Caller and the Method

◮ Caller ensures precondition ◮ Method ensures postcondition

→ If the client violates precondition (the contract), then the method does not have to guarantee the postcondition → The method does not have to check the precondition!!

slide-13
SLIDE 13

Example Counter

Counter inc() : void dec() : void i : int {context Counter inv: i >= 0} {context Counter :: inc ( ) post: i = i@pre + 1} {context Counter :: dec ( ) pre: i > 0 post: i = i@pre - 1 }

◮ Caller of c.dec() has to ensure pre-condition ◮ Ex. c.i == 3 ◮ Caller can assume post-condition ◮ c.dec() → c.i == 2

slide-14
SLIDE 14

Example Counter

Counter inc() : void dec() : void i : int {context Counter inv: i >= 0} {context Counter :: inc ( ) post: i = i@pre + 1} {context Counter :: dec ( ) pre: i > 0 post: i = i@pre - 1 }

◮ Caller of c.dec() has to ensure pre-condition ◮ Ex. c.i == 3 ◮ Caller can assume post-condition ◮ c.dec() → c.i == 2 ◮ Pre-Condition violated ◮ c.i == 0 ◮ Post-condition is not guaranteed ◮ c.dec() → undefined

... if (c.i > 0) { c.dec(); } ...

slide-15
SLIDE 15

MinMax Example

Code

public class MinMax { int min, max; public void minmax(int[] array) { min = max = array[0]; for (int i = 1; i < array.length; i++) { int obs = array[i]; if (obs > max) max = obs; else if (min < obs) min = obs; } } }

slide-16
SLIDE 16

Please stop the video and try to find the answer Continue with the video to see the solution

slide-17
SLIDE 17

MinMax Example

Code

public class MinMax { int min, max; public void minmax(int[] array) { min = max = array[0]; for (int i = 1; i < array.length; i++) { int obs = array[i]; if (obs > max) max = obs; else if (min < obs) min = obs; } } }

Contract

public void minmax(int[] array) { pre: array != null && array.length > 1 post: array.contains(min) && array.contains(max) && forall i in array: min <= i <= max

slide-18
SLIDE 18

MinMax Example

Code

public class MinMax { int min, max; public void minmax(int[] array) { min = max = array[0]; for (int i = 1; i < array.length; i++) { int obs = array[i]; if (obs > max) max = obs; else if (min < obs) min = obs; } } }

Contract

public void minmax(int[] array) { pre: array != null && array.length > 1 post: array.contains(min) && array.contains(max) && forall i in array: min <= i <= max

Client checks pre-condition

if (array != null && array.length > 1) { MinMax mm = new MinMax().minmax(array) ... }

slide-19
SLIDE 19

LibraryApp Example:

Code

public void addMedium(Medium medium) { mediumRepository.addMedium(medium); }

slide-20
SLIDE 20

LibraryApp Example:

Code

public void addMedium(Medium medium) { mediumRepository.addMedium(medium); }

Contract

public void addMedium(Medium medium) { pre: adminLoggedIn; post: mediumRepository.allMedia() == mediumRepository@pre .allAllMedia() .add(medium); }

Checking the pre-condition

if (libApp.adminLoggedIn()) { libApp.addMedium(medium); }

slide-21
SLIDE 21

LibraryApp Example:

Code

public List<Medium> search(String searchText) { List<Medium> found = new ArrayList<>(); for (Medium m : mediumRepository.getAllMedia) { if (b.match(searchText)) { found.add(m); } } return found; }

slide-22
SLIDE 22

LibraryApp Example:

Code

public List<Medium> search(String searchText) { List<Medium> found = new ArrayList<>(); for (Medium m : mediumRepository.getAllMedia) { if (b.match(searchText)) { found.add(m); } } return found; }

Contract

public List<Medium> search(String searchText) { post result == { m | m in mediumRepository.getAllMedia() && m.match(searchText) } }

slide-23
SLIDE 23

Postcondition

Assume that result denotes the result of the function f(x : double) : double. 1) post: result2 = x 2) post: result = x2 3) post: x2 = result 4) post: x = result2 Which of this statements describe a the postcondition of the square function? b the postcondition of the square root function?

slide-24
SLIDE 24

Please stop the video and try to find the answer Continue with the video to see the solution

slide-25
SLIDE 25

Postcondition

Assume that result denotes the result of the function f(x : double) : double. 1) post: result2 = x 2) post: result = x2 3) post: x2 = result 4) post: x = result2 Which of this statements describe a the postcondition of the square function? b the postcondition of the square root function?

slide-26
SLIDE 26

Precondition

◮ Given the contract for a method minmax(int[]array) in a class which has instance variables min and max of type int:

pre: array = null and array.length > 0 post: array.contains(min) && array.contains(max) && ∀i ∈ array : min ≤ i ≤ max

◮ Which of the following statements is true: if the client calls minmax such the precondition is not satisfied

a) A NullPointerException is thrown b) An IndexOutOfBoundsException is thrown c) Nothing happens d) What happens depends on the implementation of minmax

slide-27
SLIDE 27

Please stop the video and try to find the answer Continue with the video to see the solution

slide-28
SLIDE 28

Precondition

◮ Given the contract for a method minmax(int[]array) in a class which has instance variables min and max of type int:

pre: array = null and array.length > 0 post: array.contains(min) && array.contains(max) && ∀i ∈ array : min ≤ i ≤ max

◮ Which of the following statements is true: if the client calls minmax such the precondition is not satisfied

a) A NullPointerException is thrown b) An IndexOutOfBoundsException is thrown c) Nothing happens d) What happens depends on the implementation of minmax