SlideShare une entreprise Scribd logo
1  sur  58
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/

Contenu connexe

En vedette

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

En vedette (8)

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

Similaire à CoffeeScript & Jasmine - MadJS February 2012

Coffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesCoffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesBrandon Satrom
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with JasmineLeon van der Grient
 
CoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayCoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayEddie Kao
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with JasmineTim Tyrrell
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014Alex Kavanagh
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScriptStalin Thangaraj
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4alexsaves
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practicesSultan Khan
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Leveling Up at JavaScript
Leveling Up at JavaScriptLeveling Up at JavaScript
Leveling Up at JavaScriptRaymond Camden
 
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...Codemotion
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)Thinkful
 

Similaire à CoffeeScript & Jasmine - MadJS February 2012 (20)

Coffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesCoffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotes
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
test
testtest
test
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with Jasmine
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
CoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayCoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-Tuesday
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014
 
Coffeescript slides
Coffeescript slidesCoffeescript slides
Coffeescript slides
 
Coffee script
Coffee scriptCoffee script
Coffee script
 
Cucumber
CucumberCucumber
Cucumber
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practices
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Leveling Up at JavaScript
Leveling Up at JavaScriptLeveling Up at JavaScript
Leveling Up at JavaScript
 
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Cod...
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
 

Dernier

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 

Dernier (20)

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 

CoffeeScript & Jasmine - MadJS February 2012

Notes de l'éditeur

  1. \n
  2. First, an introduction:\n* I&amp;#x2019;m Matt Gauger\n
  3. \n
  4. * 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
  5. Which leads me to my dilemma\n
  6. 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
  7. 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
  8. \n
  9. brings us back to my thesis:\n
  10. 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
  11. \n
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. \n
  19. 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
  20. 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
  21. \n
  22. \n
  23. JS and CoffeeScript have &amp;#x201C;lexical scope&amp;#x201D;\n
  24. \n
  25. \n
  26. 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
  27. 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
  28. 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
  29. \n
  30. You write list comprehensions rather than for loops in CoffeeScript\nComprehensions are expressions, and can be returned and assigned\n
  31. CoffeeScript allows &amp;#x201C;reasonably solid&amp;#x201D; JavaScript developers to accomplish the latter by simply writing the former.\n\n
  32. \n
  33. \n
  34. 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
  35. 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
  36. \n
  37. \n
  38. Better? Not really. But we can see what the syntax is doing here\nand I&amp;#x2019;m using a real assertion!\n
  39. \n
  40. \n
  41. to make it easier to test the logic, things like AJAX calls, etc\nwithout interacting with the DOM\n
  42. it just so turns out, that the abstraction for testing is a better abstraction overall\n
  43. You can still do this in Jasmine, in fact, I find it kind of natural.\n
  44. 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
  45. \n
  46. \n
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. \n
  56. \n
  57. \n