Who am I?
Who am I?
• A software engineer and entrepreneur
• On the tablet team at Kobo
• Find me blogging at FaisalAbid.com
• Tweeting @FaisalAbid
A few things I’ve worked on
101
History of Javascript
The bad parts
• Global Variables
• operator overloading in a dynamic typed language
• semicolon optional
• typeof inconsistency. null = object?!
• ==,===, !=
• false, null, undefined, NaN
Javascript also has good parts
Being the good guy is hard in Javascript
Would you like a cup of coffee?
Not the first of its kind.
GWT Output
function A0(){this.B0();return this.js[0];}
function C0(){if(this.js.length > 1)
{this.js = [this.js.join('')];this.length = this.js[0].length;}}
function D0(E0){this.js = [E0];this.length = E0.length;}
function Ez(F0,a1){return F0.yx(yZ(a1));}
function yB(b1){c1(b1);return b1;}
function c1(d1){d1.e1('');}
function zB(){}
_ = zB.prototype = new f();_.yx = w0;_.vB = A0;_.B0 = C0;_.e1 =
D0;
_.i = 'java.lang.StringBuffer';_.j = 75;
function f1(){f1 = a;g1 = new iX();h1 = new iX();return window;}
CoffeeScript Output

 var bitlist, kids, singers, song;

 song = ["do", "re", "mi", "fa", "so"];

 singers = {
    Jagger: "Rock",
    Elvis: "Roll"
 };
The golden rule of CoffeeScript is: "It's just JavaScript". The
code compiles one-to-one into the equivalent JS, and there is no
interpretation at runtime. [....].

The compiled output is readable and pretty-printed, passes
through JavaScript Lint without warnings, will work in every
JavaScript runtime, and tends to run as fast or faster than the
equivalent handwritten JavaScript.




                               “It's just javascript!”
Javascript


    $("#ComboBox").change(function() {
             callThisMessage(function(){
                  $.callOtherMethods(function(){
                    $("#ComoboBox").move()
                  });
             })
    });
CoffeeScript


    $("#ComboBox").change ->
       callThisMessage ->
          $.callOtherMethods ->
               $("#ComoboBox").move()
Whats good about CoffeeScript
• Private by default
• no more variable leaking
• == vs ===, Fuzzy matching is gone. "is" is the ===
• Passes JSLint with flying colours.
Getting Started With CoffeeScript
• CoffeeScript.org is the best resource available.
• Install Node.js and install the CoffeeScript compiler
• coffee -c filename.coffee generates the javascript!
What well go over
• Syntax
• Whitespace Matters
• Object Syntax
• Everything is an expression
• Comprehension
• Classes, Inheritance
• Bound Functions
• Conditionals
Syntax



    message = "hello world"


    sayHello = ->
    	    alert message


    sayhelloWithName = (name)->
    	    alert message+' '+name
Whitespace Matters


    message = ->
    	   someOtherMessage ->
    	   	   console.log "hello"


    someOtherMessage = (callback)->
    	   callback()


    message()
Object Syntax

    conference =
    	   title: 'FITC Screens 2012'
    	   description: 'The coolest conference ever!'
    	   details:
    	   	   homepage: 'http://www.fitc.ca'
    	   	   rss:'http://somerssurl.com'
    	   sayName: ->
    	   	   alert @title


    conference.sayName()
Everything is an expression
    x = 4
    lessmessage = "less then four"
    greatermessage = "greater than or equal to four"
    message = if x < 4   then less message else greatermessage
    alert message


    numberToString = (value) ->
    	   switch value
    	   	   when value is 1 then "one"
    	   	   when value is 2 then "two"
    	   	   when value is 3 then "three"
    	   	   when value is 4 then "four"


    number = numberToString(3)
    alert number
Comprehension


   items = [1,2,3,4,5,6,7,8,9,10]


   doubleitems = (double num for num in items when num <5)
   tripleitems = (triple num for num in items when num >= 5)


   double = (x)->
   	   x*2


   triple = (x)->
   	   x*3
