Better Swift from the Foundation up #tryswiftnyc17 09-06

42
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

Transcript of Better Swift from the Foundation up #tryswiftnyc17 09-06

Page 1: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

My Daughterat her first Hackathon

Page 4: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

Experiment: what we can learn by reading code?

Page 6: Better Swift from the Foundation up #tryswiftnyc17 09-06

Experiment: what we can learn by reading code?

Page 7: Better Swift from the Foundation up #tryswiftnyc17 09-06

Experiment: what we can learn by reading code?

Page 8: Better Swift from the Foundation up #tryswiftnyc17 09-06

Experiment: what we can learn by reading code?

Page 9: Better Swift from the Foundation up #tryswiftnyc17 09-06

Experiment: what we can learn by reading code?

Page 10: Better Swift from the Foundation up #tryswiftnyc17 09-06

Experiment: what we can learn by reading code?

<100 {+ Lines- of Code

Page 11: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

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

Why would anyone do that?

Page 15: Better Swift from the Foundation up #tryswiftnyc17 09-06

Why would anyone do that?

Page 16: Better Swift from the Foundation up #tryswiftnyc17 09-06

Schadenfreude

“Happiness at the misfortune of others.”

(shad′ ’n froi′ dǝ)

Page 17: Better Swift from the Foundation up #tryswiftnyc17 09-06

Schadenfreude

“Happiness at the misfortune of others.”

(shad′ ’n froi′ dǝ)

codingV

Misquoted

Page 18: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

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

General vs SwiftSwift Specific General Prog. App Specific Unclear

Page 22: Better Swift from the Foundation up #tryswiftnyc17 09-06

General vs SwiftSwift Specific General Prog. App Specific Unclear

Page 23: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

– 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

– 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

General Programming Fix TypesCodeOrdering Counting Encapsulation Naming Performance

Page 27: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

Swift Specific FixesSwift Specific General Prog. App Specific Unclear

Page 30: Better Swift from the Foundation up #tryswiftnyc17 09-06

Swift-Specific Fix TypesMemory Threading(GCD) Typing/Casting Optionals

Page 31: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

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

Why learn from other people’s code?

Page 35: Better Swift from the Foundation up #tryswiftnyc17 09-06

Why learn from other people’s code?Because when anyone

ships stupid bugs:

Page 36: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

In Closing

Page 38: Better Swift from the Foundation up #tryswiftnyc17 09-06

In ClosingThe Internet is Really, Really Great…

Page 39: Better Swift from the Foundation up #tryswiftnyc17 09-06

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

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

Thank You

Page 42: Better Swift from the Foundation up #tryswiftnyc17 09-06

Thank You