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

Reviewing Kotlin (Conference for Kotliners 2019)

Reviewing Kotlin (Conference for Kotliners 2019)

I have been teaching our rapidly growing team of Android developers Kotlin for about a year, and for the last few months, I’ve been reviewing tens of thousands of lines of code written by almost a dozen people at times. Here’s what we’ve found out together about learning, teaching, and reviewing Kotlin. I’ll tell you what worked for us and what didn’t, so that you may be more prepared for this path than we were. I’ll also point out some of the issues that most often arose in the code while our developers were getting familiar with the language.

Recording and resources: https://zsmb.co/talks/reviewing-kotlin/

Marton Braun

June 07, 2019
Tweet

More Decks by Marton Braun

Other Decks in Programming

Transcript

  1. Reviewing Kotlin
    Márton Braun
    zsmb.co zsmb13
    [email protected]

    View Slide

  2. This is not…

    View Slide

  3. How great code reviews are, and what’s
    the best way to do them
    This is not…

    View Slide

  4. and how to get your
    management onboard with the idea
    Why you should use Kotlin for Android
    development…
    How great code reviews are, and what’s
    the best way to do them
    This is not…

    View Slide

  5. and how to get your
    management onboard with the idea
    Why you should use Kotlin for Android
    development…
    All the awesome things that are in the
    Kotlin standard library
    How great code reviews are, and what’s
    the best way to do them
    This is not…

    View Slide

  6. Life is Great and Everything Will Be Ok,
    Kotlin is Here
    Christina Lee, Jake Wharton
    Google I/O '17
    Dissecting the stdlib
    Huyen Tue Dao
    KotlinConf 2018
    Code Review Best Practices
    Trisha Gee
    SCLConf 2018
    This is not…

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. 99000 lines of Kotlin code
    5700 commits
    700 merge requests

    View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. Breaking the habit

    View Slide

  23. Breaking the habit
    Enums

    View Slide

  24. Breaking the habit
    Enums Lambdas

    View Slide

  25. Breaking the habit
    Enums Lambdas Typechecks

    View Slide

  26. Breaking the habit
    Enums Lambdas Typechecks
    is
    as
    as?

    View Slide

  27. entries: ArrayList) {
    fun updateColors(chart: ,
    }
    PieChart

    View Slide

  28. entries: ArrayList) {
    fun updateColors(chart: ,
    val colors = ArrayList()
    }
    PieChart

    View Slide

  29. entries: ArrayList) {
    fun updateColors(chart: ,
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    }
    PieChart

    View Slide

  30. entries: ArrayList) {
    fun updateColors(chart: ,
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    chart.colors = colors
    }
    PieChart

    View Slide

  31. entries: ArrayList) {
    fun updateColors(chart: ,
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    chart.colors = colors
    }
    PieChart

    View Slide

  32. fun PieChart.updateColors(entries:
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    this.colors = colors
    }
    List) {
    Array

    View Slide

  33. fun PieChart.updateColors(entries:
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    this.colors = colors
    }
    List) {

    View Slide

  34. fun PieChart.updateColors(entries:
    val colors = ArrayList()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    this.colors = colors
    }
    List) {

    View Slide

  35. fun PieChart.updateColors(entries: List) {
    val colors = mutableListOf()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    this.colors = colors
    }

    View Slide

  36. fun PieChart.updateColors(entries: List) {
    val colors = mutableListOf()
    entries.forEach { entry ->
    colors.add(entry.data as Int)
    }
    this.colors = colors
    }

    View Slide

  37. fun PieChart.updateColors(entries: List) {
    val colors = mutableListOf()
    entries.map { entry ->
    colors.add( )
    }
    this.colors = colors
    }
    entry.data as Int

    View Slide

  38. fun PieChart.updateColors(entries: List) {
    val colors = mutableListOf()
    entries.map { entry ->
    }
    this.colors = colors
    }
    entry.data as Int

    View Slide

  39. fun PieChart.updateColors(entries: List) {
    this.colors = colors
    }
    entry.data as Int
    }
    entries.map { entry ->
    val colors =

    View Slide

  40. fun PieChart.updateColors(entries: List) {
    this.colors = colors
    }
    entry.data as Int
    }
    entries.map { entry ->
    val colors =

    View Slide

  41. fun PieChart.updateColors(entries: List) {
    this.colors
    entry.data as Int
    }
    }
    = entries.map { entry ->

    View Slide

  42. View Slide

  43. View Slide

  44. fun getExaminationResult(resultId: UUID): ExaminationResult {
    }

    View Slide

  45. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }

    View Slide

  46. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .map { it.toExaminationResult() }

    View Slide

  47. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .map { it.toExaminationResult() }
    .first { it.id == resultId }

    View Slide

  48. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .map { it.toExaminationResult() }
    .first { it.id == resultId }

    View Slide

  49. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .map { it.toExaminationResult() }
    .first { it.id == resultId }

    View Slide

  50. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .first { it.id == resultId }
    .map { it }
    .toExaminationResult()

    View Slide

  51. fun getExaminationResult(resultId: UUID): ExaminationResult {
    return getItemGroups()
    }
    .first { it.id == resultId }
    .toExaminationResult()

    View Slide

  52. val events: List = getAllEvents()
    val upcoming = events.filter {
    it.date >
    }
    OffsetDateTime.now()

    View Slide

  53. val events: List = getAllEvents()
    val upcoming = events.filter {
    it.date >
    }
    OffsetDateTime.now()
    val now =
    now

    View Slide

  54. fun startBluetoothLeScan() {
    }

    View Slide

  55. fun startBluetoothLeScan() {
    val scanner = BluetoothAdapter.getDefaultAdapter()
    .bluetoothLeScanner
    }

    View Slide

  56. fun startBluetoothLeScan() {
    val scanner = BluetoothAdapter.getDefaultAdapter()
    .bluetoothLeScanner
    /* ... */
    }

    View Slide

  57. fun startBluetoothLeScan() {
    val scanner = BluetoothAdapter.getDefaultAdapter()
    .bluetoothLeScanner
    /* ... */
    scanner
    }
    .startScan(/* ... */)

    View Slide

  58. fun startBluetoothLeScan() {
    val scanner = BluetoothAdapter.getDefaultAdapter()
    .bluetoothLeScanner
    /* ... */
    scanner?
    }
    .startScan(/* ... */)

    View Slide

  59. if (scanner != null) {
    } else {
    /* ¯\_(ツ)_/¯ */
    }
    }
    fun startBluetoothLeScan() {
    val scanner = BluetoothAdapter.getDefaultAdapter()
    .bluetoothLeScanner
    /* ... */
    scanner.startScan(/* ... */)

    View Slide

  60. View Slide

  61. View Slide

  62. data class DailyFluidConsumption(
    )

    View Slide

  63. data class DailyFluidConsumption(
    val quantity: Double,
    )

    View Slide

  64. data class DailyFluidConsumption(
    val quantity: Double,
    val recommended: Double = 4.0,
    )

    View Slide

  65. data class DailyFluidConsumption(
    val quantity: Double,
    val recommended: Double = 4.0,
    val percentage: Int =
    ((quantity / recommended) * 100)
    .roundToInt()
    .coerceIn(0..100)
    )

    View Slide

  66. data class DailyFluidConsumption(
    val quantity: Double,
    val recommended: Double = 4.0
    )
    val percentage: Int =
    ((quantity / recommended) * 100)
    .roundToInt()
    .coerceIn(0..100)

    View Slide

  67. data class DailyFluidConsumption(
    val quantity: Double,
    val recommended: Double = 4.0
    )
    val DailyFluidConsumption.percentage: Int
    ((quantity / recommended) * 100)
    .roundToInt()
    .coerceIn(0..100)

    View Slide

  68. data class DailyFluidConsumption(
    val quantity: Double,
    val recommended: Double = 4.0
    )
    val DailyFluidConsumption.percentage: Int
    get() = ((quantity / recommended) * 100)
    .roundToInt()
    .coerceIn(0..100)

    View Slide

  69. zsmb.co/data-classes-arent-that-magical

    View Slide

  70. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )

    View Slide

  71. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )

    View Slide

  72. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )

    View Slide

  73. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )
    Ingredient(id, "Pixie dust", 6.28, "teaspoon")

    View Slide

  74. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )
    Ingredient(id, "Pixie dust", 6.28, "teaspoon")

    View Slide

  75. class Ingredient(
    val id: UUID = UUID.randomUUID(),
    val name: String = "",
    val quantity: Double? = null,
    val unit: String? = null,
    val imageId: UUID? = null
    )
    Ingredient(id, "Pixie dust", 6.28, "teaspoon")

    View Slide

  76. class Ingredient(
    val id: UUID,
    val name: String,
    val quantity: Double?,
    val unit: String?,
    val imageId: UUID?
    )
    Ingredient(id, "Pixie dust", 6.28, "teaspoon")

    View Slide

  77. class Ingredient(
    val id: UUID,
    val name: String,
    val quantity: Double?,
    val unit: String?,
    val imageId: UUID?
    )
    Ingredient(id, "Pixie dust", 6.28, "teaspoon", null)

    View Slide

  78. id =
    name =
    quantity =
    unit =
    imageId =
    id,
    6.28,
    )
    "teaspoon",
    null
    Ingredient(
    "Pixie dust",
    class Ingredient(
    val id: UUID,
    val name: String,
    val quantity: Double?,
    val unit: String?,
    val imageId: UUID?
    )

    View Slide

  79. suspend fun

    View Slide

  80. suspend fun getValidMeasurements(
    measurements: List
    ): List {
    }

    View Slide

  81. suspend fun getValidMeasurements(
    measurements: List
    ): List {
    return measurements.filter {
    }
    }

    View Slide

  82. suspend fun getValidMeasurements(
    measurements: List
    ): List {
    return measurements.filter {
    val validator = getValidatorForType(it.type)
    }
    }

    View Slide

  83. suspend fun getValidMeasurements(
    measurements: List
    ): List {
    return measurements.filter {
    val validator = getValidatorForType(it.type)
    it.value in (validator.minValue..validator.maxValue)
    }
    }

    View Slide

  84. suspend fun getValidMeasurements(
    measurements: List
    ): List {
    return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }
    }

    View Slide

  85. return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }
    }
    suspend fun getValidMeasurements(
    measurements: List
    ): List {

    View Slide

  86. return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }
    }
    suspend fun getValidMeasurements(
    measurements: List
    ): List {

    View Slide

  87. return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }
    }
    suspend fun getValidMeasurements(
    measurements: List
    ): List {

    View Slide

  88. val validatorsByType = getAllValidators()
    suspend fun getValidMeasurements(
    measurements: List
    ): List {
    }
    return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }

    View Slide

  89. val validatorsByType = getAllValidators().associateBy { it.type }
    suspend fun getValidMeasurements(
    measurements: List
    ): List {
    }
    return measurements.filter { measurement ->
    val validator = getValidatorForType(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }

    View Slide

  90. val validatorsByType = getAllValidators()
    suspend fun getValidMeasurements(
    measurements: List
    ): List {
    }
    return measurements.filter { measurement ->
    val validator = validatorsByType.getValue(measurement.type)
    measurement.value in (validator.minValue..validator.maxValue)
    }
    .associateBy { it.type }

    View Slide

  91. View Slide

  92. View Slide

  93. View Slide

  94. View Slide

  95. View Slide

  96. View Slide

  97. View Slide

  98. View Slide

  99. View Slide

  100. View Slide

  101. View Slide

  102. View Slide

  103. View Slide

  104. View Slide

  105. View Slide

  106. View Slide

  107. fun add (x:Int,y: Int) :Int{
    return x +y
    }
    fun main() {
    println( add(2,3) )
    }

    View Slide

  108. fun add(x: Int, y: Int): Int {
    return x + y
    }
    fun main() {
    println(add(2, 3))
    }

    View Slide

  109. What did you miss about Java?

    View Slide

  110. Related talks
    • Code Review Best Practices
     Trisha Gee, SCLConf 2018
     https://www.youtube.com/watch?v=jXi8h44cbQA
    • Life is Great and Everything Will Be Ok, Kotlin is Here
     Christina Lee & Jake Wharton, Google I/O ‘17
     https://www.youtube.com/watch?v=fPzxfeDJDzY
    • Dissecting the stdlib
     Huyen Tue Dao, KotlinConf 2018
     https://www.youtube.com/watch?v=Fzt_9I733Yg

    View Slide

  111. Learning resources
    • Kotlin in Action, Dmitry Jemerov and Svetlana Isakova
     https://www.manning.com/books/kotlin-in-action
    • Coursera course, Andrey Breslav and Svetlana Isakova
     https://www.coursera.org/learn/kotlin-for-java-developers
    • O’Reilly courses, Hadi Hariri
     https://hadihariri.com/2016/11/01/oreilly-kotlin-course/

    View Slide

  112. Learning resources
    • Kotlin Bootcamp for Programmers
     https://eu.udacity.com/course/kotlin-bootcamp-for-
    programmers--ud9011
    • Developing Android Apps with Kotlin
     https://eu.udacity.com/course/developing-android-apps-with-
    kotlin--ud9012

    View Slide

  113. Further reading
    • Data classes aren’t (that) magical
     https://zsmb.co/data-classes-arent-that-magical/

    View Slide

  114. zsmb13
    zsmb.co/talks

    View Slide

  115. Questions?
    Márton Braun
    zsmb.co/talks
    zsmb13
    [email protected]

    View Slide