Classes & Inheritence
  class Animal                             var Animal, Zebra,
                                            __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },

  	   constructor: (@name) ->               __hasProp = {}.hasOwnProperty,
                                            __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
                                           function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__
                                           = parent.prototype; return child; };

                                           Animal = (function() {

  	   sayName: ->                           function Animal(name) {
                                              this.name = name;
                                            }
  	   	   @name                             Animal.prototype.sayName = function() {
                                              return this.name;
                                            };

                                            return Animal;

  class Zebra extends Animal               })();

                                           Zebra = (function(_super) {
  	   constructor: (@name,@hasStripes)->    __extends(Zebra, _super);

  	   	   super(@name)                      function Zebra(name, hasStripes) {
                                             this.name = name;
                                             this.hasStripes = hasStripes;
                                             this.checkName = __bind(this.checkName, this);

                                                Zebra.__super__.constructor.call(this, this.name);
  	   hasStripes: ->                        }

                                            Zebra.prototype.hasStripes = function() {

  	   	   @hasStripes                       };
                                              return this.hasStripes;

                                            Zebra.prototype.checkName = function() {
                                              return httpgetcall(this.name, function() {
                                                return console.log(this.hasStripes);
                                              });
                                            };
  	   isCool: =>                            return Zebra;

  	   	   httpgetcall @name,->             })(Animal);



  	   	   	   console.log @hasStripes
Bound Functions


  [...]
  class Zebra extends Animal
  	   constructor: (@name,@hasStripes)->
  	   	   super(@name)


  	   hasStripes: ->
  	   	   @hasStripes


  	   isCool: =>
  	   	   httpgetcall @name,->
  	   	   	   console.log @hasStripes
Conditionals



  x = 10
  number = 5
  shouldDouble = true




  doubleNumber = (value)->
  	   value*2


  number = doubleNumber x if shouldDouble
CoffeeScript is Javascripts new Hope
Thank you.
follow me @faisalabid

