Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Better Swift from the Foundation up #tryswiftnyc17 09-06

212 views

Published on

Highlights of some useful Swift coding patterns and ways to avoid common sources of bugs and performance issues. These lessons learned are based on examination of Swift CoreLibs Foundation and other Open Source Swift libraries and projects, as he has contributed to helping bring Swift to the Server.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Better Swift from the Foundation up #tryswiftnyc17 09-06

  1. 1. Swift from the Foundation Up try!Swift NYC 2017 Illustration Renders by https://pixabay.com/en/users/3dman_eu-1553824/ Today’s talk brought to you by the number 502 and the letter Q
  2. 2. Obligatory Bio • Swift on the Server Developer at IBM • First iOS App in 2008, many projects since • Author, App Accomplished • Meetup Organizer • SwiftAustin & CocoaCoders • Parent @CarlBrwn
  3. 3. My Daughter at her first Hackathon
  4. 4. Compare/Contrast • Learning English • We read books to my Daughter for years • Then she read to herself for years • Then she started being expected to write • Learning Swift • A few screens of intro, then “Start Coding” *Note: I’m NOT picking on Playgrounds team - this is much better than most From Word World From Swift Playgrounds
  5. 5. Experiment: what we can learn by reading code?
  6. 6. Experiment: what we can learn by reading code?
  7. 7. Experiment: what we can learn by reading code?
  8. 8. Experiment: what we can learn by reading code?
  9. 9. Experiment: what we can learn by reading code?
  10. 10. Experiment: what we can learn by reading code? <100 {+ Lines - of Code
  11. 11. Experiment: what we can learn by reading code? <100 {+ Lines - of Code Comments Whitespace Formatting Swift Releases New Features Refactoring TestCode NO:
  12. 12. Experiment: what we can learn by reading code? <100 {+ Lines - of Code through July 2017 Comments Whitespace Formatting Swift Releases New Features Refactoring TestCode NO:
  13. 13. Experiment: what we can learn by reading code? <100 {+ Lines - of Code through July 2017 Comments Whitespace Formatting Swift Releases New Features Refactoring TestCode NO: =502 Pull Requests
  14. 14. Why would anyone do that?
  15. 15. Why would anyone do that?
  16. 16. Schadenfreude “Happiness at the misfortune of others.” (shad′ ’n froi′ dǝ)
  17. 17. Schadenfreude “Happiness at the misfortune of others.” (shad′ ’n froi′ dǝ) coding V Misquoted
  18. 18. Data for this Talk is on GitHub So don’t stress about trying to memorize all this github.com/carlbrown/SwiftPRInsights
  19. 19. 0 35 70 105 140 PR Fixes CodeOrdering Counting Encapsulation Logic(App) Memory Naming Optionals Performance Threading Typing Unclear PRs Meeting Criteria (502 total) ?
  20. 20. 0 35 70 105 140 PR Fixes CodeOrdering Counting Encapsulation Logic(App) Memory Naming Optionals Performance Threading Typing Unclear PRs Meeting Criteria (502 total) ? WARNING: Manual Classification
  21. 21. General vs Swift Swift Specific General Prog. App Specific Unclear
  22. 22. General vs Swift Swift Specific General Prog. App Specific Unclear
  23. 23. General Programming Fixes Swift Specific General Prog. App Specific Unclear –Jean-Baptiste Alphonse Karr (1849, translated from French) “The more things change, the more they stay the same.”
  24. 24. – Phil Karlton (as reported by Tim Bray) https://twitter.com/timbray/status/506146595650699264 “There are two hard things in computer science: cache invalidation and naming things.”
  25. 25. – Phil Karlton (as reported by Tim Bray) https://twitter.com/timbray/status/506146595650699264 “There are two hard things in computer science: cache invalidation and naming things … and off-by-1 errors.” – Leon Bambrick https://twitter.com/secretGeek/status/7269997868
  26. 26. General Programming Fix Types CodeOrdering Counting Encapsulation Naming Performance
  27. 27. Counting (9.8%) • Bounds, ranges and off-by-one errors are far too common • They’re also very easy to write tests for • Seems like you wouldn’t need to, but the statistics say otherwise
  28. 28. Performance (10.2%) • Handle common cases (or easy cases) early • Cut down on allocations, especially in loops • Use built-in Array constructors instead of loops/map • Reuse objects (but beware memory…)
  29. 29. Swift Specific Fixes Swift Specific General Prog. App Specific Unclear
  30. 30. Swift-Specific Fix Types Memory Threading(GCD) Typing/Casting Optionals
  31. 31. Memory (6.4%) • Use [weak] for closures. (Careful of lifetime of [unowned]). • Much bigger topic Server-Side • Less inherent organization/structure • Longer-lived processes • Lack of tooling when not on Darwin
  32. 32. Optionals (5.4%) • Nontrivial number of ‘!’ changed to ‘?’ or ‘if let’ • Some ‘nil’ initializations changed to Empty • Let the compiler help you
  33. 33. Threading/GCD (3.4%) • Multithreading is hard • Adding of Locks • `barrier` seems under-utilized • Can help with reader/writer
  34. 34. Why learn from other people’s code?
  35. 35. Why learn from other people’s code? Because when anyone ships stupid bugs:
  36. 36. Why learn from other people’s code? “It sucks to be me.” –Hopefully someone other than you Because when anyone ships stupid bugs:
  37. 37. In Closing
  38. 38. In Closing The Internet is Really, Really Great…
  39. 39. In Closing The Internet is Really, Really Great… Misquoted code examples we can learn from. for poor n, onacademic
  40. 40. In Closing The Internet is Really, Really Great… Misquoted code examples we can learn from. • Take your time • Get the easy 1s correct • Write your tests • Let the compiler help for poor n, onacademic
  41. 41. Thank You
  42. 42. Thank You

×