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/

Márton Braun

June 07, 2019
Tweet

More Decks by Márton Braun

Other Decks in Programming

Transcript

  1. 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…
  2. 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…
  3. 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…
  4. entries: ArrayList<Entry>) { fun updateColors(chart: , val colors = ArrayList<Int>()

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

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

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

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

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

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

    entry -> colors.add( ) } this.colors = colors } entry.data as Int
  11. val events: List<Event> = getAllEvents() val upcoming = events.filter {

    it.date > } OffsetDateTime.now() val now = now
  12. if (scanner != null) { } else { /* ¯\_(ツ)_/¯

    */ } } fun startBluetoothLeScan() { val scanner = BluetoothAdapter.getDefaultAdapter() .bluetoothLeScanner /* ... */ scanner.startScan(/* ... */)
  13. data class DailyFluidConsumption( val quantity: Double, val recommended: Double =

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

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

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

    4.0 ) val DailyFluidConsumption.percentage: Int get() = ((quantity / recommended) * 100) .roundToInt() .coerceIn(0..100)
  17. class Ingredient( val id: UUID = UUID.randomUUID(), val name: String

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

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

    = "", val quantity: Double? = null, val unit: String? = null, val imageId: UUID? = null )
  20. 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")
  21. 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")
  22. 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")
  23. class Ingredient( val id: UUID, val name: String, val quantity:

    Double?, val unit: String?, val imageId: UUID? ) Ingredient(id, "Pixie dust", 6.28, "teaspoon")
  24. 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)
  25. 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? )
  26. suspend fun getValidMeasurements( measurements: List<Measurement> ): List<Measurement> { return measurements.filter

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

    { measurement -> val validator = getValidatorForType(measurement.type) measurement.value in (validator.minValue..validator.maxValue) } }
  28. return measurements.filter { measurement -> val validator = getValidatorForType(measurement.type) measurement.value

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

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

    in (validator.minValue..validator.maxValue) } } suspend fun getValidMeasurements( measurements: List<Measurement> ): List<Measurement> {
  31. val validatorsByType = getAllValidators() suspend fun getValidMeasurements( measurements: List<Measurement> ):

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

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

    List<Measurement> { } return measurements.filter { measurement -> val validator = validatorsByType.getValue(measurement.type) measurement.value in (validator.minValue..validator.maxValue) } .associateBy { it.type }
  34. fun add (x:Int,y: Int) :Int{ return x +y } fun

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

    y } fun main() { println(add(2, 3)) }
  36. 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
  37. 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/
  38. 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