FITC CoffeeScript 101

  • 1.
  • 2.
    Who am I? •A software engineer and entrepreneur • On the tablet team at Kobo • Find me blogging at FaisalAbid.com • Tweeting @FaisalAbid
  • 3.
    A few thingsI’ve worked on
  • 4.
  • 5.
  • 6.
    The bad parts •Global Variables • operator overloading in a dynamic typed language • semicolon optional • typeof inconsistency. null = object?! • ==,===, != • false, null, undefined, NaN
  • 7.
  • 8.
    Being the goodguy is hard in Javascript
  • 9.
    Would you likea cup of coffee?
  • 10.
    Not the firstof its kind.
  • 11.
    GWT Output function A0(){this.B0();returnthis.js[0];} function C0(){if(this.js.length > 1) {this.js = [this.js.join('')];this.length = this.js[0].length;}} function D0(E0){this.js = [E0];this.length = E0.length;} function Ez(F0,a1){return F0.yx(yZ(a1));} function yB(b1){c1(b1);return b1;} function c1(d1){d1.e1('');} function zB(){} _ = zB.prototype = new f();_.yx = w0;_.vB = A0;_.B0 = C0;_.e1 = D0; _.i = 'java.lang.StringBuffer';_.j = 75; function f1(){f1 = a;g1 = new iX();h1 = new iX();return window;}
  • 13.
    CoffeeScript Output varbitlist, kids, singers, song; song = ["do", "re", "mi", "fa", "so"]; singers = { Jagger: "Rock", Elvis: "Roll" };
  • 15.
    The golden ruleof CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. [....]. The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript. “It's just javascript!”
  • 16.
    Javascript $("#ComboBox").change(function() { callThisMessage(function(){ $.callOtherMethods(function(){ $("#ComoboBox").move() }); }) });
  • 17.
    CoffeeScript $("#ComboBox").change -> callThisMessage -> $.callOtherMethods -> $("#ComoboBox").move()
  • 18.
    Whats good aboutCoffeeScript • Private by default • no more variable leaking • == vs ===, Fuzzy matching is gone. "is" is the === • Passes JSLint with flying colours.
  • 19.
    Getting Started WithCoffeeScript • CoffeeScript.org is the best resource available. • Install Node.js and install the CoffeeScript compiler • coffee -c filename.coffee generates the javascript!
  • 20.
    What well goover • Syntax • Whitespace Matters • Object Syntax • Everything is an expression • Comprehension • Classes, Inheritance • Bound Functions • Conditionals
  • 21.
    Syntax message = "hello world" sayHello = -> alert message sayhelloWithName = (name)-> alert message+' '+name
  • 22.
    Whitespace Matters message = -> someOtherMessage -> console.log "hello" someOtherMessage = (callback)-> callback() message()
  • 23.
    Object Syntax conference = title: 'FITC Screens 2012' description: 'The coolest conference ever!' details: homepage: 'http://www.fitc.ca' rss:'http://somerssurl.com' sayName: -> alert @title conference.sayName()
  • 24.
    Everything is anexpression x = 4 lessmessage = "less then four" greatermessage = "greater than or equal to four" message = if x < 4 then less message else greatermessage alert message numberToString = (value) -> switch value when value is 1 then "one" when value is 2 then "two" when value is 3 then "three" when value is 4 then "four" number = numberToString(3) alert number
  • 25.
    Comprehension items = [1,2,3,4,5,6,7,8,9,10] doubleitems = (double num for num in items when num <5) tripleitems = (triple num for num in items when num >= 5) double = (x)-> x*2 triple = (x)-> x*3
  • 26.
    Classes & Inheritence class Animal var Animal, Zebra, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, constructor: (@name) -> __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Animal = (function() { sayName: -> function Animal(name) { this.name = name; } @name Animal.prototype.sayName = function() { return this.name; }; return Animal; class Zebra extends Animal })(); Zebra = (function(_super) { constructor: (@name,@hasStripes)-> __extends(Zebra, _super); super(@name) function Zebra(name, hasStripes) { this.name = name; this.hasStripes = hasStripes; this.checkName = __bind(this.checkName, this); Zebra.__super__.constructor.call(this, this.name); hasStripes: -> } Zebra.prototype.hasStripes = function() { @hasStripes }; return this.hasStripes; Zebra.prototype.checkName = function() { return httpgetcall(this.name, function() { return console.log(this.hasStripes); }); }; isCool: => return Zebra; httpgetcall @name,-> })(Animal); console.log @hasStripes
  • 27.
    Bound Functions [...] class Zebra extends Animal constructor: (@name,@hasStripes)-> super(@name) hasStripes: -> @hasStripes isCool: => httpgetcall @name,-> console.log @hasStripes
  • 28.
    Conditionals x= 10 number = 5 shouldDouble = true doubleNumber = (value)-> value*2 number = doubleNumber x if shouldDouble
  • 29.
  • 30.

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 - Some projects ive worked on\n- Describe the projects\n- Talk about the ARC\n- Personal projects use CoffeeScript\n
  • #5 - CoffeeScript 101\n- Popular language taking over the landscape\n- Dropbox recently changed over most of their code to CoffeeScript.\n- If DB can change with there immense scale, then there has to be something to it.\n- before we see what that something is, lets take a step back and go back in time to 2005\n
  • #6 - JS is 17 years old. \n- Originally Mocha, LiveScript. \n- Brendan Eich created this in 3 days. But it still lives today.\n- lots of holes, but behind those scars lies a beautiful language.\n\n- Before Javascript was for garbage mouse followers, flashy things, and a lot of people werent really js developers\n- in 2005 google introduced Google maps which became a revolution in JS\n- Suddenly javascript went from being a bullshit sprinkled language to a full blown powerhouse in the eyes of developers\n- Frameworks cropped up, JQUery came out. and in 2012, HTML5 and JS have taken over.\n\n- but theres a problem. \n- JS is very clunky, and very very hard to write Google Map type web apps without tons of time invested.\n- Time = Money \n- Gets very messy and unreadable after a while.\n\n- Ajax, and other stuff\n\n- in short JS has its bad parts\n\n
  • #7 \n
  • #8 - Great book by Douglas Crockford.\n- Pioneer in good JS development\n- JSLint/ JSOn Libraries\n- following these &amp;#x201C;good parts&amp;#x201D; is not always easy.\n
  • #9 Not always easy to adopt to changes. \nTons of JS libraries make it hard. Some might be bad, not your fault!\n\n
  • #10 On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: &quot;initial commit of the mystery language.&quot;[9] The compiler was written in Ruby. \nOn December 24, he made the first tagged and documented release, 0.1.0. On February 21, 2010, he committed version 0.5, which replaced the Ruby compiler with one written in pure CoffeeScript. By that time the project had attracted several other contributors on GitHub, and was receiving over 300 page hits per day.\nOn December 24, 2010, Ashkenas announced the release of stable 1.0.0 to Hacker News, the site where the project was announced for the first time.[10][11]\n\nTalk abou how you came to the language. what were you doing?\n\n
  • #11 - in 2006 google announced GWT\n- holy crist mind blown, java to javascript. no need to learn that javascript crap anymore.\n- Checked it out. Had to go through tons of hoops. \n- Install extension ,etc etc. JS it outputs is cryptic.\n
  • #12 - first impression. wow look at how optimized my js i, hell ya hell ya\n- I love this tool.when people see im doing this ill be so cool\n\n
  • #13 - Makes your JS hard to debug. Obfuscation is fine in production, but in development its stupid.\n- on the contrary, take a look at what CoffeeScript outputs\n
  • #14 - Boring old javascript. nothing special.\n
  • #15 \n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 A new hope of Javascript.\nCleaner more efficent.\nWith Node.js its even more awesome.\nPlease check it out.\n\n\n
  • #31 \n