Slide 1

Slide 1 text

Lessons learned from porting to Swift and Kotlin by Ellen Shapiro Mobile Era | Oslo, Norway | October 2017 @designatednerd | justhum.com | designatednerd.com

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

(this is awkward)

Slide 4

Slide 4 text

!

Slide 5

Slide 5 text

!

Slide 6

Slide 6 text

!""

Slide 7

Slide 7 text

!"#

Slide 8

Slide 8 text

!"#

Slide 9

Slide 9 text

!

Slide 10

Slide 10 text

!

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

ANYWAY

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

Android

Slide 22

Slide 22 text

iOS

Slide 23

Slide 23 text

iOS (excluding dependencies)

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Step 1 Selling it to The Bosses

Slide 26

Slide 26 text

Identify specific problems addressed by the new language

Slide 27

Slide 27 text

Objective-C if (![paymentNetworks containsObject:PKPaymentNetworkDiscover]) { [paymentNetworks addObject:PKPaymentNetworkDiscover]; }

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Objective-C if (![paymentNetworks containsObject:PKPaymentNetworkDiscover]) { [paymentNetworks addObject:PKPaymentNetworkDiscover]; } iOS 9 = !

Slide 30

Slide 30 text

Objective-C if (![paymentNetworks containsObject:PKPaymentNetworkDiscover]) { [paymentNetworks addObject:PKPaymentNetworkDiscover]; } iOS 8 = !

Slide 31

Slide 31 text

Swift if !paymentNetworks.contains(.discover) { paymentNetworks.append(.discover) }

Slide 32

Slide 32 text

Swift if !paymentNetworks.contains(.discover) { paymentNetworks.append(.discover) } !" Does Not Compile

Slide 33

Slide 33 text

Swift if @available(iOS 9.0, *) { if !paymentNetworks.contains(.discover) { paymentNetworks.append(.discover) } }

Slide 34

Slide 34 text

Swift if @available(iOS 9.0, *) { if !paymentNetworks.contains(.discover) { paymentNetworks.append(.discover) } } Safe at compile time and run time

Slide 35

Slide 35 text

Be honest about potential drawbacks

Slide 36

Slide 36 text

Be honest (with yourself) about potential drawbacks

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Step 2 Start small

Slide 42

Slide 42 text

Step 2 Start small

Slide 43

Slide 43 text

Learn the new language

Slide 44

Slide 44 text

Fight New System APIs or A New Language not both at once

Slide 45

Slide 45 text

Stop writing old language patterns in a new language

Slide 46

Slide 46 text

Objective-C NSMutableArray *capitalizedStrings = [NSMutableArray array]; for (NSString *string in arrayOfStrings) { NSString *thing = [string capitalized]; [capitalizedStrings addObject: thing]; }

Slide 47

Slide 47 text

Objective-C, but written in Swift var capitalizedStrings = [String]() for string in arrayOfStrings { let thing = string.capitalized capitalizedStrings.append(thing) }

Slide 48

Slide 48 text

Swiftier let capitalizedStrings = arrayOfStrings.map { string in let thing = string.capitalized return thing }

Slide 49

Slide 49 text

Swiftier let capitalizedStrings = arrayOfStrings.map { string in let thing = string.capitalized return thing } Swiftiest let capitalizedStrings = arrayOfStrings.map { $0.capitalized }

Slide 50

Slide 50 text

Limit your risk

Slide 51

Slide 51 text

A parable of going too far, too fast

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Be willing to pause and re-evaluate (before everything goes wrong)

Slide 60

Slide 60 text

Be willing to pause and re-evaluate (before everything goes wrong)

Slide 61

Slide 61 text

Step 3 Biggest ! For Your "

Slide 62

Slide 62 text

Step 3 Biggest ! For Your "

Slide 63

Slide 63 text

Look at your bugs and crashes

Slide 64

Slide 64 text

NPE

Slide 65

Slide 65 text

NPE + GoogleMaps

Slide 66

Slide 66 text

Look at your most error-prone code

Slide 67

Slide 67 text

Objective-C JSON Parsing - (instancetype)fromJSONData:(NSData *)data { NSDictionary* jsonObject = [NSJSONSerialization jsonObjectWithData:data error:nil]; if (jsonObject == nil]) { return nil; } user.firstName = dictionary[@"first_name"]; user.lastName = dictionary[@"last_name"]; user.age = [dictionary[@"age"] integerValue]; return user; }

Slide 68

Slide 68 text

Objective-C JSON Parsing - (instancetype)fromJSONData:(NSData *)data { NSDictionary* jsonObject = [NSJSONSerialization jsonObjectWithData:data error:nil]; if (jsonObject == nil || ![jsonObject isKindOfClass:[NSDictionary class]]) { return nil; } if ([dictionary[@"first_name"] isKindOfClass: [NSString class]]) { user.firstName = dictionary[@"first_name"]; } else { return nil; } if ([dictionary[@"last_name"] isKindOfClass: [NSString class]]) { user.lastName = dictionary[@"last_name"]; } else { return nil; } if ([dictionary[@"age"] isKindOfClass: [NSNumber class]]) { user.age = [dictionary[@"age"] integerValue]; } else { return nil; } return user; }

