Slide 1

Slide 1 text

Succumb to the Kotlin side

Slide 2

Slide 2 text

Who am I?

Slide 3

Slide 3 text

Acknowledge

Slide 4

Slide 4 text

Index

Slide 5

Slide 5 text

Constructors class Person {}

Slide 6

Slide 6 text

Constructors class Person constructor(firstName: String) {}

Slide 7

Slide 7 text

Constructors class Person(firstName: String) {} (if no annotations or visibility modifiers)

Slide 8

Slide 8 text

Constructors class Person(firstName: String) { init { //We can use/initialize firstName here } }

Slide 9

Slide 9 text

Constructors class Person(val firstName: String) {}

Slide 10

Slide 10 text

Constructors class Person(val firstName: String = "Alberto") {}

Slide 11

Slide 11 text

Constructors data class Person(val firstName: String = "Alberto"){}

Slide 12

Slide 12 text

Constructors data class Person(val firstName: String = "Alberto"){} ● equals()/hashCode() ● toString() of the form "Person(firstName=Alberto)" ● componentN() functions ● copy()

Slide 13

Slide 13 text

Constructors data class Person(val firstName: String = "Alberto"){} // val alberto = Person() val joe = Person("Joe")

Slide 14

Slide 14 text

Constructors data class Person(val firstName: String = "Alberto"){} // val alberto = Person() val joe = Person("Joe") Ok, but...

Slide 15

Slide 15 text

Constructors data class Person(val firstName: String = "Alberto"){} // val alberto = Person() val joe = Person("Joe") Where´s the “new”?

Slide 16

Slide 16 text

Constructors data class Person(val firstName: String = "Alberto"){} // val alberto = Person() val joe = Person("Joe") Where´s the “new”? and the semicolon!?

Slide 17

Slide 17 text

Functions fun read(items: Array, offset: Int = 0, length: Int = items.size()) { // ... }

Slide 18

Slide 18 text

Functions fun read(items: Array, offset: Int = 0, length: Int = items.size()) { // ... } read(ArrayOf(1, 2, 3))

Slide 19

Slide 19 text

Functions fun read(items: Array, offset: Int = 0, length: Int = items.size()) { // ... } read(ArrayOf(1, 2, 3), length = 1)

Slide 20

Slide 20 text

Control flow (I) var max: Int if (a > b) //do some stuff max = a else //do some stuff max = b

Slide 21

Slide 21 text

Control flow (I) var max: Int if (a > b) //do some stuff max = a else //do some stuff max = b (but… if is now an expression)

Slide 22

Slide 22 text

Control flow (I) var max = if (a > b) //do some stuff a else //do some stuff b (if branches can be blocks, and the last expression is the value of a block)

Slide 23

Slide 23 text

Control flow (I) var max = if (a > b) a else b (and for simpler ifs...)

Slide 24

Slide 24 text

Control flow (I) var max = if (a > b) a else b

Slide 25

Slide 25 text

Control flow (II) switch (x) { 1 -> print("x is 1") 2 -> print("x is 2") }

Slide 26

Slide 26 text

Control flow (II) switch (x) { 1 -> print("x is 1") 2 -> print("x is 2") }

Slide 27

Slide 27 text

Control flow (II) when (x) { 1 -> print("x is 1") 2 -> print("x is 2") }

Slide 28

Slide 28 text

Control flow (II) when (x) { 0, 1 -> print("x is 0 or 1") 2 -> print("x is 2") }

Slide 29

Slide 29 text

Control flow (II) when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } (validNumbers is a collection)

Slide 30

Slide 30 text

Control flow (II) val hasPrefix = when(x) { is String -> x.startsWith("prefix") else -> false } (since here when is an expression we’re forced to put the else)

Slide 31

Slide 31 text

Control flow (II) val hasPrefix = when(x) { is String -> x.startsWith("prefix") else -> false } (since here when is an expression we’re forced to put the else)

Slide 32

Slide 32 text

Control flow (extra) if (obj is String) { // At this point obj is magically a String! print(obj.length) }

Slide 33

Slide 33 text

Null safety var a: String = "abc" a = null a.capitalize()

Slide 34

Slide 34 text

Null safety var a: String = "abc" a = null a.capitalize() Kotlin Quiz a) NullPointerException

Slide 35

Slide 35 text

Null safety var a: String = "abc" a = null a.capitalize() Kotlin Quiz a) NullPointerException b) Compilation error

Slide 36

Slide 36 text

Null safety var a: String = "abc" a = null a.capitalize() Kotlin Quiz a) NullPointerException b) Compilation error c) Nothing, the method won´t be executed

