Better Swift from the Foundation up #tryswiftnyc17 09-06
-
Upload
carl-brown -
Category
Technology
-
view
191 -
download
1
Transcript of Better Swift from the Foundation up #tryswiftnyc17 09-06
![Page 1: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/1.jpg)
Swift from the Foundation Uptry!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
![Page 2: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/2.jpg)
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
![Page 3: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/3.jpg)
My Daughterat her first Hackathon
![Page 4: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/4.jpg)
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
![Page 5: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/5.jpg)
Experiment: what we can learn by reading code?
![Page 6: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/6.jpg)
Experiment: what we can learn by reading code?
![Page 7: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/7.jpg)
Experiment: what we can learn by reading code?
![Page 8: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/8.jpg)
Experiment: what we can learn by reading code?
![Page 9: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/9.jpg)
Experiment: what we can learn by reading code?
![Page 10: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/10.jpg)
Experiment: what we can learn by reading code?
<100 {+ Lines- of Code
![Page 11: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/11.jpg)
Experiment: what we can learn by reading code?
<100 {+ Lines- of Code
Comments Whitespace Formatting Swift Releases New Features Refactoring TestCode
NO:
![Page 12: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/12.jpg)
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:
![Page 13: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/13.jpg)
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
![Page 14: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/14.jpg)
Why would anyone do that?
![Page 15: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/15.jpg)
Why would anyone do that?
![Page 16: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/16.jpg)
Schadenfreude
“Happiness at the misfortune of others.”
(shad′ ’n froi′ dǝ)
![Page 17: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/17.jpg)
Schadenfreude
“Happiness at the misfortune of others.”
(shad′ ’n froi′ dǝ)
codingV
Misquoted
![Page 18: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/18.jpg)
Data for this Talk is on GitHubSo don’t stress about trying to memorize all this
github.com/carlbrown/SwiftPRInsights
![Page 19: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/19.jpg)
0
35
70
105
140
PR Fixes
CodeOrdering Counting Encapsulation Logic(App) MemoryNaming Optionals Performance Threading TypingUnclear
PRs Meeting Criteria (502 total)
?
![Page 20: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/20.jpg)
0
35
70
105
140
PR Fixes
CodeOrdering Counting Encapsulation Logic(App) MemoryNaming Optionals Performance Threading TypingUnclear
PRs Meeting Criteria (502 total)
?WARNING: Manual Classification
![Page 21: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/21.jpg)
General vs SwiftSwift Specific General Prog. App Specific Unclear
![Page 22: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/22.jpg)
General vs SwiftSwift Specific General Prog. App Specific Unclear
![Page 23: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/23.jpg)
General Programming FixesSwift Specific General Prog. App Specific Unclear
–Jean-Baptiste Alphonse Karr (1849, translated from French)
“The more things change, the more they stay the same.”
![Page 24: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/24.jpg)
– 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 .”
![Page 25: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/25.jpg)
– 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
![Page 26: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/26.jpg)
General Programming Fix TypesCodeOrdering Counting Encapsulation Naming Performance
![Page 27: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/27.jpg)
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
![Page 28: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/28.jpg)
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…)
![Page 29: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/29.jpg)
Swift Specific FixesSwift Specific General Prog. App Specific Unclear
![Page 30: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/30.jpg)
Swift-Specific Fix TypesMemory Threading(GCD) Typing/Casting Optionals
![Page 31: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/31.jpg)
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
![Page 32: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/32.jpg)
Optionals (5.4%)
• Nontrivial number of ‘!’ changed to ‘?’ or ‘if let’
• Some ‘nil’ initializations changed to Empty
• Let the compiler help you
![Page 33: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/33.jpg)
Threading/GCD (3.4%)
• Multithreading is hard
• Adding of Locks
• `barrier` seems under-utilized
• Can help with reader/writer
![Page 34: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/34.jpg)
Why learn from other people’s code?
![Page 35: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/35.jpg)
Why learn from other people’s code?Because when anyone
ships stupid bugs:
![Page 36: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/36.jpg)
Why learn from other people’s code?
“It sucks to be me.” –Hopefully someone other than you
Because when anyone ships stupid bugs:
![Page 37: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/37.jpg)
In Closing
![Page 38: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/38.jpg)
In ClosingThe Internet is Really, Really Great…
![Page 39: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/39.jpg)
In ClosingThe Internet is Really, Really Great…
Misquoted
code examples we can learn from.for poor n, onacademic
![Page 40: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/40.jpg)
In ClosingThe 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
![Page 41: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/41.jpg)
Thank You
![Page 42: Better Swift from the Foundation up #tryswiftnyc17 09-06](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cd/html5/thumbnails/42.jpg)
Thank You