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

Type Safety in an Unsafe World with Kotlin

Ce0de7299bfbc54dd950689e04a4f053?s=47 Cody Engel
February 02, 2021

Type Safety in an Unsafe World with Kotlin

Ce0de7299bfbc54dd950689e04a4f053?s=128

Cody Engel

February 02, 2021
Tweet

Transcript

  1. Type Safety in an Unsafe World with Kotlin Cody Engel,

    Staff Software Engineer @ PayPal
  2. What is Type Safety? It is kind of confusing. Photo:

    Matthew Hamilton via Unsplash
  3. Robin Milner, A Theory of Type Polymorphism in Programming “Well

    typed programs cannot go wrong.”
  4. Going Wrong Syntactically valid, but meaningless. Type Safety by Michael

    Hicks - http://bit.ly/ple-type-safety
  5. Well Typed It may crash, but that is by design

    in Kotlin. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
  6. Well Typed, Continued This is by design in Ruby and

    may also crash. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
  7. Cody Engel, Type Safety in an Unsafe World with Kotlin

    “Some languages have stricter behavior than others.”
  8. What is Kotlin? It is a strict, yet concise programming

    language. Photo: Marc Reichelt via Unsplash
  9. Define Types Name can be inferred to be a String.

  10. Define Mutability A variable defined as “var” can change.

  11. Define Structures Structured data is more predictable.

  12. Define Nullability Nullability is opt-in.

  13. Define Concurrency Coroutines make concurrency simpler.

  14. And The Unsafe World? That would be our APIs. Photo:

    Jason Yoder via Unsplash
  15. is_admin As a boolean it works well. Unfortunately, this wasn’t

    how it was actually modeled.
  16. is_admin Our permissions class was never safer! However this is

    how we would model that information in Kotlin.
  17. is_admin Okay, technically this isn’t too bad. Although, it is

    not the best way to model a boolean with JSON.
  18. is_admin Extension functions to the rescue. This could also be

    an extension property, but extension functions look better in Keynote
  19. is_admin Strings can also represent numeric values which can represent

    booleans. I was pretty excited to make use of extension functions, so it was fine.
  20. is_admin Unfortunately String already has a toBoolean function. And that

    function is for converting “true” and “false” into a boolean
  21. is_admin I can’t recall if this was actually possible with

    the API. However there is a good chance it could have been possible based on customer configurations.
  22. is_admin Fortunately, it isn’t too difficult to add. Nonetheless, it’d

    be great if this wasn’t required in the first place
  23. is_admin It was nullable though. That’s okay though, nullability is

    actually pretty simple with Kotlin.
  24. is_admin Updating our extension function to be on a nullable

    Any works. Since we type cast on non-null types, the else branch catches our null case.
  25. Making The World a Safer Place How to design safer

    APIs. Photo: Matthew Rumph via Unsplash
  26. Stick to a strict API when rolling your own specification.

  27. Use an industry standard specification, such as Open API.

  28. Look at JSON alternatives such as Protocol Buffers.

  29. Let’s build some great and safe APIs. • YouTube -

    bit.ly/cody-yt • Twitter - bit.ly/cody-twitter • Medium - bit.ly/cody-medium Thanks!