Handling Null in Functional Programming

969c21ccc008d9df9cb2fd8bd2bc5723?s=47 Evelyn Masso
February 02, 2019

Handling Null in Functional Programming

Nulls are a fact of life—sometimes a value can be absent, which is important information to have However if a variable can be null, we often have to write extra code to handle that possibility. How can we utilize functional, typed languages to only allow null where it communicates important information and eliminate it everywhere else? This talk will identify patterns to do just that and discuss how they can help shape your application’s architecture.

Given at Empex LA (http://empex.co/la.html)

969c21ccc008d9df9cb2fd8bd2bc5723?s=128

Evelyn Masso

February 02, 2019
Tweet

Transcript

  1. Handling Null Evelyn Masso (@outofambit)

  2. None
  3. What is “null”?

  4. “not any” in latin

  5. also known as nil, nothing, none

  6. type nullable<T> = null | T option type

  7. option type

  8. type nullable<Number> = null | Number option type

  9. function(amount: nullable<Number>) { if(amount !=="" null) { // do something


    } else { // ???? } } using an optional number
  10. function(amount: nullable<Number>) { if(amount !=="" null) { // do something


    } else { // ???? } } using an optional number
  11. Dropdown menu state

  12. None
  13. Peeta Gale

  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. The tradeoff

  21. When to use null • when something can optionally exist

    • instead of using a number (like -1) or a boolean type union • for data structures (but then check for it as soon as possible in business logic)
  22. When to avoid null? • there are multiple ways something

    can not exist (that you care about) • future proofing your data model
  23. None
  24. Alternatives to null

  25. enum People { Peeta, Gale, Hermione, } explicit cases enum

    Options { NoOne, SomeoneElse, Unknown }
  26. type DatingOptions = People | Options explicit cases

  27. default values enum Dates = { Peeta, Gail, Hermione, None

    } type Availables = Dates function(date: Availables = Dates.None) { // do something }
  28. The tradeoff

  29. None
  30. Thanks! (@outofambit)