Slide 1

Slide 1 text

KOTLIN MY WAY IN +Carlos Mota @cafonsomota

Slide 2

Slide 2 text

speakerdeck.com/cmota/kotlin-my-way-in

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

When the QA team starts to teste your app NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException TransactionTooLargeException

Slide 5

Slide 5 text

A long time ago in a galaxy far, far away… (or roughly four years ago at Coimbra, Portugal)

Slide 6

Slide 6 text

me: did you heard about Kotlin? - Cotton? me: Kotlin! - Cotton? me: Kotlin! The new language developed by JetBrains - I’ve never heard about it Cotton before. - But it’s like Cotton Candy? Those people and sweets…

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

A brief history of time (when and why it all started)

Slide 9

Slide 9 text

Android timeline Android 1.0 HTC Dream 2017 Kotlin 1.3 Kotlin official for Android 2018 2008 2009 2011 2016 2013 Android Studio Kotlin 1.0 Kotlin announced Android Runtime Android Cupcake HTC Magic

Slide 10

Slide 10 text

JAVA timeline 2004 2011 2014 2017 2018 2019 JAVA 5 2006 JAVA 6 JAVA 7 JAVA 8 JAVA 9 JAVA 10/11 JAVA 12 2016 Android N (subset of JAVA 8) 2013 JAVA 6 Android KitKat AS 0.3.2 JAVA 7 AS 3.0 { { 2 years 2 years

Slide 11

Slide 11 text

#10YearChallenge

Slide 12

Slide 12 text

HTC Magic Android 2.2 (Froyo) JAVA 1.5 Pixel 3 Android 9.0 (Pie) JAVA 1.8 Kotlin 1.3

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Kotlin - JVM - Developed by JetBrains - Open source - Supported/ used by Google for Android Developed - Concise, safe, interoperable, tool-friendly (what are we talking about)

Slide 15

Slide 15 text

Kotlin (what are we talking about) Server-side Android Kotlin JS Native - No runtime overhead - Lots of new features - It’s more than just “for Android”

Slide 16

Slide 16 text

(worldwide adoption) Kotlin

Slide 17

Slide 17 text

5 Reasons Why N26 is Moving to Kotlin Pat Kua - #CTO of @N26 Square Open Source —s Kotlin Jake Wharton - Android Google (previous @Square) How we made Basecamp 3’s Android app 100% Kotlin Dan Kim - Android @ Basecamp

Slide 18

Slide 18 text

Kotlin (worldwide)

Slide 19

Slide 19 text

Kotlin (worldwide) *octoverse.github.com/projects Fastest 2.6xGrowing language on GitHub more contributors

Slide 20

Slide 20 text

75% Kotlin (worldwide) *insights.stackoverflow.com/survey/2018/ Second most loved language on StackOverflow of developers

Slide 21

Slide 21 text

Actions on Google launched a Java & Kotlin client library (Jan 2019)

Slide 22

Slide 22 text

“We built the client library entirely in Kotlin. We found Kotlin to be an expressive language with many features that allow you to implement your Actions with safer code that is easier to maintain. Kishore Subramanian Software Engineer/Developer relations — Actions on Google

Slide 23

Slide 23 text

(behind the scenes) Kotlin

Slide 24

Slide 24 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 25

Slide 25 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 26

Slide 26 text

Mutability etc. Null safety Default values Type inference String interpolation when Collections

Slide 27

Slide 27 text

Mutability (language features) val life: Life = Life()
 life.answer = 42 life.answer = 100 var answer = 42
 answer = 100 val answer = 42
 answer = 100 Val cannot be reassigned

Slide 28

Slide 28 text

Mutability (language features) val life: Life = Life()
 life.answer = 42 life.answer = 100 var answer = 42
 answer = 100 var - value can change (mutable) val - value will never change (immutable)

Slide 29

Slide 29 text

Type inference (language features) val life: Life = Life()
 life.answer = 42 val life = Life()
 life.answer = 42 var answer = 42
 answer = 100 var answer = 42
 answer = 100L Type mismatch. Required: Int Found: Long

Slide 30

Slide 30 text

String interpolation (language features) val life = getAnswerLife()
 val universe = getAnswerUniverse() val everything = getAnswerEverything() Log.d(TAG, “Life is $life, universe is $universe and everything is $everything”) Log.d(TAG, "Answer is ${life + universe + everything}”)

Slide 31

Slide 31 text

public int compute(int life) {
 return 42;
 } public int compute(int life, int universe) {
 return 42;
 } public int compute(int life, int universe, int everything) {
 return 42;
 }
 
 public void calculateAnswer() { Log.d(TAG, compute(getAnswerLife()); Log.d(TAG, compute(getAnswerLife(), getAnswerUniverse());
 Log.d(TAG, compute(getAnswerLife(), getAnswerUniverse(), getAnswerEverything());
 } Default values (language features)

Slide 32

Slide 32 text

fun compute(life: Int, universe: Int = 42, everything: Int = 42): Int = 42
 
 fun calculateAnswer() { Log.d(TAG, compute(getAnswerLife()); Log.d(TAG, compute(getAnswerLife(), getAnswerUniverse());
 Log.d(TAG, compute(getAnswerLife(), getAnswerUniverse(), getAnswerEverything()); } Default values (language features)

Slide 33

Slide 33 text

fun calculateAnswer() { ... when (answer) {
 1 -> print(“I’m afraid your answer is incorrect Dave.”)
 2 -> { print(“Computer says no.”) }
 3, 4 -> print(“The error is between the chair and the keyboard.”)
 in 5..10 -> print(“Have you tried to turn it on and off?”) 42 -> print(“The answer to life, the universe and everything.”)
 else -> print(“Oopsy.”)
 } } Control flow: when (language features)

Slide 34

Slide 34 text

NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException TransactionTooLargeException

Slide 35

Slide 35 text

Null Safety (language features)

Slide 36

Slide 36 text

Null Safety (language features) var result: String result = null Log.d(TAG, "Result = $result") Null can not be a value of a non-null type String Types are non-null by default

Slide 37

Slide 37 text

Null Safety (language features) What if I want to use null because of… my legacy code?

Slide 38

Slide 38 text

Null Safety (language features) var result: String result = null Log.d(TAG, "Result = $result") Types are non-null by default Safe accessor ?

Slide 39

Slide 39 text

Null Safety (language features) var result: String? result = null val notNull: String = result ?: “hello!” result?.let { Log.d(TAG, "Result = $result") }

Slide 40

Slide 40 text

Null Safety (language features) var result: String? result = null val notNull: String = result ?: “hello!” result?.let { Log.d(TAG, "Result = $result") } Elvis operator ?:

Slide 41

Slide 41 text

-getOrElse() -find() -filter() -filterNot() -filterNotNull() -flatMap() -mapNotNull() -all() -any() -sumBy() -zip() -… -take() -takeLast() -sortBy() -sortByDescending() -groupBy() -map() Collections (language features)

Slide 42

Slide 42 text

val numbers = mutableListOf(1,2,3)
 numbers.add(4)
 numbers += 5
 val numbers = listOf(1, 2, 3)
 numbers.add(4) Collections (language features) immutable list of integers mutable list of integers Unresolved reference: add

Slide 43

Slide 43 text

mutableListOf(1, null, 2, null, 3, 4, 5, 6, 7, 8, 9)
 .filterNotNull()
 .filter { it % 2 == 0 }
 .sortedDescending()
 > [1, null, 2, null, 3, 4, 5, 6, 7, 8, 9] Collections (language features)

Slide 44

Slide 44 text

mutableListOf(1, null, 2, null, 3, 4, 5, 6, 7, 8, 9)
 .filterNotNull()
 .filter { it % 2 == 0 }
 .sortedDescending()
 > [1, 2, 3, 4, 5, 6, 7, 8, 9] Collections (language features)

Slide 45

Slide 45 text

mutableListOf(1, null, 2, null, 3, 4, 5, 6, 7, 8, 9)
 .filterNotNull()
 .filter { it % 2 == 0 }
 .sortedDescending()
 > [2, 4, 6, 8] Collections (language features)

Slide 46

Slide 46 text

mutableListOf(1, null, 2, null, 3, 4, 5, 6, 7, 8, 9)
 .filterNotNull()
 .filter { it % 2 == 0 }
 .sortedDescending()
 > [8, 6, 4, 2] Collections (language features)

Slide 47

Slide 47 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 48

Slide 48 text

“ The best code is no code at all. Jeff Atwood Coding Horror, StackOverflow, Discourse

Slide 49

Slide 49 text

public class Conference { private String mName; private String mLocal; private List mTechs; public Conference(String name, String local, List techs) { mName = name; mLocal = local; mTechs = techs; } public String getName() { return mName; } public void setName(String name) { this.mName = name; Concise (less code) Conference.java

Slide 50

Slide 50 text

Concise (less code) data class Conference (val name: String, val location: String, val techs: List) Conference.kt

Slide 51

Slide 51 text

Concise (less code) 65 1 Kotlin JAVA lines of code line of code

Slide 52

Slide 52 text

Concise (less code) findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Do something } }); .java

Slide 53

Slide 53 text

Concise (less code) findViewById(R.id.btn_done).setOnClickListener { //Do something } .kt

Slide 54

Slide 54 text

Concise (less code) it findViewById(R.id.btn_done).setOnClickListener { //Do something } .kt

Slide 55

Slide 55 text

Concise (less code) But can we do better? findViewById(R.id.btn_done).setOnClickListener { //Do something } it .kt

Slide 56

Slide 56 text

Concise (less code) apply plugin: 'kotlin-android-extensions' btn_done.setOnClickListener { it //Do something } build.gradle findViewById(R.id.btn_done).setOnClickListener { it //Do something } .kt

Slide 57

Slide 57 text

Concise (less code) AppButton.java public class AppButton extends Button { public AppButton(Context context) { super(context); } public AppButton(Context context, AttributeSet attrs) { super(context, attrs); } public AppButton(Context context, AttributeSet attrs, int style) { super(context, attrs, style); } ... }

Slide 58

Slide 58 text

Concise (less code) public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.buttonStyle); } public Button(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } Button.java (native)

Slide 59

Slide 59 text

Concise (less code) class AppButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, style: Int = com.android.internal.R.attr.buttonStyle) : Button(context, attrs, style) { ... } AppButton.kt

Slide 60

Slide 60 text

Concise (less code) class AppButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, style: Int = com.android.internal.R.attr.buttonStyle) : Button(context, attrs, style) { ... } AppButton.kt default value

Slide 61

Slide 61 text

Concise (less code) class AppButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, style: Int = com.android.internal.R.attr.buttonStyle) : Button(context, attrs, style) { ... } AppButton.kt overloads Button.java constructor

Slide 62

Slide 62 text

Concise (less code) androidExtensions { experimental = true } build.gradle @Parcelize data class Conference (val name: String, val location: String, val techs: List) : Parcelable Conference.kt

Slide 63

Slide 63 text

Less bugs! Concise (less code) more free time (and happier team… and management)

Slide 64

Slide 64 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 65

Slide 65 text

- Small learning curve involved - Easily to go from JavaScript/ Swift into Kotlin and back Easy to learn (from a developer’s point of view)

Slide 66

Slide 66 text

Kotlin vs Swift (variables and constants) var variable = 42 variable = 1 let value = 42 var variable = 42 variable = 1 val value = 42 *adapted from http://nilhcem.com/swift-is-like-kotlin/ Swift Kotlin

Slide 67

Slide 67 text

let explicitDouble: Double = 70 val explicitDouble: Double = 70.0 Kotlin vs Swift (explicit types) *adapted from http://nilhcem.com/swift-is-like-kotlin/ Swift Kotlin

Slide 68

Slide 68 text

Swift Kotlin fun greet(name: String, day: String): String { return "Hello $name, today is $day." } greet(“OPorto", "Saturday") Kotlin vs Swift (functions) *updated from http://nilhcem.com/swift-is-like-kotlin/ func greet(_ name: String,_ day: String) -> String { return "Hello \(name), today is \(day)." } greet(“OPorto", "Saturday")

Slide 69

Slide 69 text

Swift Kotlin class Shape { var numberOfSides = 0 fun simpleDescription() = "A shape with $numberOfSides sides." } Kotlin vs Swift (classes) *adapted from http://nilhcem.com/swift-is-like-kotlin/ class Shape { var numberOfSides = 0 func simpleDescription() -> String { "A shape with \(numberOfSides) sides." }

Slide 70

Slide 70 text

Swift Kotlin var shape = Shape() shape.numberOfSides = 7 var shapeDescription = shape.simpleDescription() Kotlin vs Swift (classes usage) *adapted from http://nilhcem.com/swift-is-like-kotlin/ var shape = Shape() shape.numberOfSides = 7 var shapeDescription = shape.simpleDescription()

Slide 71

Slide 71 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 72

Slide 72 text

Interoperable with JAVA (from a developer point of view) - @JvmStatic Generates static methods (for functions) and getter/setters (for properties) - @JvmOverloads Generates as many methods as the number of parameters with default values + 1 - @JvmField The compiler doesn’t generate getter/setters for this property and expose it as a field

Slide 73

Slide 73 text

- Language features - Concise - Easy to learn - Interoperable with JAVA - Big (growing) community Kotlin (from a developer’s point of view)

Slide 74

Slide 74 text

- Good documentation - A lot of resources - Internally used by Google’s Android team - Push forward by Google, JetBrains and the community Big (growing) community (from a developer point of view)

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Spreading the word (wolololol…) Spreading the word (wolololol…)

Slide 77

Slide 77 text

I’m convinced! and now?

Slide 78

Slide 78 text

We should consider to adopt Kotlin in our project

Slide 79

Slide 79 text

The talk

Slide 80

Slide 80 text

The talk pre Google I/O 17 #1 (I think you all know how this talk went, right?)

Slide 81

Slide 81 text

me: Aaaa… not yet? me: we should start using it on your project because… me: did you heard about Kotlin? - Not really - what’s that? me: A cool new language developed by JetBrains - Does Google support it?

Slide 82

Slide 82 text

NO.

Slide 83

Slide 83 text

The talk #2 2.5 years ago (if at first you don’t succeed call it version 1.0 - right?)

Slide 84

Slide 84 text

it sure will motivate the team and improve productivity It is null safe, smart casts, lambdas… - Does Google support it? me: Yes! -

Slide 85

Slide 85 text

The talk #3 2 years ago

Slide 86

Slide 86 text

The talk #3 2 years ago

Slide 87

Slide 87 text

Kotlin my way in. That’s right!

Slide 88

Slide 88 text

Kotlin my way in 1. Community 2. Team 3. Management (have a plan)

Slide 89

Slide 89 text

1. Community 2. Team 3. Management Kotlin my way in (have a plan)

Slide 90

Slide 90 text

Community Let’s start! Do you have a local community on your town/ nearby? Meet other enthusiasts Discuss ideas Go for a talk/ side project! kotlinlang.slack android-pt.slack Create your own

Slide 91

Slide 91 text

The talk #3 1. Community 2. Team 3. Management (talk with your)

Slide 92

Slide 92 text

Why would I want to learn a new language if I can do my daily tasks with JAVA? “ Team Team member Cupcake

Slide 93

Slide 93 text

Team - Comparison between JAVA code with Kotlin code So they can really see the differences - It’s faster to develop More concise language - It has less crashes Leave it to the compiler to make it’s initial checks for NPE’s, mutability, etc. - And is more reliable We will have more free time

Slide 94

Slide 94 text

Team - Internal tech talks - Start small Don’t just convert your entire code base to Kotlin - If someone wants to keep with JAVA… Well, it’s interoperable - so, why not?

Slide 95

Slide 95 text

Keep in mind - Teams are made by diverse people - Not everyone will share your enthusiasm - People are used to JAVA And all the particular cases of the language - Learning a new language always requires time and effort

Slide 96

Slide 96 text

The talk #3 1. Community 2. Team 3. Management (talk with your)

Slide 97

Slide 97 text

Management - Advantages of using Kotlin Null safety Smart casts Default values Override constructors Data classes Type inference …

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

Management - More stable application The compiler identifies errors sooner: NPE’s, mutability, etc. - Less crashes - Happier users - Lower development and bug fixing phases - More time for other features

Slide 100

Slide 100 text

Management - Features that will be done faster More concise language (comparing with JAVA) - Improves team motivation - Low risk

Slide 101

Slide 101 text

Kotlin my way in. how we’ve done it

Slide 102

Slide 102 text

Kotlin my way in (some raw data) - project with more than 7 years of daily coding 4755 classes 625487 lines of code We’re talking about

Slide 103

Slide 103 text

Kotlin my way in (some raw data) - project with more than 7 years of daily coding - low code coverage - team of 14 developers - time restrictions - enthusiasm of a single developer (to start with)

Slide 104

Slide 104 text

Kotlin my way in (how we’ve done it) - Risk analysis Learning curve Compile time - JAVA to Kotlin IDE conversion - Tests made with Kotlin - Kotlin POC’s - Time to develop/ productivity

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Kotlin my way in (how we’ve done it) - Team of 2 developers + 1 reviewer (redundancy) Have people from your team onboard Share knowledge/ discuss ideas - New feature - Isolated module - Integration with external libs (Retrofit, GreenDao, etc.)

Slide 107

Slide 107 text

Quotes (roughly translated from Portuguese) We should refactor the entire app to use Kotlin! “ Team member Cupcake

Slide 108

Slide 108 text

Quotes (roughly translated from Portuguese) Kotlin is smart enough to detect some programming errors before you hit compile. “ Team member KitKat

Slide 109

Slide 109 text

During code review (roughly translated from Portuguese) “I was fixing an issue on this class and realised that I would spend less time rewriting it on Kotlin. Team member Marshmallow

Slide 110

Slide 110 text

During code review (roughly translated from Portuguese) “Many years turning chickens! Great success with Kotlin. Team member Pie

Slide 111

Slide 111 text

32% 68% 67% 33% 58% 42% Nowadays (stats) 32% Of the project is now on Kotlin New features written on Kotlin Team developing on Kotlin 58% 67%

Slide 112

Slide 112 text

- Coding conventions - There’s still a big codebase in JAVA Does it justifies to be 100% Kotlin - Coding conventions and backwards support - Keep moving forward - Improve on boarding process Future (upcoming challenges)

Slide 113

Slide 113 text

Resources - Android @ Portugal Slack http://bit.ly/AndroidPTSlack - Kotlin Official http://kotlinlang.org/docs/reference/ - Kotlin Training https://try.kotlinlang.org/ - Kotlin by: https://jakewharton.com/presentations/ http://antonioleiva.com/kotlin

Slide 114

Slide 114 text

+Carlos Mota @cafonsomota KOTLIN MY WAY IN