Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

This is not…

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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…

Slide 5

Slide 5 text

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…

Slide 6

Slide 6 text

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…

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

99000 lines of Kotlin code 5700 commits 700 merge requests

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Breaking the habit

Slide 23

Slide 23 text

Breaking the habit Enums

Slide 24

Slide 24 text

Breaking the habit Enums Lambdas

Slide 25

Slide 25 text

Breaking the habit Enums Lambdas Typechecks

Slide 26

Slide 26 text

Breaking the habit Enums Lambdas Typechecks is as as?

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

fun getExaminationResult(resultId: UUID): ExaminationResult { }

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

fun startBluetoothLeScan() { }

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

data class DailyFluidConsumption( )

Slide 63

Slide 63 text

data class DailyFluidConsumption( val quantity: Double, )

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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")

Slide 74

Slide 74 text

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")

Slide 75

Slide 75 text

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")

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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)

Slide 78

Slide 78 text

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? )

Slide 79

Slide 79 text

suspend fun

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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) }

Slide 89

Slide 89 text

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) }

Slide 90

Slide 90 text

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 }

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

What did you miss about Java?

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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/

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

zsmb13 zsmb.co/talks

Slide 115

Slide 115 text

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