Want To Be a Better Programmer? Lars Bak and Kasper Lund, Inventors - - PowerPoint PPT Presentation

want to be a better programmer
SMART_READER_LITE
LIVE PREVIEW

Want To Be a Better Programmer? Lars Bak and Kasper Lund, Inventors - - PowerPoint PPT Presentation

Want To Be a Better Programmer? Lars Bak and Kasper Lund, Inventors of Dart, Software engineers at Google Combined Experiences JavaScript - We joined Google in 2006 to improve JavaScript performance - Innovation, open source, and friendly


slide-1
SLIDE 1

Lars Bak and Kasper Lund, Inventors of Dart, Software engineers at Google

Want To Be a Better Programmer?

slide-2
SLIDE 2

Combined Experiences

slide-3
SLIDE 3

JavaScript

  • We joined Google in 2006 to improve JavaScript performance
  • Innovation, open source, and friendly competition made JavaScript >100x faster

Performance New features I n n

  • v

a t i

  • n

i n A p p s

slide-4
SLIDE 4

Did that make you a better programmer?

slide-5
SLIDE 5

Hmm…

var data = [0,1,2,3,4,5,6,7,8,9,0]; var opacity; for(var i=0; i<data.length && i<10; i++){

  • pacity = .5;

if(i=0)

  • pacity = 1;

}

slide-6
SLIDE 6

Okay…

if('Function' == Object.prototype.toString.call(this._storeUnsubscribe).slice(8, -1)) { // Do something }

slide-7
SLIDE 7

Clearly we made you a better programmer!

slide-8
SLIDE 8

…. NOT

slide-9
SLIDE 9

Wasted Effort? Nope!

  • Faster JavaScript enables innovation on the web

○ Enables richer frameworks and better abstractions ○ Allows for much larger applications

  • Developers still suffer from puzzling semantics and hard-to-identify errors

○ There is almost no declarative syntax and dependencies are hard to find ○ Errors are often absorbed and values are implicitly converted

slide-10
SLIDE 10

How Do People Get By Today?

  • TypeScript
  • CoffeeScript
  • Ceylon
  • Scala.js
  • Haxe
  • Elm
  • ClojureScript
  • GWT and Closure compiler
  • r Dart
slide-11
SLIDE 11

What Makes You a Better Programmer?

Simplicity and consistency! So we need:

  • A simple and consistent programming language
  • A simple and consistent application framework
slide-12
SLIDE 12

Simple language semantics

slide-13
SLIDE 13

The Dart Language Introduction in One Slide

  • Unsurprising object-oriented programming language
  • Class-based single inheritance with interfaces and mixins
  • Familiar syntax with proper lexical scoping
  • Single-threaded with isolate-based concurrency
  • Optional static types
slide-14
SLIDE 14

Puzzling Value Coercion in JavaScript

print(2.0 == '2' == new Boolean(true) == '1')

$ v8 print.js ??? $ console

Implicit conversions will make your head explode

slide-15
SLIDE 15

Puzzling Value Coercion in JavaScript

print(2.0 == '2' == new Boolean(true) == '1')

$ v8 print.js true $ console

Implicit conversions will make your head explode

slide-16
SLIDE 16

Dart is an Open Standard (ECMA)

  • Governed by a technical committee (TC52) since 2013
  • Three editions of the specification have been published

June, 2014 First edition December, 2014 Enumerations, asynchrony support, and deferred loading June, 2015 Null-aware operators and generalized tear-offs

slide-17
SLIDE 17

Constructors

slide-18
SLIDE 18

Constructors in Object-oriented Languages

Simple example in:

  • C++
  • Java
  • Dart

A B

f() .. f() ..

slide-19
SLIDE 19

Constructors in C++

#include <stdio.h> class A { public: A() { f(); } virtual void f() { printf("A\n"); } }; class B : public A { public: B() : A() { f(); } void f() { printf("B\n"); } }; int main() { B b; }

$ ./a.out A B $ console

slide-20
SLIDE 20

Constructors in Java

class A { A() { f(); } void f() { System.out.println("A"); } }; class B extends A { B() { f(); } void f() { System.out.println("B"); } }; public class Prog { public static void main(String[] args) { new B(); } }

$ java Prog B B $ console

slide-21
SLIDE 21

Constructors in Java

class A { A() { f(); } void f() { System.out.println("A"); } }; class B extends A { B() { f(); } final String x = "B"; void f() { System.out.println(x); } }; public class Prog { public static void main(String[] args) { new B(); } }

$ java Prog B B $ console

slide-22
SLIDE 22

Constructors in Java

class A { A() { f(); } void f() { System.out.println("A"); } }; class B extends A { B() { f(); } final String x = "B".trim(); void f() { System.out.println(x); } }; public class Prog { public static void main(String[] args) { new B(); } }

$ java Prog ??? B $ console

slide-23
SLIDE 23

Constructors in Java

class A { A() { f(); } void f() { System.out.println("A"); } }; class B extends A { B() { f(); } final String x = "B".trim(); void f() { System.out.println(x); } }; public class Prog { public static void main(String[] args) { new B(); } }

$ java Prog null B $ console

slide-24
SLIDE 24

Constructors in Dart

class A { A() { f(); } f() => print("A"); } class B extends A { B() { f(); } final x = "B".trim(); f() => print(x); } main() => new B();

$ dart prog.dart B B $ console

slide-25
SLIDE 25

Clean Semantics Make You Better

In Dart, constructors enforce two pass initialization

  • All fields are initialized before ...
  • … constructor bodies are executed
slide-26
SLIDE 26

Constructors in Dart

class Symbol { final String name; static Map<String, Symbol> _cache = <String, Symbol>{}; factory Symbol(String name) { if (_cache.containsKey(name)) return _cache[name]; return _cache[name] = new Symbol._internal(name); } Symbol._internal(this.name); } main() { print(new Symbol("X") == new Symbol("X")); }

slide-27
SLIDE 27

Boilerplate

slide-28
SLIDE 28

Simple Constructors

class Point { final num x, y; Point(this.x, this.y); } public class Point { public final double x, y; public Point(double x, double y) { this.x = x; this.y = y; } }

Dart Java

slide-29
SLIDE 29

No Need for Explicit Accessors

class Point { final num x, y; Point(this.x, this.y); } public class Point { private final double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } }

