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

Let's Tune This Kotlin

Let's Tune This Kotlin

Simple Kotlin tips and tricks

Avatar for Kingsley Adio

Kingsley Adio

November 04, 2017
Tweet

Other Decks in Programming

Transcript

  1. Kingsley Adio •Software Developer @HelloFresh •Eats droids for a living

    •Big Kotlin enthusiast •Twitter @kingsleyadio
  2. Hello Kotlin • Modern • Safe • Developer and tool

    friendly • Great Java interop • Thriving ecosystem • No extra overhead
  3. Quick Dive • Companion Object • To const or not

    to const • Inline functions • FP and collections • Lazy vs Lateinit • Sealed classes • Data classes
  4. Companion Object • Companion objects are objects themselves. Take advantage

    of this! • They can implement interfaces and extend other classes
  5. To const or not to const • Prefer const for

    truly static final compile time guarantees. • Note: const only works for JVM primitives and Strings • Use @JvmField in other cases
  6. Inline functions • Slapping inline onto a function doesn’t necessarily

    help if the functional argument will be stored • Debugging could be tricky • Heavy inlining could disable potential JVM optimizations
  7. Lazy vs Lateinit • Lazy is easier • Lateinit is

    cheaper • Both can be quite tricky
  8. Sealed Classes • Sealed classes are actual classes. • Remember

    all the fuss with enums? They are actual concerns with sealed classes. • They are great for modelling related classes with specialised values • Result types: Result, Either, Maybe
  9. Data Classes • They implement toString, equals and hashCode out

    of the box. • Destructuring ❤ data class • Avoid slapping data on all your POKOs just because it feels nice • Method count!
  10. Bonus: no-arg plugin • Generates a public no argument constructor.

    • Helps avoid self-defeating defaults • Great for JPA/BV, GSON, Moshi, Realm, etc
  11. Conclusion • Kotlin is a great language with a very

    beautiful syntax and low entry barrier • We naturally want to explore beauty • However, beauty comes at a cost • Once we understand this, we can find the right balance between beauty and quality