Slide 37

Slide 37 text

Null safety var a: String = "abc" a = null a.capitalize() Kotlin Quiz a) NullPointerException b) Compilation error c) Nothing, the method won´t be executed d) Assertion error

Slide 38

Slide 38 text

Null safety var a: String = "abc" a = null a.capitalize() Kotlin Quiz a) NullPointerException b) Compilation error c) Nothing, the method won´t be executed d) Assertion error e) I really think it´s a NullPointerException

Slide 39

Slide 39 text

Null safety var a: String = "abc" a = null // compilation error a.capitalize()

Slide 40

Slide 40 text

Null safety var a: String? = "abc" a = null a.capitalize()

Slide 41

Slide 41 text

Null safety var a: String? = "abc" a = null a.capitalize() // compilation error

Slide 42

Slide 42 text

Null safety var a: String? = "abc" a = null if (a != null) { a.capitalize() }

Slide 43

Slide 43 text

Null safety var a: String? = "abc" a = null if (a != null) { // boring a.capitalize() }

Slide 44

Slide 44 text

Null safety var a: String? = "abc" a = null if (a != null) { // boring a.capitalize() } // 3 lines

Slide 45

Slide 45 text

Null safety var a: String? = "abc" a = null if (a != null) { // boring a.capitalize()// 17 characters for a null-check } // 3 lines

Slide 46

Slide 46 text

Null safety var a: String? = "abc" a = null a?.capitalize()

Slide 47

Slide 47 text

Null safety var a: String? = "abc" a = null a ?.capitalize()

Slide 48

Slide 48 text

Null safety var a: String? = "abc" a = null a ?.capitalize() // 1 char

Slide 49

Slide 49 text

Null safety var a: String? = "abc" a = null a ?.capitalize() // 1 char, 16 less than before :)

Slide 50

Slide 50 text

Null safety var a: String? = "abc" var b = if (a != null) { a.capitalize() } else { "" }

Slide 51

Slide 51 text

Null safety var a: String? = "abc" var b = a?.capitalize() else ""

Slide 52

Slide 52 text

Null safety var a: String? = "abc" var b = a?.capitalize() else ""

Slide 53

Slide 53 text

Null safety var a: String? = "abc" var b = a?.capitalize() ?: ""

Slide 54

Slide 54 text

Null safety var a: String? = "abc" var b = a?.capitalize() ?: ""

Slide 55

Slide 55 text

Static

Slide 56

Slide 56 text

Static Doesn’t exist

Slide 57

Slide 57 text

Static Doesn’t exist Bye utils?

Slide 58

Slide 58 text

Static Doesn’t exist Bye singleton? Bye utils?

Slide 59

Slide 59 text

Object window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } } // This is an Object expression

Slide 60

Slide 60 text

Object object StuffManager { fun registerStuff(stuff: Stuff) { // ... } val allStuffs: Collection get() = // ... } // This is an Object declaration

Slide 61

Slide 61 text

Object object StuffManager { fun registerStuff(stuff: Stuff) { // ... } val allStuffs: Collection get() = // ... } // This is an Object declaration Hello singleton!

Slide 62

Slide 62 text

Object class MyClass { companion object { fun getSomeUtilStuff() { //... } } } Hello utils

Slide 63

Slide 63 text

Object class MyClass { companion object { fun getSomeUtilStuff() { //... } } } Hello utils?

Slide 64

Slide 64 text

Extension Functions // Java code public static void hideImage(ImageView image) { image.setVisibility(View.GONE); image.setImageResource(0); } UiUtils.hideImage(new ImageView(context));

Slide 65

Slide 65 text

Extension Functions // Kotlin magic fun ImageView.hide() { visibility = View.GONE setImageResource(0) }

Slide 66

Slide 66 text

Extension Functions // Kotlin magic fun ImageView.hide() { visibility = View.GONE setImageResource(0) } // import com.xing.core.extensions.hide ImageView(context).hide()

Slide 67

Slide 67 text

Higher-Order Functions fun logException(func: () -> T): T? { try { return func() } catch (ignored: Throwable) { Log.e("ERROR", "...") return null } }

Slide 68

Slide 68 text

Higher-Order Functions fun logException(func: () -> T) = try { return func() } catch (ignored: Throwable) { Log.e("ERROR", "...") return null }

Slide 69

Slide 69 text

Higher-Order Functions inline fun logException(func: () -> T) = try { return func() } catch (ignored: Throwable) { Log.e("ERROR", "...") return null }

Slide 70

Slide 70 text

Higher-Order Functions inline fun logException(func: () -> T) = try { return func() } catch (ignored: Throwable) { Log.e("ERROR", "...") return null } // ... logException({ myDangerousMethod() })

Slide 71

Slide 71 text

Higher-Order Functions inline fun logException(func: () -> T) = try { return func() } catch (ignored: Throwable) { Log.e("ERROR", "...") return null } // ... logException { myDangerousMethod() }

Slide 72

Slide 72 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) val edit = prefs.edit() edit.putBoolean("enabled", true) edit.commit()

Slide 73

Slide 73 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabled", true) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() }

Slide 74

Slide 74 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabledCount", 1) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() }

Slide 75

Slide 75 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabledCount", 1) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() } fun edit(key: String, value: Int) { prefs.edit().putInt(key, value).commit() }

Slide 76

Slide 76 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabledCount", 1F) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() } fun edit(key: String, value: Int) { prefs.edit().putInt(key, value).commit() }

Slide 77

Slide 77 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabledCount", 1F) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() } fun edit(key: String, value: Int) { prefs.edit().putInt(key, value).commit() } fun edit(key: String, value: Float) { prefs.edit().putFloat(key, value).commit() }

Slide 78

Slide 78 text

Extensions + High-Order = Awesomeness val prefs = context.getSharedPreferences(...) // in some method... edit("enabledCount", 1L) fun edit(key: String, value: Boolean) { prefs.edit().putBoolean(key, value).commit() } fun edit(key: String, value: Int) { prefs.edit().putInt(key, value).commit() } fun edit(key: String, value: Float) { prefs.edit().putFloat(key, value).commit() }

Slide 79

Slide 79 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: () -> Unit) { val editor = edit() func() editor.commit() }

Slide 80

Slide 80 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: () -> Unit) { val editor = edit() func() editor.commit() } prefs.inEdit { prefs.putBoolean(...) }

Slide 81

Slide 81 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: () -> Unit) { val editor = edit() func() editor.commit() } prefs.inEdit { prefs.putBoolean(...) }

Slide 82

Slide 82 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: (SharedPreferences.Editor) -> Unit) { val editor = edit() func() editor.commit() } prefs.inEdit { prefs.putBoolean(...) }

Slide 83

Slide 83 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: (SharedPreferences.Editor) -> Unit) { val editor = edit() func(editor) editor.commit() } prefs.inEdit { prefs.putBoolean(...) }

Slide 84

Slide 84 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: (SharedPreferences.Editor) -> Unit) { val editor = edit() func(editor) editor.commit() } prefs.inEdit { editor -> editor.putBoolean(...) }

Slide 85

Slide 85 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: (SharedPreferences.Editor) -> Unit) { val editor = edit() func(editor) editor.commit() } prefs.inEdit { it.putBoolean(...) }

Slide 86

Slide 86 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: SharedPreferences.Editor.() -> Unit) { val editor = edit() func(editor) editor.commit() } prefs.inEdit { it.putBoolean(...) }

Slide 87

Slide 87 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.commit() } prefs.inEdit { it.putBoolean(...) }

Slide 88

Slide 88 text

Extensions + High-Order = Awesomeness inline fun SharedPreferences.inEdit( func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.commit() } prefs.inEdit { putBoolean(...) }

Slide 89

Slide 89 text

And more...

Slide 90

Slide 90 text

And more... Because… why not?

Slide 91

Slide 91 text

Android extensions import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Instead of findView(R.id.textView) as TextView textView.text = "Hello, world!" } }