Slide 69

Slide 69 text

Swift 4 JSON Parsing static func from(jsonData: Data) -> User? { let decoder = JSONDecoder() return try? decoder.decode(User.self, for: jsonData) }

Slide 70

Slide 70 text

(This does require some setup) struct User: Codable { let firstName: String let lastName: String let age: Int enum CodingKeys: String, CodingKey { case firstName = "first_name", lastName = "last_name", age } }

Slide 71

Slide 71 text

Step 4 Advanced Features for Fun and Profit

Slide 72

Slide 72 text

Step 4 Advanced Features for Fun, Maintainability, and Profit

Slide 73

Slide 73 text

Make your code more reusable

Slide 74

Slide 74 text

Swift: Protocols!

Slide 75

Slide 75 text

Kotlin: Extensions!

Slide 76

Slide 76 text

Make your code safer

Slide 77

Slide 77 text

Swift: Version handling!

Slide 78

Slide 78 text

Kotlin: XML ids Become automatic variables

Slide 79

Slide 79 text

activity_profile.xml ProfileActivity.java TextView mUsernameTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile_activity); mUsernameTextView = (TextView) findViewById(R.id.textview_username); mUsernameTextView.setText("Hello, User"); }

Slide 80

Slide 80 text

activity_profile.xml ProfileActivity.java @BindView(R.id.textview_username) TextView mUsernameTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile_activity); mUsernameTextView.setText("Hello, User"); }

Slide 81

Slide 81 text

activity_profile.xml ProfileActivity.kt import kotlinx.android.synthetic.main.activity_profile.* public override fun onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_profile) textview_username.text = "Hello, User" }

Slide 82

Slide 82 text

activity_profile.xml ProfileActivity.kt import kotlinx.android.synthetic.main.activity_profile.* public override fun onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_profile) usernameTextView = "Hello, User" }

Slide 83

Slide 83 text

Things You Should Do at Every Step

Slide 84

Slide 84 text

Test

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Seriously though, Test

Slide 87

Slide 87 text

What was I even trying to do here?

Slide 88

Slide 88 text

Share

Slide 89

Slide 89 text

Share strategies

Slide 90

Slide 90 text

Share benefits

Slide 91

Slide 91 text

Share problems

Slide 92

Slide 92 text

Review

Slide 93

Slide 93 text

Learn by reading

Slide 94

Slide 94 text

Keep code clear

Slide 95

Slide 95 text

Swiftiest let capitalizedStrings = arrayOfStrings.map { $0.capitalized }

Slide 96

Slide 96 text

Unnecessarily One-Liniest let studentNames = students.reduce("") { $0 + "\n" + ($1.firstName ?? "") + " " + ($1.lastName ?? "") }

Slide 97

Slide 97 text

! Borderline Incomprehensible ! array.flatMap { $0 } .map { $0.doSomething() } .flatMap { $0 } .filter { $0.isSomething || $0.isSomethingElse } .reduce("") { $0 + $1.description }

Slide 98

Slide 98 text

"I don't understand this code" != "I don't understand this language"

Slide 99

Slide 99 text

Reducing confusion

Slide 100

Slide 100 text

Listen to the creators

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

No content

Slide 108

Slide 108 text

Listen skeptically to the creators

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Watch out for Yaks

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Never let the perfect be the enemy of the shipping

Slide 116

Slide 116 text

Obligatory Summary Slide → Honestly assess the pros and cons → Start small, then go bonkers once it works → Test the crap out of stuff in the new language → Share your code, your joy, and your pain → Resist desire to rewrite your whole app at once → Ship it!

Slide 117

Slide 117 text

Links → Concurrency In Swift: One Approach https://gist.github.com/lattner/ 31ed37682ef1576b16bca1432ea9f782 → Apple's Swift Blog https://developer.apple.com/swift/blog/ → Jetbrains' Kotlin Blog https://blog.jetbrains.com/kotlin/

Slide 118

Slide 118 text

Links → Kotlin Android Extensions https://kotlinlang.org/docs/tutorials/android- plugin.html

Slide 119

Slide 119 text

Videos → Anything You Can Do, I Can Do Better (Kickstarter team on FP in Kotlin vs. Swift) https://www.youtube.com/watch? v=_DuGaAkQSnM → iOS & Android & Swift & Kotlin (Stuart Kent on Kotlin for iOS Devs) http://www.stkent.com/2017/07/27/ios-and- android-and-swift-and-kotlin.html

Slide 120

Slide 120 text

Photo Credits → Paperwork, by Keith Williamson https://www.flickr.com/photos/elwillo/4729801304 → Simpsons screenshots + memes, Frinkiac https://frinkiac.com/