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. Emerging Best Practices in
    Swift
    Ash Furrow

    View Slide

  2. View Slide

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

    View Slide

  4. Everything turned out
    Fine

    View Slide

  5. Today, we're exploring best
    practices in Swift.

    View Slide

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

    View Slide

  7. Let's Go

    View Slide

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

    View Slide

  9. Wrong.

    View Slide

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

    View Slide

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

    View Slide

  12. 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];

    View Slide

  13. 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];

    View Slide

  14. !

    View Slide

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

    View Slide

  16. Adopted immediately.
    Clearly better.
    Became a best practice.

    View Slide

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

    View Slide

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

    View Slide

  19. Swift 2

    View Slide

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

    View Slide

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

    View Slide

  22. Should I use guard?

    View Slide

  23. What can I do with guard?

    View Slide

  24. Examples

    View Slide

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

    View Slide

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

    View Slide

  27. 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?

    View Slide

  28. 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!

    View Slide

  29. 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
    }

    View Slide

  30. 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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. That's all just syntax.

    View Slide

  36. Protocol-Oriented
    Programming

    View Slide

  37. Just go watch the WWDC video.

    View Slide

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

    View Slide

  39. Learning
    !

    View Slide

  40. Learning shouldn't just
    happen during the Xcode
    betas.

    View Slide

  41. Learning is a constant
    activity, a state of mind.

    View Slide

  42. 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.

    View Slide

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

    View Slide

  44. You should write a
    Blog

    View Slide

  45. Wrap-up

    View Slide

  46. 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.

    View Slide

  47. Make better
    mistakes tomorrow.

    View Slide