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

Practical Kotlin

Practical Kotlin

Introduction talk to some Kotlin features and practices based on my previous experience.

Avatar for Alexander Levin

Alexander Levin

July 17, 2020
Tweet

More Decks by Alexander Levin

Other Decks in Programming

Transcript

  1. Agenda • Why Kotlin? • Know your features • Know

    your library • Avoid overengineering
  2. Why Kotlin? • Solved a lot of Java problems •

    Pretty easy to learn • Not too much compromises
  3. Know your features: null • Best part about null in

    Kotlin - you can choose to not use it • But if you choose to - there are things to help: a?.b a ?: 5 a?.let { … } if (a != null) { … } else { … }
  4. Know your features: Extensions • Helping to write more fluent

    code • Existing “regular” function can be used with `.let`
  5. Know your features: Reified Generics • Help a lot with

    boilerplate • Sometimes have more possibilities than providing Class type
  6. Know your features: Reified Generics data class Person(val age: Int,

    val name: String) val mapper = jacksonObjectMapper() val json = """{ "age": 25, "name": "Jason" }""" mapper.readValue(json, Person::class.java)
  7. Know your features: Reified Generics data class Person(val age: Int,

    val name: String) val mapper = jacksonObjectMapper() val json = """{ "age": 25, "name": "Jason" }""" mapper.readValue(json, object: TypeReference<Person>(){})
  8. Know your features: Reified Generics data class Person(val age: Int,

    val name: String) val mapper = jacksonObjectMapper() val json = """{ "age": 25, "name": "Jason" }""" mapper.readValue<Person>(json)
  9. Know your features: Contracts println(nullableString.capitalize()) // not allowed, no `.capitalize()`

    for nullable string requireNotNull(nullableString) println(nullableString.capitalize()) // now it’s fine
  10. Know your features: Contracts println(nullableString.capitalize()) // not allowed, no `.capitalize()`

    for nullable string requireNotNull(nullableString) println(nullableString.capitalize()) // now it’s fine
  11. Know your library: Name Validation I know RegExes: val validCharRegex

    = """[\s\w\d?\-_'.,/@&()!+:]""".toRegex() return name.all { validCharRegex.matches(it.toString()) } // please gods I hope that’s working
  12. Know your library: Name Validation I ACTUALLY know RegExes: val

    validCharRegex = """[\s\p{gc=L}\d?\-_'.,/@&()!+:]""".toRegex() return name.all { validCharRegex.matches(it.toString()) } // did you notice the change? No? That’s fine
  13. Know your library: Name Validation I know Kotlin: val validChars

    = setOf('-', '\'', '.', ',', '/', '@', '&', '(', ')', '!', '+', ':') return name.all { it.isLetter() || it in validChars } // looks easier, maybe even written by human
  14. Know your library: Name Validation I know how to validate

    person’s name: return true // because you always fail if you try
  15. Know your library: Overall • A lot of great stuff

    in the standard library • Most likely you don’t need Apache/Guava • It’s fine to still use Java standard library
  16. Avoid overengineering: null x?.let { … } // Fine idiomatic

    code x?.let { … } ?: run { … } // Stop it. Get some help
  17. Avoid overengineering: null if (x != null) { … }

    else { … } // More code but less chances to break a leg
  18. Avoid overengineering: common algorithms val percent = when { input

    > 100 -> 100 input < 0 -> 0 else -> input } val percent = input.coerceIn(0, 100)
  19. Avoid overengineering: common algorithms val percent = when { input

    > 100 -> 100 input < 0 -> 0 else -> input } val percent = input.coerceIn(0, 100)
  20. Avoid overengineering: DSL for everything • DSLs are cool •

    But essentially it is just bunch of setters • Sometimes simple constructor is better
  21. Avoid overengineering: DSL for everything val r = Rectangle() r.color

    = 0xFAFAFA r.xSize = 100 r.ySize = 200 // meh
  22. Avoid overengineering: DSL for everything val r = Rectangle().apply {

    color = 0xFAFAFA xSize = 100 ySize = 200 } // better
  23. Avoid overengineering: DSL for everything val r = Rectangle( color

    = 0xFAFAFA, xSize = 100, ySize = 200 ) // best