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

Kotlin in production for 2 years, my learnings

Kotlin in production for 2 years, my learnings

Pascal Welsch

June 12, 2017
Tweet

More Decks by Pascal Welsch

Other Decks in Programming

Transcript

  1. Welcome to
    GDG DUS
    July

    View Slide

  2. Kotlin in
    production
    One year later
    Pascal Welsch, grandcentrix GmbH

    View Slide

  3. Our existing JVM languages are great, but none of them have
    demonstrated the potential to become the de facto language of
    choice for a large percentage of JVM developers.
    - Joe Kutner - Heroku

    View Slide

  4. View Slide

  5. How I started with Kotlin
    ● Read the documentation
    ● Did the Kotlin Kohans
    ● Solved a few coding challenges with kotlin
    ○ Google Code Jam
    ○ Advent of Code
    ● Kotlin Slack community is very helpful
    ● Convert Java code to learn how to write it in Kotlin

    View Slide

  6. View Slide

  7. How I started with Kotlin
    ● Started writing new features in Kotlin
    ● Migrated code (mostly manually)

    View Slide

  8. ● Wohin Du Willst - Android App - live
    ○ Existing project written in java
    ● Nearby Debugger - Android App - live
    ○ Small app entirely in kotlin
    ● Internal Technical Prototype - Android App - not released
    ○ 98% in kotlin
    ● GitVersioner Plugin - Gradle Plugin - live
    ○ Written in kotlin
    ● Current Client Project - Android App - not released
    ○ Pure kotlin
    Last years projects

    View Slide

  9. Gradle Plugin
    ● Fast releases when Android Studio upgrades
    ○ Will get better with the official support
    ● Better use

    View Slide

  10. Kotlin major releases
    ● No pain!
    ● Works 99% on day 1
    ● Long EAP and beta program
    ● Tip: Don’t upgrade on day 1, wait for patch 1 or 2

    View Slide

  11. Method count
    ● org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.3-2
    ○ 6317
    ● io.reactivex.rxjava2:rxjava:2.1.1
    ○ 10204
    ● Io.reactivex:rxjava:1.3.0
    ○ 5751

    View Slide

  12. RxJava is harder
    to learn than
    Kotlin

    View Slide

  13. Mixing Kotlin and Java
    ● Calling back and forth works!
    ● NullPointerException still possible when using Java
    libs
    ● Don’t use retrolambda for Interfaces in Kotlin

    View Slide

  14. Lambdas <3
    ● So much better than Java lambdas

    View Slide

  15. Multi dimensional arrays
    ● Manual initialization required was required
    ● Kind of a bummer when starting
    ● New initializer function added in Kotlin 1.1

    View Slide

  16. Data classes

    View Slide

  17. Keep Multithreading in mind

    View Slide

  18. Keep Multithreading in mind

    View Slide

  19. Keep Multithreading in mind

    View Slide

  20. Keep Multithreading in mind

    View Slide

  21. Keep Multithreading in mind

    View Slide

  22. No static
    ● Global variable definitions
    ● Global function definitions
    ● Global objects
    ● Companion objects
    ● Except for Java, most languages have a way to declare
    constants without wrapping them in a class

    View Slide

  23. No static

    View Slide

  24. No SAM conversion for Kotlin
    interfaces
    ● Solutions:
    ○ write two methods, one for kotlin, one for Java
    ○ Return Unit in Java code
    ○ Write Interfaces in Java
    ● Most requested feature in recent developer survey

    View Slide

  25. No SAM
    conversion
    for Kotlin
    interfaces

    View Slide

  26. Properties
    ● No getters, setters required (optional)
    ● Properties can by dynamic

    View Slide

  27. Properties

    View Slide

  28. Optional and named
    Parameters

    View Slide

  29. Annotations processors
    ● Completely unstable. Kapt barely works, only works every
    5. Release
    ● I stopped using Dagger2 -> Migrated to KoDeIn

    View Slide

  30. KoDeIn

    View Slide

  31. Reified Generics
    ● Makes injection easy

    View Slide

  32. Protobuf code generation
    ● compile task order of protobuf plugin for doesn’t work with
    kotlin plugin
    ● Can’t reference Protobuf classes from Kotlin code
    ● Solution: Separate module for Protobuf code required

    View Slide

  33. Bit shifting
    ● Handling low level bits and bytes in Java was annoying
    ● It got worse in Kotlin
    ● Extension functions help a lot
    ● Jetbrains is working on better bit shifting syntax and
    unsigned data types

    View Slide

  34. Extension functions
    ● Use a ActivityExt.kt file for Activity extension function
    ● Use private where possible

    View Slide

  35. Public by default
    ● I would prefer internal by default
    ● I always forget private for properties

    View Slide

  36. Testing library
    ● I’m still using AssertJ
    ○ >5 assertion libs out there, non dominates, not really better
    ● Mockito-kotlin helps with generics

    View Slide

  37. Use IntelliJ for non Android
    projects
    ● AS 2.3 has problems running tests for pure kotlin projects

    View Slide

  38. Adoption of Kotlin at
    grandcentrix
    ● We had a GO from management to use Kotlin in all projects
    one week before it was officially adopted by Google
    ● Now we roll it out in all active projects over the next
    months
    ● Starting with Unit Tests and new features

    View Slide

  39. Coroutines
    ● Beta in Kotlin 1.1
    ● Cool idea for async/await implementations
    ● Useful for server side code to replace Promises
    ● On Android RxJava is a better fit than coroutines

    View Slide

  40. DSL feature
    ● Gradle scripts can be written with kotlin (.kts)

    View Slide

  41. Kotlin libraries
    ● Write Android libraries in Java
    ● Maybe release a separate kt artifact

    View Slide

  42. JVM is stable
    ● I tried Swift with generics and got a segmentation fault
    ● I started to love the JVM

    View Slide

  43. Pascal Welsch
    @passsy
    grandcentrix.net
    @grandcentrix

    View Slide