SlideShare a Scribd company logo
How
CoffeeScript &
Jasmine made
me a better JS
developer
I’m Matt Gauger
@mathiasx
I work at
Bendyworks
My dilemma:
Impostor Syndrome
I was a
JavaScript
impostor
So, what does this
 have to do with
  CoffeeScript?
My thesis:
How CoffeeScript & Jasmine
made me a better JS
developer
(and how it can help you,
too)
CoffeeScript
History
Today
CoffeeScript has
 Good Parts?
CoffeeScript:   JavaScript:
CoffeeScript:   JavaScript:




     ^ Major Win ^
Criticisms of
CoffeeScript
That isn't to say that
CoffeeScript
eliminates all bugs.
Some examples:
Coercing the wrong type

    if (1 == '1') {
       console.log("it's true!");
}
CoffeeScript:
if 1 is ‘1’
  console.log ‘never true!’


Compiles to:
if (1 === ‘1’) {
  console.log("never true!");
}
Scope
CoffeeScript scope is
essentially the same
      as in JS
1. 	 Every function creates a scope, and
the only way to create a scope is to
define a function.

2. A variable lives in the outermost
scope in which an assignment has been
made to that variable.

3.	 Outside of its scope, a variable is
invisible.
The neat thing is that
CoffeeScript’s compiler
places the vars for
each scope at the top
of that scope
For example, this
     compiled CoffeeScript:
  var singCountdown;
singCountdown = function(count) {
  var singBottleCount, singDecrement;
  singBottleCount = function(specifyLocation) {
    var bottleStr, locationStr;
    // ...
  }
  // ...
}
Define a variable at
a specific scope by
giving it a sensible
    initial value
a ?= b
is one way to give a default
Lastly, wrapping up
     your code.
Compiled CoffeeScript Example:



(function() {
  console.log “hello world!”;
}).call(this);
Simpler Looping
Jeremy Ashkenas’s Example
Loop over every item in a list, in CoffeeScript:
  for item in list

      process item



Intention gets obscured by managing the loop, in JS:
  for (var i = 0, l = list.length; i < l; i++) {

      var item = list[i];

      process(item);

  }
CoffeeScript will help you:
•Write OO, Prototype-based code
•Avoid bugs in comparisons
 •Stop using ==, only use ===
•Manage scope and avoid state through
  scope creep

•Reduce off-by-one errors in looping, and
  generally write better loops than you were
  writing before
Jasmine
(My) History
(with Jasmine)
Why Jasmine?
The example on the site:
describe("Jasmine", function() {
  it("makes testing awesome!", function() {
    expect(yourCode).toBeLotsBetter();
  });
});
This example sucks!
describe ('addition', function () {  
    it('adds two numbers', function () {  
        expect(1 + 2).toEqual(3);  
    });  
});
How should we test JS?
Functions should not
depend on the DOM
Our logic needs to
be in separate pieces
Easier to test = better code
Follow TDD/BDD:
red, green, refactor
Jasmine is designed
 to be standalone
Some really cool
features of Jasmine:
Matchers:
•.toBe()
•.toBeNull()
•.toBeTruthy()
•.toBeDefined()
•.toBeUndefined()
•etc.
Setup and Teardown:

•beforeEach()
•afterEach()
Spies: built-in
mocking & stubbing
But what about
legacy codebases?
Start simple.
Fix one bug.
(red, green, refactor)
Rewrite the affected
code in CoffeeScript.
Start grouping in
 files / modules.
Keep improving the
    codebase.
Lessons Learned:

•These tools can help you learn JS better.
•Legacy codebases can slowly grow
  better through using CoffeeScript &
  Jasmine.

•Taking advantage of these is up to you!
Thanks!
Resources to learn more:

•http://jashkenas.github.com/coffee-script/
•http://pivotal.github.com/jasmine/
•http://pragprog.com/book/tbcoffee/coffeescript
•http://js2coffee.org/

More Related Content

PDF
Programming ten commandments
KEY
PPTX
CoffeeScript the Awesome
PPTX
Introduce cucumber
PDF
What is JavaScript?
PPTX
Behavior lab
PDF
JSConf US 2014: Building Isomorphic Apps
Programming ten commandments
CoffeeScript the Awesome
Introduce cucumber
What is JavaScript?
Behavior lab
JSConf US 2014: Building Isomorphic Apps

Viewers also liked (8)

KEY
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
KEY
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
PDF
Oh shit! the cops are here!
PPTX
Software for your mind
PPTX
Live+Social - Being Remarkable - the key to social business success
KEY
Matt Gauger - Git & Github web414 December 2010
PDF
Making the impossible possible
PDF
Paradigm of agile project management
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
Oh shit! the cops are here!
Software for your mind
Live+Social - Being Remarkable - the key to social business success
Matt Gauger - Git & Github web414 December 2010
Making the impossible possible
Paradigm of agile project management
Ad

