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

Swift 3 API Best Practices by Example

Swift 3 API Best Practices by Example

The slide of my "Swift 3 API Best Practices" talk at Cocoaheads Stockholm #74 https://www.meetup.com/CocoaHeads-Stockholm/events/234037606/

Reda Lemeden

October 03, 2016
Tweet

Other Decks in Programming

Transcript

  1. Why? • Consistency with Cocoa/Foundation. Apple is leading by example.

    • Conventions make code easier to read and review. • Improving clarity and readability. • Paramount for open-source maintainers.
  2. Add just enough to not be ambiguous. Keep just enough

    to not be redundant. THE GOLDEN RULE
  3. func frame(at index: Int) -> UIImage? func f(_ i: Int)

    -> UIImage? func frame(_ i: Int) -> UIImage? func frame(_ index: Int) -> UIImage? func frame(atIndex index: Int) -> UIImage? frame(at: 2) f(2) frame(2) frame(2) frame(atIndex: 2) Declaration Call Site func frameAtIndex(index: Int -> UIImage? frameAtIndex(index: 2)
  4. func frame(at index: Int) -> UIImage? func f(_ i: Int)

    -> UIImage? func frame(_ i: Int) -> UIImage? func frame(_ index: Int) -> UIImage? func frame(atIndex index: Int) -> UIImage? frame(at: 2) f(2) frame(2) frame(2) frame(atIndex: 2) Declaration Call Site func frameAtIndex(index: Int -> UIImage? frameAtIndex(index: 2)
  5. func frame(at index: Int) -> UIImage? func image(at index: Int)

    -> UIImage? frame(at: 2) image(at: 2) Declaration Call Site
  6. func frame(at index: Int) -> UIImage? func image(at index: Int)

    -> UIImage? frame(at: 2) image(at: 2) Declaration Call Site
  7. func frame(at index: Int) -> UIImage? func frame(atIndex index: Int)

    -> UIImage? frame(at: 2) frame(atIndex: 2) Declaration Call Site
  8. func frame(at index: Int) -> UIImage? func frame(atIndex index: Int)

    -> UIImage? frame(at: 2) frame(atIndex: 2) Declaration Call Site
  9. func frame(at index: Int) -> UIImage? func frameAt(_ index: Int)

    -> UIImage? frame(at: 2) frameAt(2) Declaration Call Site
  10. func frame(at index: Int) -> UIImage? func frameAt(_ index: Int)

    -> UIImage? frame(at: 2) frameAt(2) Declaration Call Site
  11. func frame(at index: Int) -> UIImage? func decodeFrame(at index: Int)

    -> UIImage? frame(at: 2) decodeFrame(at: 2) Declaration Call Site
  12. func frame(at index: Int) -> UIImage? func decodeFrame(at index: Int)

    -> UIImage? frame(at: 2) decodeFrame(at: 2) Declaration Call Site
  13. func frame(at index: Int) -> UIImage? func frame(aPosition index: Int)

    -> UIImage? frame(at: 2) frame(aPosition: 2) Declaration Call Site
  14. func frame(at index: Int) -> UIImage? func frame(aPosition index: Int)

    -> UIImage? frame(at: 2) frame(aPosition: 2) Declaration Call Site
  15. func prepareForAnimation(with data: Data) prepareForAnimation(with: data) Declaration Call Site func

    prepareForAnimation(withGIFData data: Data) prepareForAnimation(withGIFData: data)
  16. func prepareForAnimation(with data: Data) prepareForAnimation(with: data) Declaration Call Site func

    prepareForAnimation(withGIFData data: Data) prepareForAnimation(withGIFData: data)
  17. func animatedFrame(with newImage: UIImage?) -> AnimatedFrame frame.animatedFrame(with: image) Declaration Call

    Site func makeAnimatedFrame(with newImage: UIImage?) -> AnimatedFrame frame.makeAnimatedFrame(with: image)
  18. func animatedFrame(with newImage: UIImage?) -> AnimatedFrame frame.animatedFrame(with: image) Declaration Call

    Site func makeAnimatedFrame(with newImage: UIImage?) -> AnimatedFrame frame.makeAnimatedFrame(with: image)
  19. func constrain(by size: CGSize) size.constrain(by: size) Declaration – CGSize Extension

    Call Site func constrained(by size: CGSize) size.constrained(by: size)
  20. func constrain(by size: CGSize) size.constrain(by: size) Declaration – CGSize Extension

    Call Site func constrained(by size: CGSize) size.constrained(by: size)
  21. var animating: Bool view.animating Declaration Call Site var isAnimating: Bool

    View.isAnimating var isNotAnimating: Bool var notAnimating: Bool view.notAnimating View.isNotAnimating
  22. var animating: Bool view.animating Declaration Call Site var isAnimating: Bool

    View.isAnimating var isNotAnimating: Bool var notAnimating: Bool view.notAnimating View.isNotAnimating
  23. func frameDur(prop: GIFProperties) -> Double? frameDur(prop: properties) Declaration Call Site

    func frameDuration(with properties: GIFProperties) -> Double? frameDuration(with: properties)
  24. func frameDur(prop: GIFProperties) -> Double? frameDur(prop: properties) Declaration Call Site

    func frameDuration(with properties: GIFProperties) -> Double? frameDuration(with: properties)
  25. var GifImageSource: CGImageSource Declaration var GIFImageSource: CGImageSource var gifImageSource: CGImageSource

    func prepareForAnimation(withGIFData data: Data) func prepareForAnimation(withGifData data: Data) func prepareForAnimation(withgifData data: Data)
  26. var GifImageSource: CGImageSource Declaration var GIFImageSource: CGImageSource var gifImageSource: CGImageSource

    func prepareForAnimation(withGIFData data: Data) func prepareForAnimation(withGifData data: Data) func prepareForAnimation(withgifData data: Data)
  27. Always document your code. A must for code meant to

    be used by others (SDKs, open-source, etc.)
  28. func prepareForAnimation(withGIFNamed name: String, size: CGSize = .zero, contentMode: UIViewContentMode?

    = nil) Declaration func prepareForAnimation(withGIFNamed: “cocoaheads”) Call Site func prepareForAnimation(withGIFNamed: “cocoaheads”, size: CGSize(width: 1, height: 1))
  29. Useful Links Session 403 - WWDC ‘16: https://developer.apple.com/videos/play/wwdc2016/403/ Swift API

    Design Guidelines: https://swift.org/documentation/api-design-guidelines/ SE-0023 - Swift Evolution: https://github.com/apple/swift-evolution/ blob/master/proposals/0023-api-guidelines.md