Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Adoption at Udacity © @n8ebel, Udacity Engineering 2018 4

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Why Kotlin? © @n8ebel, Udacity Engineering 2018 7

Slide 8

Slide 8 text

“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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Game Changer © @n8ebel, Udacity Engineering 2018 20

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

“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

Slide 24

Slide 24 text

Migration Plan © @n8ebel, Udacity Engineering 2018 24

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Moving Forward © @n8ebel, Udacity Engineering 2018 46

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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