Slide 1

Slide 1 text

Emerging Best Practices in Swift Ash Furrow

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

I was Afraid That we'd just write Objective-C in Swift syntax.

Slide 4

Slide 4 text

Everything turned out Fine

Slide 5

Slide 5 text

Today, we're exploring best practices in Swift.

Slide 6

Slide 6 text

We've been here before. Swift 2 is significantly different. Always be learning.

Slide 7

Slide 7 text

Let's Go

Slide 8

Slide 8 text

Those who don't know history are doomed to repeat it. — Lots of people.

Slide 9

Slide 9 text

Wrong.

Slide 10

Slide 10 text

Those who don't know the past can't make informed decisions about the present. — Me

Slide 11

Slide 11 text

iOS 5 or Earlier? Let's see a show of hands.

Slide 12

Slide 12 text

Before Object Literals NSArray *array = [NSArray arrayWithObjects: @"This", @"is", @"so", @"tedious", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil]; NSNumber *number = [NSNumber numberWithInt: 401];

Slide 13

Slide 13 text

Before Object Literals and ARC NSArray *array = [[NSArray arrayWithObjects: @"This", @"is", @"so", @"tedious", nil] retain]; NSDictionary *dictionary = [[NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil] retain]; NSNumber *number = [[NSNumber numberWithInt: 401] retain];

Slide 14

Slide 14 text

!

Slide 15

Slide 15 text

After Object Literals NSArray *array = @[ @"This", @"is", @"much", @"better" ]; NSDictionary *dictionary = @{ @"Who likes this?": @"Me!" }; NSNumber *number = @(401);

Slide 16

Slide 16 text

Adopted immediately. Clearly better. Became a best practice.

Slide 17

Slide 17 text

Blocks — iOS 4 introduced blocks and GCD. — Adopted... eventually. — Required new ideas. — Became a best practice.

Slide 18

Slide 18 text

Blocks — iOS 4 introduced blocks and GCD. — Adopted... eventually. — Required new ideas. — Became a best practice. — Blocks now enable other best practices.

Slide 19

Slide 19 text

Swift 2

Slide 20

Slide 20 text

Swift 2 — Lots of new syntax. — New syntax lets us do cool new things. — Like blocks, syntax is only a tool.

Slide 21

Slide 21 text

Swift 2 — guard — defer — throws — etc...

Slide 22

Slide 22 text

Should I use guard?

Slide 23

Slide 23 text

What can I do with guard?

Slide 24

Slide 24 text

Examples

Slide 25

Slide 25 text

If Overload if let thing = optionalThing { if thing.shouldDoThing { if let otherThing = thing.otherThing { doStuffWithThing(otherThing) } } }

Slide 26

Slide 26 text

where to the rescue if let thing = optionalThing, let otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) }

Slide 27

Slide 27 text

where to the rescue if let thing = optionalThing, let otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool?

Slide 28

Slide 28 text

where to the rescue if let thing = optionalThing, let otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool? ... Neither do I. Let's look together!

Slide 29

Slide 29 text

Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String] { var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray }

Slide 30

Slide 30 text

Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String] { var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray } That's silly.

Slide 31

Slide 31 text

Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String] { return parameter.filter { $0.hasPrefix(prefix) } }

Slide 32

Slide 32 text

Extract associated values 1. Use Swift enums. 2. Attach associated values. 3. Extract using case.

Slide 33

Slide 33 text

Extract associated values enum Result { case Success(T) case Failure(reason: String) } ... switch doThing() { case .Success: print("!") case .Failure(let reason): print("Oops: \(reason)") }

Slide 34

Slide 34 text

Extract associated values if case .Success = doThing() { print("!") }

Slide 35

Slide 35 text

That's all just syntax.

Slide 36

Slide 36 text

Protocol-Oriented Programming

Slide 37

Slide 37 text

Just go watch the WWDC video.

Slide 38

Slide 38 text

Syntax itself is not a best practice. The patterns enabled by syntax are what really matter. We need to discover them.

Slide 39

Slide 39 text

Learning !

Slide 40

Slide 40 text

Learning shouldn't just happen during the Xcode betas.

Slide 41

Slide 41 text

Learning is a constant activity, a state of mind.

Slide 42

Slide 42 text

Learning — Look for code smells. — Ask yourself how you'd solve something differently. — Pick a Swift feature, ask "what could I do with this?" — Be comfortable throwing code away.

Slide 43

Slide 43 text

What about other communities? I bet they have good ideas, too...

Slide 44

Slide 44 text

You should write a Blog

Slide 45

Slide 45 text

Wrap-up

Slide 46

Slide 46 text

We have a history of being awesome, let's keep it up. Re-evaluate solutions to familiar problems. Always be learning. Also, write a blog.

Slide 47

Slide 47 text

Make better mistakes tomorrow.