Kotlin, Practically Speaking
Who am I?
Andrew Watson
Senior Android developer @ Microsoft - Yammer team
What I'm going to talk about
We've been using Kotlin for 1½ years, and it's great!
- The path we took to get started
- Practical lessons we've learned along the way
- Particularly re: migrating and converting an existing codebase
Getting the party started at Yammer
May, 2017
- Personally I had been using "not-official" Kotlin since 2015
- I/O 2017: Google FTW!
- Our team was understandably mixed
Taking our time
- Didn't make anyone do Kotlin right away
- Book club for 6 months
- "Kotlin in Action" by Jemerov and Isakova
- We didn't outright block the usage of it though
Our next steps
- Wrote a draft of Kotlin coding standards
- Converting unit/Espresso tests was good practice
- Eventually: all new code to be written in Kotlin
A word on buy-in from higher-ups
- Thankfully we had the freedom to do this
- I can tell you intuitively things are better
- Official metrics would be very nice
- Maybe this talk could help you get buy-in!
But I will say one thing...
It's not Swift
- No 100mb runtime
- No impact on app build time (right?)
- Complete interop with existing code
- Impact is more like a library than a new tech stack
Lesson:
The auto-converter leaves work for you to do 🤷
An example:
Regarding Companion objects
Converted:
Consumed in Java:
The right way:
New consumption:
Lesson:
Converting to Kotlin can introduce crashes
Most often related to null values
Lesson:
Kotlin conversion will wipe all Git history 🙀
A beautiful history...
Gone in an instant!
This is actually due to the way Git works
Not the auto-converter's fault
So how do we actually do this?
- Do an initial auto Kotlin conversion
- Clean up all the auto convert issues, ensure app builds
- Copy the converted contents to a scratch file
- Revert your changes
- Rename **just** the file name to *.kt
- Commit! Your build is mega broken.
- Immediately paste in your converted Kotlin code
- Commit a 2nd time. Now you are converted! Ensure build.
Plz don't squash!
Lesson:
Your custom lint rules don't apply here
(But probably need reviewing anyway)
Goodbye JavaScanner, hello UastScanner
Just kidding, SourceCodeScanner
Lesson:
No really, don't use !!
- Don't follow the auto-converter's example
- With rare exeception, if you're using !!, you're doing it wrong
- Short-circuits Kotlin's nullability features
- Our PR rule: If there's a !!, explain yourself!
Lesson:
Your Kotlin files can share a 🏡 with your Java files
- Initially we created two src dirs - java/ and kotlin/
- Made conversion painful - had to recreate directory structures
- After a while, we were totally comfortable with .java and .kt side-by-side
Lesson:
Some files just can't be converted
- The older the file, the more likely you can't convert
- Base classes (protected fields, getters/setters, etc)
- Assumptions about nullability and/or using null as data "state"
Lesson:
Death to Utils! Death to Helpers!
Long live Extensions!
Organize by class type
Oh and functions too. Just, functions.
Lesson:
Evolve your coding standards
- Usage of "it"
- Declaring type when we really didn't need to
You really can start thinking different! Better!
A final word
Kotlin conversion != eliminating tech debt
You need to be sober about it
But yes, it's worth it!