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

Seattle Kotlin: Adopting Kotlin

Nate Ebel
February 20, 2018

Seattle Kotlin: Adopting Kotlin

Nate Ebel

February 20, 2018
Tweet

More Decks by Nate Ebel

Other Decks in Programming

Transcript

  1. Adopting Kotlin
    A case study in team buy-in and adoption
    © @n8ebel, Udacity Engineering 2018 1

    View Slide

  2. What Is Kotlin?
    » a programming language
    developed by JetBrains
    » targets the JVM
    » is 100% compatible with
    Java for Android
    development
    © @n8ebel, Udacity Engineering 2018 2

    View Slide

  3. Where Is It Used?
    » Android
    » JVM
    » Browser
    » Backend
    » iOS
    © @n8ebel, Udacity Engineering 2018 3

    View Slide

  4. Adoption at Udacity
    © @n8ebel, Udacity Engineering 2018 4

    View Slide

  5. Production Today
    Have been using in production
    for over a year
    Today, Kotlin is our primary
    development language for
    Android
    © @n8ebel, Udacity Engineering 2018 5

    View Slide

  6. Why Invest In Kotlin?
    » Our adoption process
    » How are we using Kotlin today
    » Tips for incorporating Kotlin into your project
    © @n8ebel, Udacity Engineering 2018 6

    View Slide

  7. Why Kotlin?
    © @n8ebel, Udacity Engineering 2018 7

    View Slide

  8. “Kotlin provides a more
    enjoyable developer
    experience by allowing us
    to express our features &
    ideas using safer, more
    concise code. This, in turn,
    enables us to build a better
    product for our users”
    © @n8ebel, Udacity Engineering 2018 8

    View Slide

  9. Key Benefits
    » Improved crash rate
    » Reduction in lines of code written
    » Decreased time spent in code review
    » Renewed excitement around experimentation and
    innovation
    © @n8ebel, Udacity Engineering 2018 9

    View Slide

  10. Ultimately, our goal is to
    provide our users with the
    best learning experience
    possible.
    © @n8ebel, Udacity Engineering 2018 10

    View Slide

  11. We believe Kotlin helps us do that.
    © @n8ebel, Udacity Engineering 2018 11

    View Slide

  12. How Did We Start?
    © @n8ebel, Udacity Engineering 2018 12

    View Slide

  13. Recognition of Pain Points
    We started to notice patterns in our code reviews
    » “we need to be consistent in our handling of null”
    » “this code could be shortened using a more
    functional approach”
    © @n8ebel, Udacity Engineering 2018 13

    View Slide

  14. Recognition of Pain Points
    Having experimented with Kotlin outside Udacity, we
    knew the language could help with some of these
    issues
    © @n8ebel, Udacity Engineering 2018 14

    View Slide

  15. Kotlin, Meet the Team
    » built a small new feature
    » create both a Java & Kotlin
    PR
    » demonstrated that Kotlin
    could be easily integrated
    © @n8ebel, Udacity Engineering 2018 15

    View Slide

  16. Jumping Off Point
    » Set out to migrate our Settings screen
    » During testing, we looked closely for 2 things
    © @n8ebel, Udacity Engineering 2018 16

    View Slide

  17. Jumping Off Point
    1.Usability issues: what impact did Kotlin have on
    crashes/bugs?
    2.Development process: did Kotlin cause CI issues,
    slow down our build, or slow down feature
    development?
    © @n8ebel, Udacity Engineering 2018 17

    View Slide

  18. Ship It
    As expected, no significant
    issues found
    Released to production
    © @n8ebel, Udacity Engineering 2018 18

    View Slide

  19. Slow Down
    At this point, we slowed down a bit.
    We loved using Kotlin, but recognized the potential
    risks of going all-in on an unofficially supported
    language.
    We continued to add/convert some tests to Kotlin, but
    no new feature code was using it
    © @n8ebel, Udacity Engineering 2018 19

    View Slide

  20. Game Changer
    © @n8ebel, Udacity Engineering 2018 20

    View Slide

  21. Google I/O
    The Google I/O announcement
    of official support for
    Kotlin was the push we needed
    We decided immediately that
    we wanted to go all-in on
    Kotlin
    © @n8ebel, Udacity Engineering 2018 21

    View Slide

  22. Team Buy In
    Met with our PM and expressed our desire/plan to
    adopt Kotlin.
    We were met with a healthy dose of pragmatism.
    © @n8ebel, Udacity Engineering 2018 22

    View Slide

  23. “What’s the value prop?”
    » Is the language stable/safe to be using now?
    » Will it slow down development?
    » Are there tangible benefits?
    © @n8ebel, Udacity Engineering 2018 23

    View Slide

  24. Migration Plan
    © @n8ebel, Udacity Engineering 2018 24

    View Slide

  25. Migration Plan
    1.All new dev work in Kotlin
    2.If anything larger than a small bug fix must be
    done to a class, consider migrating the class to
    Kotlin
    3.Start migrating existing code
    © @n8ebel, Udacity Engineering 2018 25

    View Slide

  26. Migration Plan
    Planned to tackle the low hanging fruit first
    (utilities, helpers, isolated classes)
    Work towards migrating enough code that the majority
    of our daily development would be in Kotlin
    © @n8ebel, Udacity Engineering 2018 26

    View Slide

  27. Migration Process
    » Started by using the Java to Kotlin conversion
    tool within Android Studio
    » Then made manual adjustments to the converted code
    © @n8ebel, Udacity Engineering 2018 27

    View Slide

  28. Migration Process
    Some of the adjustments we made to converted code:
    » Avoiding the use of !! operator
    » When possible, convert helper methods to top-level
    functions
    » Avoid companion object where possible
    » Reorganize the generated code
    » Expressing code in a more Kotlin idiomatic way
    © @n8ebel, Udacity Engineering 2018 28

    View Slide

  29. Handling null
    In many places we focused on leveraging statements
    such as
    object?.let {...}
    instead of
    if(object != null) { ... }
    © @n8ebel, Udacity Engineering 2018 29

    View Slide

  30. stdlib
    Tried to leverage the great collections stdlib that
    Kotlin provides.
    Allowed us to replace large blocks of code with a few
    lines of concise functional code
    © @n8ebel, Udacity Engineering 2018 30

    View Slide

  31. Shorter, safer code
    val recentQueries:List? = listOf()
    val recentsToDisplay = recentQueries
    ?.filterNotNull()
    ?.filter{ it.isNotBlank() }
    ?.take(5)
    ?: listOf()
    © @n8ebel, Udacity Engineering 2018 31

    View Slide

  32. Where Are We Today?
    © @n8ebel, Udacity Engineering 2018 32

    View Slide

  33. Flagship App
    Our flagship codebase continues to be migrated to
    Kotlin
    » ~40% Kotlin today
    » All new dev work is done in Kotlin
    © @n8ebel, Udacity Engineering 2018 33

    View Slide

  34. New Codebase
    Additional code base that is 100% Kotlin
    » Allowed us to fully explore the language
    » Have began using several language features
    extensively
    © @n8ebel, Udacity Engineering 2018 34

    View Slide

  35. New Codebase
    Began leveraging more interesting aspects of the
    language
    » data classes
    » sealed classes
    » named arguments & default parameter values
    » higher order functions
    © @n8ebel, Udacity Engineering 2018 35

    View Slide

  36. Tips for Incorporating Kotlin
    © @n8ebel, Udacity Engineering 2018 36

    View Slide

  37. Tips for Incorporating Kotlin
    » If new to the language, start with the conversion
    tool
    » Don’t be afraid to further modify the converted
    code
    © @n8ebel, Udacity Engineering 2018 37

    View Slide

  38. Tips for Incorporating Kotlin
    Start with tests
    - can help work out build issues
    - explore basic syntax
    - great for understanding interop story
    Features
    - writing feature code will give a more comprehensive
    picture of how it can affect your code
    © @n8ebel, Udacity Engineering 2018 38

    View Slide

  39. Tips for Incorporating Kotlin
    Don’t go wild trying to write “idiomatic” Kotlin.
    - As you progress with the language, it’s likely your
    opinion of whats readable vs what’s possible will
    evolve
    © @n8ebel, Udacity Engineering 2018 39

    View Slide

  40. “Just because you can doesn’t mean you should”
    © @n8ebel, Udacity Engineering 2018 40

    View Slide

  41. Tips for Incorporating Kotlin
    » As you migrate existing code, re-visit previously
    converted code from time to time. You may find
    that newly discovered conventions/patterns could
    be applied.
    » Don’t forget to thoroughly test any interop layers
    between Java and Kotlin. Nullability issues can
    crop up during runtime when dealing with nullable
    platform types.
    © @n8ebel, Udacity Engineering 2018 41

    View Slide

  42. “Explore, experiment, migrate, refactor”
    © @n8ebel, Udacity Engineering 2018 42

    View Slide

  43. How Can Kotlin Improve Your Product?
    take advantage of this period of exploration while
    learning the language
    © @n8ebel, Udacity Engineering 2018 43

    View Slide

  44. How Can Kotlin Improve Your Product?
    Question existing patterns/conventions for writing
    Android apps and ask yourself if/how Kotlin might
    enable you to write alternative solutions
    © @n8ebel, Udacity Engineering 2018 44

    View Slide

  45. How Can Kotlin Improve Your Product?
    » can your code be refactored to avoid null
    properties?
    » can you replace helper classes/methods with top-
    level functions?
    » can you avoid handling null properties by using a
    delegate?
    » could a logical path be expressed using functional
    concepts to reduce a class’s state?
    © @n8ebel, Udacity Engineering 2018 45

    View Slide

  46. Moving Forward
    © @n8ebel, Udacity Engineering 2018 46

    View Slide

  47. Confident Moving Forward
    We feel great about our investment into Kotlin
    » Google continues to poor resources into Kotlin
    » Samples now defaulting to Kotlin
    » New ktx library for Android
    » Increased interest from users will accelerate
    improvements
    © @n8ebel, Udacity Engineering 2018 47

    View Slide

  48. More From Udacity
    Free Course
    - Kotlin for Android Developers
    Engineering Blog Posts*
    - Adopting Kotlin
    - Learning Kotlin by Mistake
    - Modeling ViewModel States Using Kotlin’s Sealed
    Classes
    © @n8ebel, Udacity Engineering 2018 48

    View Slide