TypeScript vs. CoffeeScript vs. ES6

Post on 15-Jul-2015

10.168 views 6 download

Transcript of TypeScript vs. CoffeeScript vs. ES6

by Neil Green (neilonsoftware.com)

(Please hold questions until the end)

Rules of this talk:

• Please be nice.

• Please be respectful.

• Please don’t throw things.

• Please take no personal offense to anything I may say, imply, or infer regarding programming languages.

• Please forgive me if I am not 100% accurate or complete in my slides (I err on the side of conciseness.)

To keep everyone calm and relaxed before and/or after stressful slides, periodically photos of cute

kittens will be shown.

Understanding My Perspective

Why only a crazy personwould attempt this talk

• There is no way to do this topic justice in an hour.

• Developers are fiercely opinionated about languages, especially JavaScript.

• This talk can also be cast as “Static vs Dynamic Typing” or “Java vs [insert language]”

• Regretably, this topic now also invites AngularJS + TypeScript vs ReactJS + Flow debates.

• It is incredibly difficult to not come across as biased on this topic - but I will try!

Things I am not

• A language designer – I am a language user.

• An academic – I work to get paid.

• Flawless in my memory and understanding in TypeScript, CoffeeScript, and ES6 (but I did a lot of preparation for this talk).

• Totally familiar with your programming language such that I can answer any comparison/contrast questions.

• Able to teach you 3 programming languages in 60 minutes, much less demonstrate their full capabilities.

My Perspective on

Programming Languages

• Have written JavaScript since 1997

• Have written CoffeeScript exclusively since 2011

• Have never written TypeScript professionally

• Coded Java on and off since 1999 (~5 years total)

• Have gotten to use many of the new ES6s features

• Dabbled in Ruby and Scala over the years

• Learning Haskell and ClojureScript currently

• I pick whatever Language the situation requires

For simplicity I am going to refer to JavaScript language versions prior to ES6 as simply

“JavaScript”

JavaScriptCoffeeScript Transpiles to TypeScriptTranspiles to

ES6

Extended by

Extended by

If Jeremy decides the feature is important,

can be extended by

Babel

Transpiles to

Overview of Languages and Transpilers

Crash Course in the Languages

Crash Course in ES6

ES6 History

• JavaScript developed by Brendan Eich in 1995

• Netscape released in 1996 supporting JavaScript

• Microsoft created JScript dialect in 1996

• Ecma International standardized ECMA-262 to settle disputes between browser vendors

• ECMA-262 standardized ECMAScript

• ECMA-262 had 5 editions, with a 6th scheduled for mid 2015 (ES6)

JavaScript Keywords

• break• case• catch• continue• debugger• default• delete• else

• finally• for• function• if• in• instanceof• new• return

• switch• this• throw• try• typeof• var• while• with

ES6 Keywords

• break• case• class• catch• const• continue• debugger• default• delete• do• else• export

• extends• finally• for• function• if• import• in• instanceof• let• new• return• super

• switch• this• throw• try• typeof• var• void• while• with• yield

E56 Features

• Arrow Functions

• Classes

• Enhanced object literals

• Template strings

• Destructuring

• default + rest + spread

• let + const

• iterators + for..of

• Generators

• Unicode

• Modules

• Module loaders

• map + set + weakmap + weakset

• Proxies

• Symbols

• Subclassable built-ins

• Promises

• math + number + string + array + object APIs

• Binary and Octal literals

• Reflect API

• Tail calls

https://github.com/lukehoban/es6features

Because lists of keywords and features are a poor way to compare languages, we are going to look at the same simple class implementation written in

each language.

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

user = new User(123, ‘John’, ‘Smith’)

function User(id, firstName, lastName) {this.id = id;this.firstName = firstName;this.lastName = lastName;

}

User.prototype = {getId: function() {

return this.id;},getFirstName: function() {

return this.firstName;},getLastName: function() {

return this.lastName;},setFirstName: function(firstName) {

this.firstName = firstName;},setLastName: function(lastName) {

this.lastName = lastName;}

};

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

JavaScript