Similar to CoffeeScript & Jasmine - MadJS February 2012 (20)

PPTX
Coffee scriptisforclosers nonotes
PPTX
Object Oriented Programming In JavaScript
PDF
Real life-coffeescript
PDF
Testing Backbone applications with Jasmine
PPTX
Loops in java script
PDF
CoffeeScript-Ruby-Tuesday
PPT
Testing Javascript with Jasmine
PDF
TDD super mondays-june-2014
PDF
Coffeescript slides
PPTX
Coffee script
PDF
Cucumber
PDF
Introduction to CoffeeScript
PPT
JavaScript 2.0 in Dreamweaver CS4
PPT
Javascript and Jquery Best practices
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
PDF
Leveling Up at JavaScript
PDF
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
PDF
Intro to javascript (6:19)
Coffee scriptisforclosers nonotes
Object Oriented Programming In JavaScript
Real life-coffeescript
Testing Backbone applications with Jasmine
Loops in java script
CoffeeScript-Ruby-Tuesday
Testing Javascript with Jasmine
TDD super mondays-june-2014
Coffeescript slides
Coffee script
Cucumber
Introduction to CoffeeScript
JavaScript 2.0 in Dreamweaver CS4
Javascript and Jquery Best practices
We Are All Testers Now: The Testing Pyramid and Front-End Development
Leveling Up at JavaScript
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
Intro to javascript (6:19)
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm

CoffeeScript & Jasmine - MadJS February 2012

