swifty methods, swifty apis

66
Radek Pietruszewski radex.io • @radexp

Upload: radexp

Post on 15-Jul-2015

328 views

Category:

Software


1 download

TRANSCRIPT

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

Clever is dumb

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

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()

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() }

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

radex.io/swift/methods

@radexp