class User {constructor(id, firstName, lastName) {

this.id = id;this.firstName = firstName;this.lastName = lastName;

}getId() {

return this.id;}getFirstName() {

return this.firstName;}setFirstName(firstName) {

this.firstName = firstName;}getLastName() {

return this.lastName;}setLastName(lastName) {

this.lastName = lastName;}

}User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

ES6

ES6 Bottom Line

• ES6 has various features supported in various browsers

• If you want to use ES6 features in all browsers now use Babel

Crash Course in TypeScript

TypeScript History

• Typescript was first made public in October 2012

• Anders Hejlsberg, lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript

• Originated from the perceived short-comings of JavaScript for the development of large-scale applications both at Microsoft and among their external customers.

• As TypeScript is a superset of JavaScript, any existing JavaScript programs are also valid TypeScript programs.

TypeScript Features

• Type annotations

• Public/Private

• Compile-time type checking

• Type inference

• Interfaces

• Enums

• Mixin

• Generics

• Optional properties

• Tuple types

class User {private id: number;private firstName: string;private lastName: string;

constructor(id: number, firstName: string, lastName: string) {this.id = id;this.firstName = firstName;this.lastName = lastName;

}getId() {

return this.id;}getFirstName(): string {

return this.firstName;}setFirstName(firstName: string) {

this.firstName = firstName;}getLastName(): string {

return this.lastName;}setLastName(lastName: string) : void {

this.lastName = lastName;}

}

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

TypeScript

TypeScript Bottom Line

• If you are used to Java and C#, you should feel at home in TypeScript.

• If you hate looking at Type information, you should not use TypeScript.

• If believe in compile-time type checking, use TypeScript.

• If you have problems with Type-related bugs in JavaScript, use TypeScript.

• If you are not going to make use of Type annotations, just use ES6.

Crash Course in CoffeeScript

CoffeeScript History

• Created by Jeremy Ashkenas, who also created Backbone.js and Underscore.js.

• First version released December 24th, 2010

• Goal was to create, “A language that takes out the frustrating and overly verbose bits of JS, and provides a safer, briefer way to stick to the good parts.”

CoffeeScript Features

• Everything is an Expression (mostly)

• Whitespace to delimit blocks of code

• Optional parens

• Optional postfix

• Classes, Inheritance, and Super

• Operators and Aliases

• Iterations

• Splatting

• Comprehensions

• Arrow Functions

• Destructuring Assignments

• Chained Comparisons

• Block Strings

• Prototype Shorthand

• Parameters to Properties Binding

• Class/Static Functions

• Existential Operator

• Bound and Generator Functions

• Block Regular Expressions

• Default parameters

class Userconstructor (@id, @firstName, @lastName) ->

getId: ->@id

getFirstName: ->@firstName

setFirstName: (@firstName) ->

getLastName: ->@lastName

setLastName: (@lastName) ->

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

CoffeeScript

CoffeeScript Bottom Line

• If you want as little “noise” as possible when reading you programs, use CoffeeScript.

• If you like Haskell and Ruby, you should like CoffeeScript.

• You are at Jeremy’s whim when it comes to ES6 language features (though you can use ES6 APIs)

Recap of Code Samples

function User(id, firstName, lastName) {this.id = id;this.firstName = firstName;this.lastName = lastName;

}

User.prototype = {getId: function() {

return this.id;},getFirstName: function() {

return this.firstName;},getLastName: function() {

return this.lastName;},setFirstName: function(firstName) {

this.firstName = firstName;},setLastName: function(lastName) {

this.lastName = lastName;}

};

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

JavaScript

class User {constructor(id, firstName, lastName) {

this.id = idthis.firstName = firstNamethis.lastName = lastName

}getId() {

return this.id}getFirstName() {

return this.firstName}setFirstName(firstName) {

this.firstName = firstName}getLastName() {

return this.lastName}setLastName(lastName) {

this.lastName = lastName}

}User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

ES6

class User {private id: number;private firstName: string;private lastName: string;

constructor(id: number, firstName: string, lastName: string) {this.id = id;this.firstName = firstName;this.lastName = lastName;

}getId() {

return this.id;}getFirstName() : string {

return this.firstName;}setFirstName(firstName: string) {

this.firstName = firstName;}getLastName() : string {

return this.lastName;}setLastName(lastName: string) : void {

this.lastName = lastName;}

}

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