Slide 92

Slide 92 text

Examples (1) Bundle args = getArguments() if (args != null) { String data = args.getString(DATA_ARG) if (data != null) { data.getBytes() } }

Slide 93

Slide 93 text

Examples (1) Bundle args = getArguments() if (args != null) { String data = args.getString(DATA_ARG) if (data != null) { data.getBytes() } }

Slide 94

Slide 94 text

Examples (1) getArguments()?.getString(DATA_ARG)?.getBytes()

Slide 95

Slide 95 text

Examples (2) imm = getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0)

Slide 96

Slide 96 text

Examples (2) fun View.hideKeyboard() { imm = getContext().getSystemService( Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(getWindowToken(), 0) }

Slide 97

Slide 97 text

Examples (2) fun View.hideKeyboard() { imm = getContext().getSystemService( Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(getWindowToken(), 0) } yourEditText.hideKeyboard()

Slide 98

Slide 98 text

Examples (3) Bundle bigBundle = Bundle() bigBundle.putString(/**/) bigBundle.putInt(/**/) bigBundle.putString(/**/) bigBundle.putString(/**/)

Slide 99

Slide 99 text

Examples (3) Bundle bigBundle = Bundle() bigBundle.apply { putString(/**/) putInt(/**/) putString(/**/) putString(/**/) //… }

Slide 100

Slide 100 text

What can be optimized ● Every Utils class ● Duplicated boilerplate code ○ Model classes ○ Actions we do in single objects (Picasso) ● Whenever you have to write the same word multiple times (almost)

Slide 101

Slide 101 text

Some cons (because nobody is perfect)

Slide 102

Slide 102 text

Some cons (because nobody is perfect)

Slide 103

Slide 103 text

Some cons (because nobody is perfect)

Slide 104

Slide 104 text

Some cons (because nobody is perfect)

Slide 105

Slide 105 text

Some cons (because nobody is perfect)

Slide 106

Slide 106 text

Some cons (because nobody is perfect)

Slide 107

Slide 107 text

Is it worth? Well, that's up to one person…

Slide 108

Slide 108 text

Is it worth? Well, that's up to one person…

Slide 109

Slide 109 text

Is it worth? Well, that's up to one person… (and your coworkers)

Slide 110

Slide 110 text

Is it worth? Well, that's up to one person… (and your coworkers) (and your tech lead)

Slide 111

Slide 111 text

Is it worth? Well, that's up to one person… (and your coworkers) (and your tech lead) (and you might want to tell your PO as well…)

Slide 112

Slide 112 text

Is it worth? Well, that's up to one person… (and your coworkers) (and your tech lead) (and you might want to tell your PO as well…) (yeah, better create a meeting for that)

Slide 113

Slide 113 text

Is it worth? Well, that's up to one person… (and your coworkers) (and your tech lead) (and you might want to tell your PO as well…) (yeah, better create a meeting for that) Silver Bullet principle

Slide 114

Slide 114 text

Links

Slide 115

Slide 115 text

Q&A