Editor's Notes

  • #2: \n
  • #3: First, an introduction:\n* I&amp;#x2019;m Matt Gauger\n
  • #4: \n
  • #5: * I work at Bendyworks\n * We primarily do Ruby on Rails work, with some iOS now.\n * We care very deeply about software craftsmanship and honing our agile practices.\n\n
  • #6: Which leads me to my dilemma\n
  • #7: Are you familiar with impostor syndrome? It&apos;s the idea that even very skilled practitioners may sometimes feel like an impostor due to over-emphasizing their weaknesses.\nFurther, it&amp;#x2019;s the inability to internalize your own accomplishments.\n
  • #8: I felt like an impostor when it came to JavaScript.\n * I could read and write the syntax, pull in jQuery, manipulate the DOM, etc.\n * I had several projects under my belt at this time that used AJAX and were fairly complex\n * I&apos;d even read JavaScript: The Good Parts several times, taken notes, etc.\n
  • #9: \n
  • #10: brings us back to my thesis:\n
  • #11: But before that: Let me warn you that I&apos;m not going to go over every piece of syntax here.\nI&apos;m not going to be able to teach you all of CoffeeScript or Jasmine in this talk.\nFor that, see the resources &amp; books at the end of the talk.\n\n
  • #12: \n
  • #13: 2010 I learn about CoffeeScript, and it sort of looks like Ruby and Python.\nIt grabs my interest.\nBut at that point it&apos;s still a novelty: The compiler is in Ruby, no one uses it for real dev yet.\nIt was a *toy*\n
  • #14: Flash-forward to today, and everyone is extolling CoffeeScript.\nIt comes with Rails 3 by default now and gets compiled on the fly into JS for your app.\nI&amp;#x2019;ve been using CoffeeScript for about a year now, but not full time.\n
  • #15: So why use CoffeeScript? What are its good parts?\n* It restricts you to a subset of JS you&amp;#x2019;d recognize from JS: The Good Parts.\n* It puts JSLint &amp; a compiler between me and the half dozen browsers I need to support\n* It warns me when I do something wrong\n
  • #16: This might be the most important part of the talk, and reason to use CoffeeScript\nIf you&amp;#x2019;re like me, you&amp;#x2019;ll put the compiled JS up next to the CoffeeScript\nBy reading the output of the compiler, you&amp;#x2019;re learning what good JS looks like.\n
  • #17: It&apos;s not what runs in the browser.\nDifficult to debug =&gt; Finding bugs (Names are the same between CoffeeScript &amp; JS; its readable)\nMay feel like you&amp;#x2019;re learning a whole different language (It&amp;#x2019;s not, it&amp;#x2019;s less verbose JS)\n
  • #18: At this point we may want to differentiate between bugs that are caused by poor syntax and mistakes (mistake bugs), and bugs that come from the interaction between complicated data &amp; edge cases (ie computer is in a state you didn&apos;t predict when you wrote the code)\nCoffeeScript can help cut down on a lot of the former.\n\n
  • #19: \n
  • #20: This will print it&amp;#x2019;s true happily.\nThat&amp;#x2019;s not quite what you expect when the data is more complicated than 1 and the string 1.\nType coercion is the number 1 reason for WTF JS\n
  • #21: Ok, so that&amp;#x2019;s a very simple example.\nBut how often are you going to get bitten by more complicated versions of that same bug?\nAnd are you always going to remember to use triple equals? === I am now.\n
  • #22: \n
  • #23: \n
  • #24: JS and CoffeeScript have &amp;#x201C;lexical scope&amp;#x201D;\n
  • #25: \n
  • #26: \n
  • #27: Hopefully this is better than &amp;#x2018;null&amp;#x2019;, but you could do worse and just not initialize it at all.\nJavaScript won&amp;#x2019;t force you to initialize it, but doing so can help you to figure out scope issues.\n
  • #28: the ?= is syntactic sugar, the ? is called the existential operator in CoffeeScript\nCombined with =, the existential operator means &amp;#x201C;a equals b unless a?&amp;#x201D; or\n &amp;#x201C;Let b be the default value for a.&amp;#x201D;\n\n\n
  • #29: CoffeeScript can wrap each compiled file into a scope\nThis may be the default, depending on the version of coffeescript you&amp;#x2019;re using - option now.\nThis is actually pretty cool -- if you&amp;#x2019;re including a lot of JavaScripts on a website, you can&amp;#x2019;t mix scope there -- no accidental leakage into the global scope space.\n
  • #30: \n
  • #31: You write list comprehensions rather than for loops in CoffeeScript\nComprehensions are expressions, and can be returned and assigned\n
  • #32: CoffeeScript allows &amp;#x201C;reasonably solid&amp;#x201D; JavaScript developers to accomplish the latter by simply writing the former.\n\n
  • #33: \n
  • #34: \n
  • #35: I started using Jasmine last summer on a client project.\nIt&amp;#x2019;s enough like the BDD tool we use in Rails, Cucumber, that I consider it a BDD tool.\nIt makes the most sense to me of the BDD/TDD tools in JS I&amp;#x2019;ve used\n
  • #36: All code should be tested =&gt; that&amp;#x2019;s what I believe.\nYou can spend some up-front time testing your code, or you can spend a lot of time bug fixing later\nI realize that not all legacy codebases are going to be TDD overnight.\n
  • #37: \n
  • #38: \n
  • #39: Better? Not really. But we can see what the syntax is doing here\nand I&amp;#x2019;m using a real assertion!\n
  • #40: \n
  • #41: \n
  • #42: to make it easier to test the logic, things like AJAX calls, etc\nwithout interacting with the DOM\n
  • #43: it just so turns out, that the abstraction for testing is a better abstraction overall\n
  • #44: You can still do this in Jasmine, in fact, I find it kind of natural.\n
  • #45: This means you don&amp;#x2019;t need jQuery and you don&amp;#x2019;t need to run it in a real browser (but you can)\n
  • #46: \n
  • #47: \n
  • #48: You can use after Each to run a teardown function after each successful test\nIf you need a teardown function after a test whether it passed or failed, use after()\n
  • #49: In Ruby, we&amp;#x2019;ve been doing mocking and stubbing for awhile.\nJasmine&amp;#x2019;s spies make it easy!\nThese let you do things like watch to see if a method was called\nOr to stub out other methods so you don&amp;#x2019;t do real AJAX calls, etc.\n
  • #50: So you&amp;#x2019;re thinking, CoffeeScript and Jasmine sound great, but I have a legacy codebase.\nI&amp;#x2019;ll never get to use either; and they don&amp;#x2019;t help my big legacy codebase.\nWell, we&amp;#x2019;ve run into this and I have a plan.\n
  • #51: First, get your tools lined up.\nGet the CoffeeScript compiler in your tool chain, and get Jasmine set up and passing a dummy test.\nYou still haven&amp;#x2019;t done anything with your legacy code at this point.\n
  • #52: It all starts with one bug. Or one feature, if you&amp;#x2019;re feeling adventurous. \nYou might not be able to pull out an entire feature and rewrite it. I understand that.\nThe way to start this is to write a test around the bug and see it fail. (this might be hard -&gt; DOM)\nThen fix the bug in regular old JavaScript. See the Jasmine test pass.\n
  • #53: You&amp;#x2019;ve got a working test around this bug.\n(You know the test works because you saw it red then green.)\nNow&amp;#x2019;s your chance to rewrite it in CoffeeScript. It may only be one function at this point. That&amp;#x2019;s ok.\n
  • #54: We found that even with a legacy codebase of a lot of JavaScript, we were able to figure out logical chunks that should live together in CoffeeScript files.\n
  • #55: This is the hardest part.\nThe temptation is there to just give up and fix bugs only in JS, not to write unit tests, etc.\nThe other temptation is the one you usually can&amp;#x2019;t give into, which is to try to rewrite everything all at once -&gt; this rarely is accomplishable, it&amp;#x2019;s better to stage the changes.\n
  • #56: \n
  • #57: \n
  • #58: \n