Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Type Integrity: The Software Engineering Behind...

Type Integrity: The Software Engineering Behind Stricter Typing

Back in January, PHPSW had two great talks from Rob Allen (@akrabat) and Dave Liddament (@daveliddament) on the static analysis tools that the PHP community has built around stricter typing. If you missed them, you can check them out here: https://joind.in/event/phpsw-jan-2020.

But what is "stricter typing"? What problems does it solve, and how do you use it to solve them?

In this talk, Stuart will introduce you to the fundamental principles involved. He'll show you the basics of "robustness" and "correctness" in your software engineering, the costs of tackling those using defensive programming and Design by Contract(™), and how to use stricter typing to bring those costs right down.

Although the examples are written in PHP, these are basic principles that apply to all software. You don't have to be a PHP programmer to get something out of this talk.

Stuart Herbert

April 08, 2020
Tweet

More Decks by Stuart Herbert

Other Decks in Programming

Transcript

  1. @GanbaroDigital This is a follow-up to the January 2020 talks

    @PHPSW by Rob Allen and Dave Liddament
  2. @GanbaroDigital function calculateVat($amount, $rate) { if (!is_int($amount)) { throw new

    Exception(...); } if (!is_int($rate)) { throw new Exception(...); } return ($amount/100) * $rate; }
  3. @GanbaroDigital We've gone from 1 line of code to 5

    lines of code ... ... and we're just getting started!
  4. @GanbaroDigital We live in a world where the time it

    takes to create and ship working code is often the biggest cost for a project / org / business.
  5. @GanbaroDigital function calculateVat($amount, $rate) { if (!is_int($amount)) { throw new

    Exception(...); } if (!is_int($rate)) { throw new Exception(...); } return ($amount/100) * $rate; }
  6. @GanbaroDigital Remaining Issues Include ... • Generating negative values •

    Locale-specific rules on rounding up / down • Accepting the wrong currency • Accepting invalid VAT rates
  7. @GanbaroDigital Every legal value of $amount and $rate is an

    integer. Not every integer is a legal value of $amount and $rate.
  8. @GanbaroDigital “Defensive programming is like the pandemic lockdown. It only

    works if it is practiced everywhere and by everyone.
  9. @GanbaroDigital Design By Contract ™ is a mid-80s technique to

    mitigate the risks of Object-Oriented Programming.
  10. @GanbaroDigital “ A value is data that has no identity.

    Two values are the same if their state is identical.
  11. @GanbaroDigital “Data that has identity is called an entity. Two

    entities are the same if their identities are identical.
  12. @GanbaroDigital In many languages (including PHP), the only way to

    define a value type is to define a class.
  13. @GanbaroDigital If you're not sure where to start, start with

    the primitive types that you are replacing.
  14. @GanbaroDigital Every legal value of $amount and $rate is an

    integer. Not every integer is a legal value of $amount and $rate.
  15. @GanbaroDigital Type refinement takes a wider data type (like an

    int) and reduces it to a narrower data type (like a VatRate).
  16. @GanbaroDigital class CartAmountToTax { public constructor( int $amount ) {

    // robustness! if ($amount < 0) { throw new Exception(...); } } }
  17. @GanbaroDigital class VatRate { public constructor( string $jurisdiction, int $rate

    ) { // robustness! if (!this->isValidVatRate(...)) { throw new Exception(...); } } }
  18. @GanbaroDigital class VatRate { public constructor( string $jurisdiction, int $rate

    ) { // robustness! if (!this->isValidVatRate(...)) { throw new Exception(...); } } }
  19. @GanbaroDigital class VatRate { public constructor( string $jurisdiction, int $rate

    ) { // robustness! if (!this->isValidVatRate(...)) { throw new Exception(...); } } }
  20. @GanbaroDigital You can't rely on developers checking return values from

    function calls. Never use return values to report an error.
  21. @GanbaroDigital We don't have to repeat the unit tests, because

    we are not repeating the code (the input validation).
  22. @GanbaroDigital ?? ?? Why did we call it CartAmountToTax and

    not something like Currency or Money?
  23. @GanbaroDigital Every legal value of $amount and $rate is an

    integer. Not every integer is a legal value of $amount and $rate.
  24. @GanbaroDigital Every legal value of $amount is Money. Not every

    value of Money is a legal value of $amount.
  25. @GanbaroDigital ?? ?? Why don't we ask the VatRate value

    object for the correct calculation?
  26. @GanbaroDigital We live in a world where the time it

    takes to create and ship working code is often the biggest cost for a project / org / business.
  27. @GanbaroDigital Type refinement takes a wider data type (like an

    int) and reduces it to a narrower data type (like a VatRate).
  28. @GanbaroDigital You can't rely on developers checking return values from

    function calls. Never use return values to report an error.
  29. Thank You How Can We Help You? A presentation by

    @stuherbert
 for @GanbaroDigital