Dart Java

intentional overflow

slide-30
SLIDE 30

Classes Can Act as Interfaces

class PolarPoint implements Point { num get x => ...; num get y => ...; } public interface Point { ... } public class CartesianPoint implements Point { ... } public class PolarPoint implements Point { ... }

Dart Java

intentional overflow

slide-31
SLIDE 31

Generic Types Are Reified

List<String> listify(Set<String> s) => s.toList(); String[] listify(Set<String> s) { return s.toArray( new String[s.size()]); }

Dart Java

slide-32
SLIDE 32

Cascaded Calls

void drawCircle(CanvasElement canvas, int x, int y, int size) { canvas.context..beginPath() ..arc(x, y, size, 0, PI * 2, false) ..fill() ..closePath() ..stroke(); }

slide-33
SLIDE 33

Cascaded Calls for Initialization

Set initialSet() => new Set()..add(42);

slide-34
SLIDE 34

Cascaded Method Invocations

  • Enables the programmer to apply method chaining to any object
  • Expression always returns cascaded receiver object
  • Inspiration from Smalltalk
slide-35
SLIDE 35

Asynchrony FTW

slide-36
SLIDE 36

What About IO?

  • Browser enforces single threaded

execution

  • Blocking IO would allow one
  • peration at a time

○ … and kill responsiveness

  • Why not solve it with multi-threading?

readWrite() { try { var c = read(); write(c); } catch (e) { handleError(e); } finally { close(); } }

Synchronous code:

slide-37
SLIDE 37

Asynchronous Callback

Synchronous code:

readWrite() { try { var c = read(); write(c); } catch (e) { handleError(e); } finally { close(); } } readWrite() { read((c) { write(c, handleError); }, handleError); } // Finally block cannot be handled. // Easy to make mistakes in error handling. // … and fairly unreadable.

slide-38
SLIDE 38

Futures Makes It Better

Synchronous code:

readWrite() { try { var c = read(); write(c); } catch (e) { handleError(e); } finally { close(); } } readWrite() { Future f = read(); return f.then((c) => write(c)) .catchError(handleError) .whenComplete(close); } // Control flow must be dealt with in library. // Chaining of futures is tedious.

slide-39
SLIDE 39

Solving the Callback Morass

  • Hired Erik Meijer to help with asynchronous design
  • Introduced special async methods
  • Libraries were already based in futures and streams
  • Inspired by C#
slide-40
SLIDE 40

Async/await Feature

Synchronous code:

readWrite() { try { var c = read(); write(c); } catch (e) { handleError(e); } finally { close(); } } readWrite() async { try { var c = await read(); await write(c); } catch (e) { handleError(e); } finally { await close(); } }

// await suspends the activation in a non-blocking way!

slide-41
SLIDE 41

Reflections on async/await

Pros

  • Restores “normal” control flow

○ Including exception and finally code

  • Incremental migration of code

Cons

  • Dual mode makes reasoning complicated
  • Stack traces disappear
  • Debugging is not intuitive
slide-42
SLIDE 42

Simple and Consistent Language ✓

  • Dart is a simple and consistent programming language

○ Unsurprising and familiar ○ Concise and useful

  • Now we just have to fix the framework...
slide-43
SLIDE 43

The Butterfly Effect with Flutter

slide-44
SLIDE 44

Making Mobile Development Easier and Cheaper

Flutter is a new project to help developers build high- performance, high-fidelity, mobile apps for iOS and Android from a single codebase in Dart

http://flutter.io

Flutter is open source

slide-45
SLIDE 45

Flutter Architectural Overview

Skia Dart VM Engine Mojo Services Animation Painting Rendering Widgets Material Gestures Shell (C++) Framework (Dart)

slide-46
SLIDE 46

Flutter is a Functional-Reactive Framework

class CounterState extends State { int counter = 0; void increment() { setState(() { counter++; }); } Widget build(BuildContext context) => new Scaffold( toolBar: new ToolBar( center: new Text("Flutter Demo")), body: new Material( child: new Center( child: new Text("Button tapped $counter times.")))); }

slide-47
SLIDE 47

"Dart has been a great fit for mobile. It's familiar, flexible, and fast. We're building our entire framework, widgets, and developer tools in Dart." Seth Ladd, Product Manager for Flutter at Google

slide-48
SLIDE 48

Simple Unified Application Framework

  • One language to rule the entire stack

○ Same semantics ○ Same great tooling experience (debugging, etc.)

  • Contrast to Dart + Angular + HTML
slide-49
SLIDE 49

Dart in 2016

slide-50
SLIDE 50

Google Fiber

slide-51
SLIDE 51

Google AdWords

slide-52
SLIDE 52

Dart for Web Apps

  • Google is shipping large, mission critical web apps built in Dart
  • Every day lots of developers are spending all their time writing in Dart

Why?

“Our engineers were highly productive as their projects scaled to many engineers. Dart’s support for strong typing, its object-oriented nature, and its excellent IDE integration really worked for us.” Joshy Joseph, Distinguished Engineer, Google AdWords

slide-53
SLIDE 53

Dart Runs Everywhere...

Browsers: Runs translated to JavaScript Mobile: Runs on optimized Dart VM (Flutter) IoT: Runs on embedded MCUs Servers: Runs on optimized Dart VM

slide-54
SLIDE 54

Work in progress... Early preview SDK supporting Coretex M4/M7 microcontrollers is available. http://dartino.org/

Small, efficient runtime for Dart:

  • 32-bit microcontrollers (ARM, MIPS)
  • 128 KB of RAM
  • 512 KB of Flash

Dartino: Dart for Embedded Devices

slide-55
SLIDE 55

Conclusions

slide-56
SLIDE 56

Summary

  • We designed a pragmatic OO programming language

○ It is readable and well-defined ○ It increases programmer productivity ○ It has nice systems properties

  • Projects using Dart have claimed better productivity
  • Several million lines of Dart code has been written at Google

Both creators of Dart hereby claim that Dart makes you a better programmer

slide-57
SLIDE 57
slide-58
SLIDE 58

Questions