TypeScript

class Userconstructor (@id, @firstName, @lastName) ->

getId: ->@id

getFirstName: ->@firstName

setFirstName: (@firstName) ->

getLastName: ->@lastName

setLastName: (@lastName) ->

User

- id:int- firstName:String- lastName:String

+ getId():int+ getFirstName():String+ setFirstName(String):void+ getLastName():String+ setLastName(String):void

CoffeeScript

Key Selection Criteria

Language Features

TypeScript has many features for you to use

class Sphere implements Thing {public radius2: number;

constructor(public center: Vector, radius: number, public surface: Surface) {this.radius2 = radius * radius;

}normal(pos: Vector): Vector { return Vector.norm(Vector.minus(pos, this.center)); }intersect(ray: Ray) {

var eo = Vector.minus(this.center, ray.start);var v = Vector.dot(eo, ray.dir);var dist = 0;if (v >= 0) {

var disc = this.radius2 - (Vector.dot(eo, eo) - v * v);if (disc >= 0) {

dist = v - Math.sqrt(disc);}

}if (dist === 0) {

return null;} else {

return { thing: this, ray: ray, dist: dist };}

}}

CoffeeScript has many features for you to use

launch() if ignition is on

volume = 10 if band isnt SpinalTap

letTheWildRumpusBegin() unless answer is no

if car.speed < limit then accelerate()

winner = yes if pick in [47, 92, 13]

print inspect "My name is #{@name}“

solipsism = true if mind? and not world?

speed = 0speed ?= 15

footprints = yeti ? "bear“

grade = (student) ->if student.excellentWork

"A+"else if student.okayStuff

if student.triedHard then "B" else "B-"else

"C"

eldest = if 24 > 21 then "Liz" else "Ike"

ES6 has many features for you to use

function* anotherGenerator(i) {yield i + 1;yield i + 2;yield i + 3;

}

function* generator(i){yield i;yield* anotherGenerator(i);yield i + 10;

}

var gen = generator(10);

console.log(gen.next().value); // 10console.log(gen.next().value); // 11console.log(gen.next().value); // 12console.log(gen.next().value); // 13console.log(gen.next().value); // 20

function letTest() {let x = 31;if (true) {

let x = 71; // different variableconsole.log(x); // 71

}console.log(x); // 31

}

Understandability

I am a happy coder

English

Ich bin ein glücklicher Coder

German

Ik ben een gelukkig coder

Dutch

Je suis un codeur heureux

Fench

Soy un codificador feliz

Spanish

나는행복코더입니다

Korean

我是一个快乐编码器

Chinese

The more familiar you are with the language, the easier it is to understand

I am a happy coder

Happy, a coder I am

Me happy with be coder

i ‘m hppy cdr

I I am am a a happy happy coder coder

John Smith writes Java code, and as a result feels the emotion of happiness.

I am, you see, someone who is in fact, indubitably, and unerringly, beyond a shadow of a doubt someone who, in

truth, does truly greatly enjoy – nay, is - indeed happy being a coder.

Once, when I was a boy, I dreamed of a life where I could be happy. Happy, that is, in not just anything. No, it would have to be

something that I enjoyed – something perhaps I could not live without. “What

could this thing be?” I asked myself. Coding. It was coding that filled my head

with overwhelming joy. Joy before which I had never known. To code is to know

happiness. To be happy is to code. I am a coder, and therefore, I am happy.

The more well written the language, the easier it is to understand

Bug Prevention

TypeScript compile-time type checking is thought to lead to fewer bugs, especially in larger code bases

class FizzBuzz {static fizz = 'Fizz';static buzz = 'Buzz';

generate(input: number): string {var output = '';

if (input % 3 === 0) {output += FizzBuzz.fizz;

}

if (input % 5 === 0) {output += FizzBuzz.buzz;

}

return output === '' ? input.toString() : output;}

}

normalNumbersReturnOriginalNumber() {this.areIdentical('1', target.generate(1));this.areIdentical('2', target.generate(2));this.areIdentical('4', target.generate(4));

}

