1. React
  2. ReactiveConf
  3. 2018
  4. Programming Safely in an Uncertain World

Programming Safely in an Uncertain World

David Chambers at ReactiveConf 2018

The world is an uncertain place. If we write programs that interact with the outside world—sometimes known as useful programs—we must acknowledge the possibility of failure. Every time we request a resource over the network, parse user input, or consume the output of another program, it's possible that the operation will not succeed. Imperative languages give us a few options in such cases: raising an exception, returning an error code, or succeeding with a bogus value such as NaN. Algebraic data types such as Maybe, Either, and Future allow us to deal with uncertainty in a principled, unified manner: no more conditional logic; no more exceptions. Instead, we get simple building blocks that snap together. In conjunction with map and flat-map, algebraic data types allow us to collapse multiple sources of uncertainty into a single success or failure value. The decision about what to do in the case of an error is left to the caller, allowing the same error to be handled differently in different contexts. JavaScript is not typically considered a functional language, but it is possible to define and use algebraic data types in JavaScript. This presentation will introduce the Maybe type in a JavaScript setting, and show how it can be used to write code that is dramatically simpler than the equivalent imperative code. The concepts learned during the presentation will be directly applicable to true functional languages such as Haskell.