Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Radek Pietruszewski | Swifty Methods
Search
Swift Summit
March 22, 2015
Programming
2
7.6k
Radek Pietruszewski | Swifty Methods
Presented at
www.swiftsummit.com
Swift Summit
March 22, 2015
Tweet
Share
More Decks by Swift Summit
See All by Swift Summit
Jack Nutting | let swift = Race?
swiftsummit
1
2.7k
Marcin Krzyżanowski | CryptoSwift: Crypto You Can Do
swiftsummit
0
25k
Gem Barrett | View from the Other Side
swiftsummit
0
1.5k
Colin Eberhardt | ReactiveCocoa and Swift: Better Together
swiftsummit
3
14k
Joseph Lord | How Swift is Swift?
swiftsummit
2
31k
Al Skipp | The Monad Among Us
swiftsummit
3
780
Al Skipp | The Monad Among Us
swiftsummit
1
4.1k
Brian Gesiak | Swift API Design: Getting Results
swiftsummit
0
7.3k
Anthony Levings | JSON, Swift and Type Safety: It's a wrap
swiftsummit
2
19k
Other Decks in Programming
See All in Programming
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
960
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
Amazon Nova Reelの可能性
hideg
0
200
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
10
5.2k
AHC041解説
terryu16
0
400
良いユニットテストを書こう
mototakatsu
11
3.6k
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
1.9k
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
為你自己學 Python
eddie
0
520
Featured
See All Featured
Bash Introduction
62gerente
610
210k
The Invisible Side of Design
smashingmag
299
50k
How to Ace a Technical Interview
jacobian
276
23k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Scaling GitHub
holman
459
140k
What's in a price? How to price your products and services
michaelherold
244
12k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Transcript
Radek
“Programs must be written for people to read, and only
incidentally for machines to execute” — Structure and Interpretation of Computer Programs
“Programs must be written for people to read, and only
incidentally for machines to execute”
Clarity
Clever is dumb
Clarity is worth it
None
clarity ≠ verbosity
naming things
naming things stringByReplacingOccurrencesOfString:withString:
naming things stringByReplacingOccurrencesOfString:withString: performSelectorOnMainThread:withObject:waitUntilDone:
naming things stringByReplacingOccurrencesOfString:withString: performSelectorOnMainThread:withObject:waitUntilDone: tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedI
[string componentsSeparatedByString:@"\n"];
[string componentsSeparatedByString:@"\n"]; huh?
[string componentsSeparatedByString:@"\n"]; huh? NSComponent?
[string componentsSeparatedByString:@"\n"]; 95% clear
[string componentsSeparatedByString:@"\n"]; 100% verbose 95% clear
string.split("\n")
string.split("\n") 95% clear
Adding more words doesn't help
What if you’re not sure?
None
split componentsSeparatedByString
Why save bytes?
clarity > brevity
brevity ⊂ clarity
Verbosity ain't free
Reading takes effort
clear > confusing short > verbose
Find the sweet spot
Remove the noise
split componentsSeparatedByString
replace stringByReplacingOccurencesOfString:withString:
stringByReplacingOccurencesOfString:withString:
stringByReplacingOccurencesOfString:withString:
replace
Remove the noise
let today : NSDate = NSDate()
let today = NSDate()
ty(activityType: "hash_state") rInfo = ["hash": hash] SURL(string: fallbackURL) omeCurrent() ity
= activity ; ; ; ; ;
ty(activityType: "hash_state") rInfo = ["hash": hash] SURL(string: fallbackURL) omeCurrent() ity
= activity
let array: [Int] = [10, 6, 2] array.reduce(0, { (acc:
Int, el: Int) -> Int in return acc + el })
let array = [10, 6, 2] array.reduce(0, +)
string1 string2 [ isEqualToString: ]
string1 string2
string1 == string2
if (foo && foo.bar) { foo.bar.baz() }
foo?.bar?.baz()
less code to understand is a good thing
[[NSWindow alloc] initWithContentRect: frame styleMask: NSTitledWindowMask backing: NSBackingStoreBuffered defer: NO
screen: nil]
[[NSWindow alloc] initWithContentRect: frame styleMask: NSTitledWindowMask backing: NSBackingStoreBuffered defer: NO
screen: nil]
init( contentRect: NSRect, styleMask: NSWindowMask = .Titled, backing: NSBackingStoreType =
.Buffered, defer: Bool = false, screen: NSScreen? = nil)
[[NSWindow alloc] initWithContentRect: frame styleMask: NSTitledWindowMask backing: NSBackingStoreBuffered defer: NO
screen: nil]
NSWindow(contentRect: frame)
Swifty APIs
[NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(foo:) userInfo: nil repeats:
YES] . . . - (void) foo: (NSTimer *timer) { NSLog(@“Hello world!”) } radex.io/swift/nstimer
[NSTimer scheduledTimerWithTimeInterval:1.0] radex.io/swift/nstimer
[NSTimer scheduledTimerWithTimeInterval:1.0] radex.io/swift/nstimer
[NSTimer scheduledTimerWithTimeInterval:1.0] radex.io/swift/nstimer
[NSTimer scheduledTimerWithTimeInterval:1.0] radex.io/swift/nstimer
NSTimer.schedule(interval: 1.0) radex.io/swift/nstimer
NSTimer.schedule(interval: 1.0, target: self, selector: "foo:", userInfo: nil, repeats: true)
func foo(timer: NSTimer) { println("Hello world") } radex.io/swift/nstimer
NSTimer.schedule(interval: 1.0, userInfo: nil, repeats: true) { println("Hello world") }
radex.io/swift/nstimer
NSTimer.schedule(interval: 1.0, repeats: true) { println("Hello world") } radex.io/swift/nstimer
NSTimer.schedule(every: 1.0) { println("Hello world") } radex.io/swift/nstimer
NSTimer.schedule(every: 1.second) { println("Hello world") } radex.io/swift/nstimer
NSTimer.schedule(after: 1.second) { println("Hello world") } radex.io/swift/nstimer
[NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(foo:) userInfo: nil repeats:
YES] . . . - (void) foo: (NSTimer *timer) { NSLog(@“Hello world!”) } radex.io/swift/nstimer
NSTimer.schedule(every: 1.second) { println("Hello world") } radex.io/swift/nstimer
Recap: 4 ideas
Focus on clarity
Don't write clever code
clarity ≠ verbosity
None
radex.io/swift/methods @radexp