numberDivisibleByThreeShouldReturnFizz() {this.areIdentical(FizzBuzz.fizz, target.generate(3));this.areIdentical(FizzBuzz.fizz, target.generate(6));this.areIdentical(FizzBuzz.fizz, target.generate(9));

}

numbersDivisibleByFiveShouldReturnBuzz() {this.areIdentical(FizzBuzz.buzz, target.generate(5));this.areIdentical(FizzBuzz.buzz, target.generate(10));this.areIdentical(FizzBuzz.buzz, target.generate(20));

}

numbersDivisibleByThreeAndFiveShouldReturnFizzBuzz() {this.areIdentical(FizzBuzz.fizz + FizzBuzz.buzz, target.generate(15));this.areIdentical(FizzBuzz.fizz + FizzBuzz.buzz, target.generate(30));this.areIdentical(FizzBuzz.fizz + FizzBuzz.buzz, target.generate(45));

}

CoffeeScript, because of its brevity, is thought to reveal bugs more easily.

class FizzBuzz@fizz = 'Fizz'@buzz = 'Buzz'

generate: (input) ->output = ''

output += @fizz if input % 3 is 0output += @buzz if input % 5 is 0

if output is '' then input.toString() else output

normalNumbersReturnOriginalNumber = ->@areIdentical '1', target.generate 1@areIdentical '2', target.generate 2@areIdentical '4', target.generate 4

numberDivisibleByThreeShouldReturnFizz = ->@areIdentical FizzBuzz.fizz, target.generate 3@areIdentical FizzBuzz.fizz, target.generate 6@areIdentical FizzBuzz.fizz, target.generate 9

numbersDivisibleByFiveShouldReturnBuzz = ->@areIdentical FizzBuzz.buzz, target.generate 5@areIdentical FizzBuzz.buzz, target.generate 10@areIdentical FizzBuzz.buzz, target.generate 20

numbersDivisibleByThreeAndFiveShouldReturnFizzBuzz = ->@areIdentical FizzBuzz.fizz + FizzBuzz.buzz, target.generate 15@areIdentical FizzBuzz.fizz + FizzBuzz.buzz, target.generate 30@areIdentical FizzBuzz.fizz + FizzBuzz.buzz, target.generate 45

Making a Decision

Picking Something based on Features

My Most Used Features oniPhone First Gen

1. Apps

2. Taking Pictures

3. Checking Email

4. Browsing Internet

5. Text Messaging

6. Making phone calls

New Features iniPhone 6

• More Bigger

• More Thinner

• More Better Camera

• More Faster Processor

• More Sensors

• More Better Keyboard

• Apple Pay

My Most Used Features oniPhone 6

1. Apps

2. Taking Pictures

3. Checking Email

4. Browsing Internet

5. Text Messaging

6. Making phone calls

Giving Context to Understandability

“Dad, there’s a weird van in front of my house”

“Officer, my daughter has a weird van in front of her house. She lives on 7890 Chesapeake Lane.”

“…Van is a 1996-year Plymouth Green Minivan with gray lower trim parked in front of 7890 Chesapeake

Lane.”

Properly PrioritizingBug Prevention

$1 Billion

$100 Million

x 1.48 = F-35

x 13 = Carrier

x 1 = Your Ass

My Advice

Use TypeScript if…

• You love Java/C# and hate JavaScript

• You use Microsoft Visual Studio, or generally like IDE refactoring support.

• You find yourself doing a lot of instanceof/type of guards in your JavaScript to prevent bugs

• You know that a lot of your bugs could be caught by Type checking

• You have a large, divergently skilled team who don’t have the opportunity to mesh/gel together on code conventions.

Use CoffeeScript if…

• You love Haskell/Ruby and hate JavaScript

• You believe that brevity leads to fewer bugs than verbosity.

• You hate looking at “noise” in your code

• You are going to take advantage of CoffeeScriptlanguage features.

• You trust that Jeremy will keep his language up-to-date with ES6 features

Use ES6 if…

• You love JavaScript

• You don’t want to use the Type checking in TypeScript.

• You don’t want to ever miss out on features because they are not added to CoffeeScript.

• You want to be a part of the largest development community, rather than a subset.

Questions?(remember to be nice)

Thank you!@neilfeyn