Slide 1

Slide 1 text

Safer Swift Code with Value Types Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 1

Slide 2

Slide 2 text

What do I mean by safety? Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 2

Slide 3

Slide 3 text

Agenda 1. What are Value Types vs. Reference Types 2. Why is this topic relevant now? 3. How: A Practical Example of a Value Oriented Architecture Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 3

Slide 4

Slide 4 text

Values vs. References Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 4

Slide 5

Slide 5 text

Reference Types class PersonRefType { let name:String var age:Int // .. } // 1 let peter = PersonRefType(name: "Peter", age: 36) // 2 let peter2 = peter // 3 peter2.age = 25 // peter {"Peter", 25} // peter2 {"Peter", 25} Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 5

Slide 6

Slide 6 text

Value Types struct Person { let name:String var age:Int } // 1 let petra = Person(name:"Petra", age:25) // 2 var petra2 = petra // 3 petra2.age = 20 // petra {"Petra", 25} // petra2 {"Petra", 20} Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 6

Slide 7

Slide 7 text

Why now? Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 7

Slide 8

Slide 8 text

Foundation / C Types Reference Types: - NSArray - NSSet - NSData Value Types: - NSInteger - Struct Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 8

Slide 9

Slide 9 text

Swift Standard Library Reference Types: - ManagedBuffer(?) - NonObjectiveCBase(??) Value Types: - Array - String - Optional Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 9

Slide 10

Slide 10 text

Enums and Structs in Swift are Powerful — Can have properties — Can have method — Can conform to protocols Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 10

Slide 11

Slide 11 text

So What Can't They Do? “Indeed, in contrast to structs, Swift classes support implementation inheritance, (limited) reflection, deinitializers, and multiple owners.” Andy Matushak1 1 http://www.objc.io/issue-16/swift-classes-vs-structs.html Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 11

Slide 12

Slide 12 text

We've Already Been Doing This! @property (copy) NSString *userName; Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 12

Slide 13

Slide 13 text

Case Study A Twitter Client Built on Immutable Value Types Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 13

Slide 14

Slide 14 text

Twitter Client 1. Download the latest 200 tweets and display them 2. Allow to filter tweets (RT only, favorited tweets only, etc.) 3. Allow user to favorite tweets (should be synced with server) Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 14

Slide 15

Slide 15 text

Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 15

Slide 16

Slide 16 text

How can we favorite Tweets? Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 16

Slide 17

Slide 17 text

It Is Very Simple with OOP tweet.favorited = true Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 17

Slide 18

Slide 18 text

It Is Simple with OOP let lockQueue = dispatch_queue_create("com.happylocking", nil) dispatch_sync(lockQueue) { tweet.favorited = true } Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 18

Slide 19

Slide 19 text

Is It Simple with OOP? let lockQueue = dispatch_queue_create("com.happylocking", nil) dispatch_sync(lockQueue) { tweet.favorited = true NSNotificationCenter.defaultCenter(). postNotificationName("Tweet Changed", object: tweet) } Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 19

Slide 20

Slide 20 text

Modeling Change is Hard! let lockQueue = dispatch_queue_create("com.happylocking", nil) dispatch_sync(lockQueue) { tweet.favorited = true NSNotificationCenter.defaultCenter(). postNotificationName("Tweet Changed", object: tweet) tweetAPIClient.markFavorited(tweet.identifier) } Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 20

Slide 21

Slide 21 text

Modeling Change is Hard! — Protect against unwanted updates — Distribute new value throughout application — Understand what the underlying identity of an object is and perform update accordingly Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 21

Slide 22

Slide 22 text

Modeling Change is Hard! — Protect against unwanted updates — Distribute new value throughout application — Understand what the underlying identity of an object is and perform update accordingly -> We need to this in all places where we mutate values! Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 22

Slide 23

Slide 23 text

Modelling Change is Hard! Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 23

Slide 24

Slide 24 text

How Can We Model Change With Immutable Value Types? Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 24

Slide 25

Slide 25 text

How Can We Model Change With Immutable Value Types? Model change to values as values! Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 25

Slide 26

Slide 26 text

How Can We Model Change With Immutable Value Types? Model change to values as values: — Create a new Tweet for every change — Save these changes in a Store — Store saves local changes and server state — Store provides a merged view on list of tweets — Store can trigger sync of local state to server Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 26

Slide 27

Slide 27 text

Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 27

Slide 28

Slide 28 text

Favoriting a Tweet let currentTweet = tweetTableViewCell.tweet! let newTweet = Tweet( content: currentTweet.content, identifier: currentTweet.identifier, user: currentTweet.user, type: currentTweet.type, favoriteCount: currentTweet.favoriteCount, isFavorited: !currentTweet.isFavorited ) store.addTweetChangeToLocalState(newTweet) Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 28

Slide 29

Slide 29 text

Modelling Change in Stores class TweetStore { var tweets: [Tweet]? { get { // merge server list and local list } } func addTweetChangeToLocalState(tweet: Tweet) { // append tweet to local list } func loadTweets() -> Promise<[Tweet]> { // trigger API request, populate server list } Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 29

Slide 30

Slide 30 text

Syncing Change Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 30

Slide 31

Slide 31 text

Syncing Change 1. Iterate over each local change 2. Generate API request that syncs that local change to server 3. Upon each API response: — If success: remove tweet from local change set — If failure: leave tweet in local change set Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 31

Slide 32

Slide 32 text

Syncing Change protocol StoreSync { typealias StoreType static func syncLocalState(merge: StateMerge) -> Promise> } struct StateMerge { let serverState: [T] let localState: [T] } enum SyncResult { case Success(StateMerge) case Error(StateMerge) } Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 32

Slide 33

Slide 33 text

Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 33

Slide 34

Slide 34 text

Benefits of a Value Oriented Architecture — Confidence that no one will change our data under the covers — Change propagation needs to be handled explicitly — Modeling change as data opens opportunities: — Undo Functionality — Sophisticated conflict resolution Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 34

Slide 35

Slide 35 text

The Value Mindset Example project: https://github.com/Ben-G/TwitterSwift Related, great talks: - https://realm.io/news/andy-matuschak-controlling- complexity/ - http://www.infoq.com/presentations/Value-Values Safer Swift Code with Value Types | @benjaminencz | AltConf, June 2015 35