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

Opinionated Code

Opinionated Code

Erica Sadun

Playgrounds

April 07, 2017
Tweet

More Decks by Playgrounds

Other Decks in Programming

Transcript

  1. WHY STYLE? ‣ Code should be invisible ‣ Focus on

    meaning ‣ Reduce cognitive load ‣
  2. WHY STYLE? ‣ Code should be invisible ‣ Focus on

    meaning ‣ Reduce cognitive load ‣
  3. WHY STYLE? ‣ Code should be invisible ‣ Focus on

    meaning ‣ Reduce cognitive load ‣ Uniformity
  4. WHY STYLE? ‣ Code should be invisible ‣ Focus on

    meaning ‣ Reduce cognitive load ‣ Uniformity
  5. // duplicates x++ defer { x = x + 1

    }; return x SEMICOLONS
  6. // duplicates x++ defer { x = x + 1

    }; return x SEMICOLONS
  7. // duplicates x++ defer { x = x + 1

    }; return x SEMICOLONS
  8. UNDERSCORES let largeInteger = 1732500 // harder to inspect let

    largeInteger = 1_732_500 // easily reviewed
  9. UNDERSCORES let largeInteger = 0xffffff // 5 f's or 6?

    let largeInteger = 0xff_ff_ff // easily reviewed
  10. TYPING var array: [String] = [] // yes var dictionary:

    [AnyHashable: Any] = [:] // yes var aSet: Set<String> = [] // yes var array = [String]() // no var array = Array<String>() // no var dictionary = [AnyHashable: Any]() // no var dictionary = Dictionary<AnyHashable: Any>() // no var aSet = Set<String>() // no
  11. TYPING var array: [String] = [] // yes var dictionary:

    [AnyHashable: Any] = [:] // yes var aSet: Set<String> = [] // yes var array = [String]() // no var array = Array<String>() // no var dictionary = [AnyHashable: Any]() // no var dictionary = Dictionary<AnyHashable: Any>() // no var aSet = Set<String>() // no
  12. TYPING var array: [String] = ["a", "b", "c"] var aSet:

    Set<String> = ["a", "b", "c"] var array = [String]() // no array = ["a", "b", "c"] var array = Set<String>() // no array = ["a", "b", "c"]
  13. TYPING let point = CGPoint(x: 15, y: 20) // fine

    let point: CGPoint = CGPoint(x: 15, y: 20) // wordy
  14. TYPING let point = CGPoint(x: 15, y: 20) // fine

    let point: CGPoint = CGPoint(x: 15, y: 20) // wordy
  15. TYPING let point = CGPoint(x: 15, y: 20) // fine

    let point: CGPoint = CGPoint(x: 15, y: 20) // wordy
  16. OPTIONAL SUGAR switch (firstOptional , secondOptional) { // no case

    let (.some(first), .some(second)): ... ... default: break } switch (firstOptional , secondOptional) { // yes case let (first?, second?): ... ... default: break }
  17. OPTIONAL SUGAR let optItems: [Int?] = [1, nil, 5, 2,

    4, nil] for case let item? in optItems { print(item) } for case .some(let item) in optItems { print(item) }
  18. OPTIONAL SUGAR let optItems: [Int?] = [1, nil, 5, 2,

    4, nil] for case let item? in optItems { // no print(item) } for item in optItems.flatMap({ $0 }) { // yes print(item) }
  19. OPTIONAL SUGAR enum Result<T> { case success(T), error(Error) } guard

    case let .success(value) = result else { return } // yes print(value)
  20. Focus on Meaning Simplify Inspection Think Readable Be Consistent Be

    Uniform Develop Swift in Swift Lessons learned
  21. Focus on Meaning Simplify Inspection Think Readable Prefer Sugar Be

    Consistent Be Uniform Develop Swift in Swift Lessons learned
  22. Focus on Meaning Simplify Inspection Think Readable Prefer Sugar Be

    Consistent Be Uniform Develop Swift in Swift Avoid Tunnel Vision Lessons learned