際際滷

際際滷Share a Scribd company logo
Radek

Pietruszewski
radex.io
油
油葵姻温糸艶恰沿
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
Swifty Methods, Swifty APIs
clarity  verbosity
naming things
stringByReplacingOccurrencesOfString:withString:
performSelectorOnMainThread:withObject:waitUntilDone:
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedI
[string componentsSeparatedByString:@n];
[string componentsSeparatedByString:@n];
huh?
NSComponent?
[string componentsSeparatedByString:@n];
100% verbose
95% clear
string.split(n)
95% clear
Adding more words doesn't help
What if youre not sure?
Swifty Methods, Swifty APIs
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()
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
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

More Related Content

Swifty Methods, Swifty APIs