34
DESIGN BY CONTRACT IN ELIXIR “LET IT CRASH” MEETS “IT SHOULDN’T CRASH”

Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

  • Upload
    others

  • View
    24

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

D E S I G N B Y C O N T R A C T I N E L I X I R

“ L E T I T C R A S H ” M E E T S “ I T S H O U L D N ’ T C R A S H ”

Page 2: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

G U I L L E R M O I G U A R Á N S U A R E Z R I D E . C O M

@ G U I L L E I G U A R A N

E L B A S Á N C H E Z M Á R Q U E Z R I D E . C O M

@ E L B A S A N C H E Z M

Page 3: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

B U G S A N D C R A S H E S

H O W D I D W E G E T H E R E

Page 4: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

D I D Y O U K N O W ?

• There are really expensive software errors

Page 5: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

"Ariane 501 Cluster" by Phrd/de:user:Stahlkocher - Own work. Licensed under CC BY-SA 3.0

A R I A N E 5 F L I G H T 5 0 1

- T H E A R I A N E 5 ’ S FA S T E R E N G I N E S E X P L O I T E D A B U G T H A T W A S N O T F O U N D I N P R E V I O U S M O D E L S .

- T H E S O F T W A R E H A D T R I E D T O C R A M A 6 4 - B I T N U M B E R I N T O A 1 6 - B I T S PA C E .

- T H E R E W A S N O E X P L I C I T E X C E P T I O N H A N D L E R T O C A T C H T H E E X C E P T I O N , S O I T F O L L O W E D T H E U S U A L FA T E O F U N C A U G H T E X C E P T I O N S A N D C R A S H E D T H E E N T I R E S O F T W A R E , H E N C E T H E O N B O A R D C O M P U T E R S , H E N C E T H E M I S S I O N

Page 6: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

B L A M E M A N A G E M E N T

Y O U C A N ’ T

B L A M E T H E L A N G U A G E

Y O U C A N ’ T

B L A M E T H E I M P L E M E N TAT I O N

Y O U C A N ’ T

B L A M E T E S T I N G

Y O U C A N ’ T

Page 7: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

T H E R E U S E S P E C I F I C AT I O N

Y O U H A V E T O B L A M E

The requirement that the horizontal bias should fit on 16 bits was in fact

stated in an obscure part of a mission document. But it was nowhere to be found in

the code itself!

Page 8: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

A L I T T L E B I T O F T H E O R Y

Page 9: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

H I S T O R Y

• The pre- and postcondition technique originated with the work of Tony Hoare, whose 1969 Communications of the ACM paper described program semantics using such assertions.

• The term was coined by Bertrand Meyer in connection with his design of the Eiffel programming language

Design by contract has its roots in work on formal verification, formal

specification and Hoare logic.

Page 10: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

B A S I C S O F H O A R E L O G I C

• Formal reasoning about program correctness using pre- and postconditions

Hoare Logic is at the core of the deductive approach of the DbC.

Page 11: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

• Typically unit tests are used to verify that the software works c o r re c t l y u n d e r c e r t a i n example cases but hardly can be used to detect all possible e d g e c a s e s d u r i n g development.

• Design by contract (DbC) is a s o f t w a r e c o r r e c t n e s s methodology that document and programmatically assert the change in state caused by a piece of a program

“Design by Contract” falls under Implementation/Design

Page 12: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

I N A R I A N E ’ S C A S E

Where the precondition (require…) states clearly and precisely what the input must satisfy to be acceptable.

Eiffel Version

Page 13: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

W H AT I S D E S I G N B Y C O N T R A C T ?

• Each party benefits and accepts obligations

• One party’s benefits are the other party’s obligation

• It is described so that both parties understand what would be guaranteed without saying how.

O B L I G AT I O N S R I G H T S

PA S S E N G E R

B U Y A I R L I N E T I C K E T, B R I N G

A C C E P T E D B A G G A G E A N D

B E AT A I R P O R T 2 H O U R S B E F O R E

R E A C H D E S T I N AT I O N

A I R L I N EB R I N G

PA S S E N G E R T O D E S T I N AT I O N

