Radek����������� ������������������ Pietruszewski
radex.io����������� ������������������ •����������� ������������������ @radexp
“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 is worth it
clarity ≠ verbosity
naming things
stringByReplacingOccurrencesOfString:withString:
performSelectorOnMainThread:withObject:waitUntilDone:
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
[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 you’re not sure?
split
componentsSeparatedByString
clarity > brevity
brevity ⊂ clarity
Verbosity ain't free
Reading takes effort
clear > confusing short > verbose
Find the sweet spot
split
componentsSeparatedByString
replace
stringByReplacingOccurencesOfString:withString:
stringByReplacingOccurencesOfString:withString:
stringByReplacingOccurencesOfString:withString:
let today : NSDate = NSDate()
NSUserActivity(activityType: "hash_state") activity.userInfo = ["hash": hash] activity = NSURL(string: fallbackURL) activity.becomeCurrent() currentActivity = 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() }
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)
[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
Don't write clever code
clarity ≠ verbosity
radex.io/swift/methods
@radexp