47

Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Embed Size (px)

Citation preview

Page 1: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels
Page 2: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Great Artists Steal

Orion Edwards M314

Page 3: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels
Page 4: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

It comes down to trying to expose yourself to the best things that humans have done and then try to bring those things in to what you’re doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve Jobs

Page 5: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

It comes down to trying to expose yourself to the best things that humans have done and then try to bring those things in to what you’re doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve Jobs

Page 6: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Topics

Ruby Scoping Lambda

Swift Nullability

C++ Zero cost abstraction

Obj-C Naming

Grand Central Dispatch

Go Channels

Contact me @borland

Page 7: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Ruby – Scoping lambdaFile.open 'test.txt', 'w' do |f| f.write "Hello World 3"end

Page 8: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C# - Scoping lambdadatabase.Transaction(tx => { tx.Execute("delete from people where id = 5"); tx.Commit();});

DEMO

github.com/borland/Ignite2015

Page 9: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Resource you really don't want to leakLock, Transaction

Build up, then commitTemplates, Batching

Scoping lambda:

Things that don't need restricted lifetime(use IDisposable instead)

Not for:

Page 10: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Code is data

Lisp

Page 11: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

canvas.Line(10, 10, 30, 30, true);

Naming

Page 12: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

[canvas lineFromX:10 fromY:10 toX:30 toY:30 antialiased:true];

The method name islineFromX:fromY:toX:toY:antialiased:

Objective-C – naming

Page 13: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

canvas.line(Color.Red, 30, 30, 100, 100);canvas.Line( fromX:30, fromY:30, toX:100, toY:100, antialiased:true);

C# – naming

Think about your parameter names as part of your method name, form a phrase

Page 14: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Swift – Optionalfunc readFile(path:String) -> String? { …}

if let contents = readFile("/path/to/file") {print(contents)

} else {print("no contents")

}

Page 15: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C# – OptionalOptional<string> ReadFile(string path) { …}

ReadFile("path/to/file").Unwrap( c => Console.WriteLine(c), () => Console.WriteLine("no contents"));

DEMO

github.com/borland/Ignite2015

Page 16: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Return type for methods that may return null

Optional:

Function input parameters (use naming instead) Person(string name, string optionalAddress);

Unwrap best for small things (use x.HasValue/Value otherwise)

Not for:

Page 17: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

HtmlEncoded<string>

Atomic<bool>

Tainted<string>

Either<string, int>

This pattern generalizes:

Page 18: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C++ - Zero Cost Abstractiondouble x;

struct Metres { double Value;};

8 bytes in memory

Also 8 bytes in memory

Page 19: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C++ - Zero Cost Abstractiondouble x;

