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

More Decks by Nate Ebel

Other Decks in Programming


  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. 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
  5. 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
  6. “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
  7. 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
  8. Ultimately, our goal is to provide our users with the

    best learning experience possible. © @n8ebel, Udacity Engineering 2018 10
  9. 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
  10. 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
  11. 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
  12. Jumping Off Point » Set out to migrate our Settings

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

    production © @n8ebel, Udacity Engineering 2018 18
  15. 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
  16. 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
  17. 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
  18. “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
  19. 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
  20. 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
  21. 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
  22. 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
  23. Handling null In many places we focused on leveraging statements

    such as object?.let {...} instead of if(object != null) { ... } © @n8ebel, Udacity Engineering 2018 29
  24. 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
  25. Shorter, safer code val recentQueries:List<String?>? = listOf() val recentsToDisplay =

    recentQueries ?.filterNotNull() ?.filter{ it.isNotBlank() } ?.take(5) ?: listOf() © @n8ebel, Udacity Engineering 2018 31
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. How Can Kotlin Improve Your Product? take advantage of this

    period of exploration while learning the language © @n8ebel, Udacity Engineering 2018 43
  34. 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
  35. 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
  36. 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
  37. 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