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

  • View
    212

  • Download
    0

Embed Size (px)

Text of Great Artists Steal Orion Edwards M314 RubyScoping Lambda SwiftNullability C++Zero cost abstraction...

Great Artists StealOrion EdwardsM314Microsoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/2/2015 6:44 PM2Microsoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/2/2015 6:44 PM3It 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 youre doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve JobsMicrosoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/2/2015 6:44 PM4It 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 youre doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve JobsMicrosoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/2/2015 6:44 PM5TopicsRubyScoping LambdaSwiftNullabilityC++Zero cost abstractionObj-CNamingGrand Central DispatchGoChannelsContact me @borlandMicrosoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/2/2015 6:44 PM6Ruby Scoping lambdaFile.open 'test.txt', 'w' do |f| f.write "Hello World 3"endC# - Scoping lambdadatabase.Transaction(tx => { tx.Execute("delete from people where id = 5"); tx.Commit();});DEMOgithub.com/borland/Ignite2015Resource you really don't want to leakLock, TransactionBuild up, then commitTemplates, Batching

Scoping lambda:Things that don't need restricted lifetime(use IDisposable instead)Not for:Code is dataLispcanvas.Line(10, 10, 30, 30, true);Naming[canvas lineFromX:10 fromY:10 toX:30 toY:30 antialiased:true];

The method name islineFromX:fromY:toX:toY:antialiased:Objective-C namingcanvas.line(Color.Red, 30, 30, 100, 100);

canvas.Line( fromX:30, fromY:30, toX:100, toY:100, antialiased:true);C# namingThink about your parameter names as part of your method name, form a phraseSwift Optionalfunc readFile(path:String) -> String? { }

if let contents = readFile("/path/to/file") {print(contents)} else {print("no contents")}C# OptionalOptional ReadFile(string path) { }

ReadFile("path/to/file").Unwrap( c => Console.WriteLine(c), () => Console.WriteLine("no contents"));DEMOgithub.com/borland/Ignite2015Return type for methods that may return nullOptional:Function input parameters (use naming instead) Person(string name, string optionalAddress);

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

This pattern generalizes:C++ - Zero Cost Abstractiondouble x;

struct Metres { double Value;};8 bytes in memoryAlso 8 bytes in memoryC++ - 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 awayDEMOYou already use them (System.DateTime)Multiple data types in combinationE.g. Distance / SpeedWhen unit of measure is importantCurrencyWrapper structs:Standalone params (use naming) void Delay(int timeoutMilliseconds);Not for:The compiler should prevent you writing programs with bugsHaskellGrand Central DispatchApple's solution for concurrency

Queues are "lightweight virtual threads"

You "dispatch" lambda functions onto them

System manages the queuesGCD Main QueueDispatcher.BeginInvoke(() => { ShowAlert("hello");});dispatch_async(dispatch_get_main_queue()) { showAlert("hello")}C#GCD Concurrent QueuesTask.Run(() => { ProcessFile("path\\to\\file");});dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { processFile("path/to/file")}C#GCD Serial Queueslet q = dispatch_create_queue("myq", nil)

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

No builtin C# equivalentWorker Thread 1Worker Thread 2Q1Q1 Job1Q1 Job2Q1 Job3Q2Q3Serial QueueAsync DispatchWorker Thread 1Worker Thread 2Q1Q1 Job2Q1 Job3Q2Q3Serial QueueAsync DispatchQ1 Job1Worker Thread 1Worker Thread 2Q1Q1 Job3Q2Q3Serial QueueAsync DispatchQ1 Job2Q3 Job1Worker Thread 1Worker Thread 2Q1Q1 Job3Q2Q3Serial QueueAsync DispatchQ1 Job2Q3 Job1Worker Thread 1Worker Thread 2Q1Q1 Job3Q2Q3Serial QueueAsync DispatchQ1 Job2Q3 Job1If 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 ontoSynchronous DispatchC# - Serial Queuesvar q = new SerialQueue();

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

q.DispatchSync(FlushFileBuffers);DEMOgithub.com/borland/SerialQueueApple: everywhere instead of threadsTo manage an actual queue"upgrade" existing thread-based codeA simpler alternative to a thread or lockSerial Queues:Parallel work that doesn't share (use Task)Basic Locking (use lock)Not for:Go goroutines and channelsfunc main() { result := make(chan int) go multiply(10, 20, result) time.Sleep(1000 * time.Millisecond) fmt.Println("result was", a * b); async void Main() { int result = await MultiplyAsync(10, 20); WriteLine($"result was{result}");} func multiply( a int, b int, result chan int, messages chan string) { result