struct Metres { // has overloaded + operator double Value;};

double x = 100;double x2 = x + 100;

Metres m(100);Metres m2 = m + Metres(100);Call overloaded operator +

Gets inlined and optimised away

DEMO

Page 20: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

You already use them (System.DateTime)

Multiple data types in combinationE.g. Distance / Speed

When unit of measure is importantCurrency

Wrapper structs:

Standalone params (use naming) void Delay(int timeoutMilliseconds);

Not for:

Page 21: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

The compiler should prevent

you writing programs with

bugs

Haskell

Page 22: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Grand Central DispatchApple's solution for concurrency

Queues are "lightweight virtual threads"

You "dispatch" lambda functions onto them

System manages the queues

Page 23: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

GCD – Main Queue

Dispatcher.BeginInvoke(() => { ShowAlert("hello");});

dispatch_async(dispatch_get_main_queue()) { showAlert("hello")}

C#

Page 24: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

GCD – Concurrent Queues

Task.Run(() => { ProcessFile("path\\to\\file");});

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { processFile("path/to/file")}

C#

Page 25: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

GCD – Serial Queues

let q = dispatch_create_queue("myq", nil)

dispatch_async(q) { processFile("path/to/file")}dispatch_sync(q) { flushFileBuffers()}

No builtin C# equivalent

Page 26: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Worker Thread 1

Worker Thread 2

Q1

Q1 Job1

Q1 Job2

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Page 27: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Worker Thread 1

Worker Thread 2

Q1

Q1 Job2

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job1

Page 28: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2

Page 29: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2

Q3 Job1

Page 30: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2 Q3 Job1

Page 31: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

If the queue is idle, it just locks, no thread switching

Else queues a request and waits for it

Serial queues are a lock that you can schedule things onto

Synchronous Dispatch

Page 32: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C# - Serial Queuesvar q = new SerialQueue();

q.DispatchAsync(() => {ProcessFile("path\\to\\file");

});

q.DispatchSync(FlushFileBuffers);

DEMO

github.com/borland/SerialQueue

Page 33: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Apple: everywhere instead of threadsTo manage an actual queue"upgrade" existing thread-based codeA simpler alternative to a thread or lock

Serial Queues:

Parallel work that doesn't share (use Task)

Basic Locking (use lock)

Not for:

Page 34: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Go – goroutines and channelsfunc main() { result := make(chan int) go multiply(10, 20, result) time.Sleep(1000 * time.Millisecond) fmt.Println("result was", <- result)}

func multiply(a int, b int, result chan int) {

result <- a * b}

Page 35: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Goroutine 1func main

Goroutine 2func multiply

result := make(chan int)go multiply(10, 20,result)

10 * 20Blocking Send to result channel

fmt.Println("off we go")

Read result channel

time.Sleep(…)

Println("result was", 200)

Page 36: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Goroutines are basically just Task.Run

Channels are interesting:send/receive allows message passing between tasks

Block the task for safetyBut not the OS thread, to achieve scalability

Has select to "switch" on multiple channels

Goroutines and channels:

Page 37: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Do not communicate by sharing memory;

instead, share memory by

communicating.

Go

Page 38: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C# – first takeTask<int> MultiplyAsync(int a, int b) => Task.Run(() => a * b); async void Main() { int result = await MultiplyAsync(10, 20); WriteLine($"result was{result}");}

Page 39: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

func multiply( a int, b int, result chan int, messages chan string) { result <- a * b messages <- "ok"}

Doesn't scale:

Page 40: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Tuple<Task<int>, Task<string>> MultiplyAsync(int a, int b) { ????????????}

Doesn't scale:C#:

Page 41: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

C# – Channels + awaitasync Task MultiplyAsync( int a, int b, Channel<int> result, Channel<string> messages){ await result.Send(a * b); await messages.Send("ok");}

DEMO

github.com/borland/Ignite2015

Page 42: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Async work which posts multiple resultsIf you like go If it helps you separate your concerns

Channels can work to enhance the TPL, not replace it

When to use Channels

Async work with a single result (just use Task)

Not for:

Page 43: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Never write finalizers in C#System.Runtime.InteropServices.SafeHandle classMicrosoft.Win32.SafeHandles namespace

System.Collections.ImmutableNuget package from the Microsoft .NET team

"Extension variables"ConditionalWeakTable class in System.Runtime.CompilerServices

Reactive ExtensionsRx-Main Nuget package from Microsoft for .NETRxcpp Nuget package for C++RxJS Nuget package for JavaScriptRxJava for Java

Honora

ble

M

entions

Page 44: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Related Ignite NZ Sessions

Torment Your Colleagues with the 'Roslyn' .NET Compiler PlatformNZ2 - Fri 1:55pm

What's new in Visual Studio 2015 and ALM 2015 – Part 2NZ2 - Wed 2:30pm

What's new in Visual Studio 2015 and ALM 2015 – Part 1NZ2 - Wed 1:55pm

TPL & async/await Best Practices for the Busy DevBallroom 1 - Wed 3:10pm

Find me later at… Hub Happy Hour Wed 5:30-6:30pm Hub Happy Hour Thu 5:30-6:30pm @borland on twitter

1

2

3

4

Page 45: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Complete your session evaluation now and win!

Page 46: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

Expose yourself to great ideas and do

great things

Page 47: Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction Obj-CNaming Grand Central Dispatch GoChannels

© 2015 Microsoft Corporation. All rights reserved.Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or

other countries.MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.