Seattle Kotlin: Adopting Kotlin

Df78ed2e647f52dadaac996721a6bdfb?s=47 Nate Ebel
February 20, 2018

Seattle Kotlin: Adopting Kotlin

Df78ed2e647f52dadaac996721a6bdfb?s=128

Nate Ebel

February 20, 2018
Tweet

Transcript

  1. Adopting Kotlin A case study in team buy-in and adoption

    © @n8ebel, Udacity Engineering 2018 1
  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
  3. Where Is It Used? » Android » JVM » Browser

    » Backend » iOS © @n8ebel, Udacity Engineering 2018 3
  4. Adoption at Udacity © @n8ebel, Udacity Engineering 2018 4

  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
  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
  7. Why Kotlin? © @n8ebel, Udacity Engineering 2018 7

  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
  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
  10. Ultimately, our goal is to provide our users with the

    best learning experience possible. © @n8ebel, Udacity Engineering 2018 10
  11. We believe Kotlin helps us do that. © @n8ebel, Udacity

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

  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
  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
  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
  16. Jumping Off Point » Set out to migrate our Settings

    screen » During testing, we looked closely for 2 things © @n8ebel, Udacity Engineering 2018 16
  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
  18. Ship It As expected, no significant issues found Released to

    production © @n8ebel, Udacity Engineering 2018 18
  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
  20. Game Changer © @n8ebel, Udacity Engineering 2018 20

  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
  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
  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
  24. Migration Plan © @n8ebel, Udacity Engineering 2018 24

  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
  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
  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
  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
  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
  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
  31. Shorter, safer code val recentQueries:List<String?>? = listOf() val recentsToDisplay =

    recentQueries ?.filterNotNull() ?.filter{ it.isNotBlank() } ?.take(5) ?: listOf() © @n8ebel, Udacity Engineering 2018 31
  32. Where Are We Today? © @n8ebel, Udacity Engineering 2018 32

  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
  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
  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
  36. Tips for Incorporating Kotlin © @n8ebel, Udacity Engineering 2018 36

  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
  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
  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
  40. “Just because you can doesn’t mean you should” © @n8ebel,

    Udacity Engineering 2018 40
  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
  42. “Explore, experiment, migrate, refactor” © @n8ebel, Udacity Engineering 2018 42

  43. How Can Kotlin Improve Your Product? take advantage of this

    period of exploration while learning the language © @n8ebel, Udacity Engineering 2018 43
  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
  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
  46. Moving Forward © @n8ebel, Udacity Engineering 2018 46

  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
  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