Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Emerging Best Practices in Swift

Ash Furrow
September 14, 2015

Emerging Best Practices in Swift

Ash Furrow

September 14, 2015
Tweet

More Decks by Ash Furrow

Other Decks in Programming

Transcript

  1. 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];
  2. 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];
  3. !

  4. After Object Literals NSArray *array = @[ @"This", @"is", @"much",

    @"better" ]; NSDictionary *dictionary = @{ @"Who likes this?": @"Me!" }; NSNumber *number = @(401);
  5. Blocks — iOS 4 introduced blocks and GCD. — Adopted...

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

    eventually. — Required new ideas. — Became a best practice. — Blocks now enable other best practices.
  7. Swift 2 — Lots of new syntax. — New syntax

    lets us do cool new things. — Like blocks, syntax is only a tool.
  8. If Overload if let thing = optionalThing { if thing.shouldDoThing

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

    otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) }
  10. 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?
  11. 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!
  12. 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 }
  13. 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.
  14. Extract associated values enum Result<T> { case Success(T) case Failure(reason:

    String) } ... switch doThing() { case .Success: print("!") case .Failure(let reason): print("Oops: \(reason)") }
  15. Syntax itself is not a best practice. The patterns enabled

    by syntax are what really matter. We need to discover them.
  16. 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.
  17. 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.