- N O N E E D T O C A R R Y

PA S S E N G E R W H O I S L AT E

- O R H A S U N A C C E P TA BL E B A G G A G E

- O R H A S N ’ T PA I D T I C K E T

Page 14: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

P R E C O N D I T I O N ( R E Q U I R E S C L A U S E ) P O S T C O N D I T I O N ( E N S U R E S C L A U S E )

• If its precondition is true when a method is called, then the method will terminate return to the calling program — and the postcondition will be true when it does return.

• If its precondition is not true when a method is called, then the method may do nothing

S T R U C T U R E O F A C O N T R A C T

Page 15: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

https://www.eiffel.com/values/design-by-contract/introduction/

Page 16: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

H O W C A N I T H E L P ?

Page 17: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

– K . F U J I N O V I C E P R E S I D E N T O F N E C C O R P O R AT I O N ’ S C & C S O F T W A R E D E V E L O P M E N T

G R O U P

“When quality is pursued, productivity follows.”

Page 18: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

A N D A L S O W E L O O K F O R …

R E L I A B I L I T Y

Correctness Robustness

Page 19: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

- Assertions (preconditions and postconditions

in particular) can be automatically turned on during testing

- Assertions can remain turned on during

execution, triggering an exception if violated

- Most important, assertions are a prime component of the software

and its automatically produced documentation

Page 20: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

L A N G U A G E S U P P O R T

• There are several implementations of DbC libraries for some languages

• And languages with native support

Page 21: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

A N D N O W W I T H E L I X I R

Page 22: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

M E TA P R O G R A M M I N GI N E L I X I R

Bo

ok

by

Chr

is M

cCo

rd -

O'R

eilly

Page 23: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

M A C R O S R U L E S

• Rule 1: Don’t write Macros

Bo

ok

by

Chr

is M

cCo

rd -

O'R

eilly

Page 24: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

M A C R O S R U L E S

• Rule 2: Use Macros Gratuitously

Bo

ok

by

Chr

is M

cCo

rd -

O'R

eilly

Page 25: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

M A C R O S

• A macro is code that writes code

• Many constructs in Elixir are macros (def, if, unless, defmodule,…)

• Elixir code runs at compile time and can be used to manipulate language AST.

Page 26: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

M E TA P R O G R A M M I N G E L I X I R B Y C H R I S M C C O R D

Bo

ok

by

Chr

is M

cCo

rd -

O'R

eilly

Page 27: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

B A C K T O D B C …

• We will use Elixir macros to extend the language adding support for basic DbC constructs.

• We will tag existing functions with “requires” and “ensures” tags.

• Macros will manipulate function body to insert precondition and postconditions inside of functions.

Page 28: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

W H AT W E H A D T O D O

Page 29: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

D E M O

Page 30: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

H T T P S : / / G I T H U B . C O M /E P S A N C H E Z M A / E L I X I R - C O N T R A C T S

Page 31: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

F U R T H E R W O R K

• Generate test-cases from Contracts

• Add configuration options to turn-on/off contracts in development and production

• Generate automated documentation from contracts

• Generate QuickCheck tests

Page 32: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

T O C O N C L U D E

• Design by contract does not replace regular testing strategies

• Contracts add an extra grade of reliability

• It’s not a silver bullet

Page 33: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

R E F E R E N C E S

• Ariane’s case: http://se.inf.ethz.ch/~meyer/publications/computer/ariane.pdf

• DbC History: http://c2.com/cgi/wiki?DesignByContract

• Hoare Logic: https://www.cs.cmu.edu/~aldrich/courses/654-sp07/slides/7-hoare.pdf

• DbC: http://ansymore.uantwerpen.be/system/files/uploads/courses/SE3BAC/06DesignContract.pdf, http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/09.Design-by-Contract.pdf

• Examples: https://www.eiffel.com/

Page 34: Design by contracts...METAPROGRAMMING IN ELIXIR d - ly. MACROS RULES •Rule 1: Don’t write Macros d - ly. MACROS RULES •Rule 2: Use Macros Gratuitously d - ly. MACROS • A macro

D A N K E S C H Ö N