Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CoffeeScript & Jasmine - MadJS February 2012

6,330 views

Published on

Published in: Technology
  • CoffeeScript: Accelerated JavaScript Development --- http://amzn.to/1nZ06aZ
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • CoffeeScript in Action --- http://amzn.to/1SaSGft
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The Little Book on CoffeeScript --- http://amzn.to/1T2KWys
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

CoffeeScript & Jasmine - MadJS February 2012

  1. 1. HowCoffeeScript &Jasmine mademe a better JSdeveloper
  2. 2. I’m Matt Gauger
  3. 3. @mathiasx
  4. 4. I work atBendyworks
  5. 5. My dilemma:
  6. 6. Impostor Syndrome
  7. 7. I was aJavaScriptimpostor
  8. 8. So, what does this have to do with CoffeeScript?
  9. 9. My thesis:
  10. 10. How CoffeeScript & Jasminemade me a better JSdeveloper(and how it can help you,too)
  11. 11. CoffeeScript
  12. 12. History
  13. 13. Today
  14. 14. CoffeeScript has Good Parts?
  15. 15. CoffeeScript: JavaScript:
  16. 16. CoffeeScript: JavaScript: ^ Major Win ^
  17. 17. Criticisms ofCoffeeScript
  18. 18. That isnt to say thatCoffeeScripteliminates all bugs.
  19. 19. Some examples:
  20. 20. Coercing the wrong type if (1 == 1) { console.log("its true!");}
  21. 21. CoffeeScript:if 1 is ‘1’ console.log ‘never true!’Compiles to:if (1 === ‘1’) { console.log("never true!");}
  22. 22. Scope
  23. 23. CoffeeScript scope isessentially the same as in JS
  24. 24. 1. Every function creates a scope, andthe only way to create a scope is todefine a function.2. A variable lives in the outermostscope in which an assignment has beenmade to that variable.3. Outside of its scope, a variable isinvisible.
  25. 25. The neat thing is thatCoffeeScript’s compilerplaces the vars foreach scope at the topof that scope
  26. 26. For example, this compiled CoffeeScript: var singCountdown;singCountdown = function(count) { var singBottleCount, singDecrement; singBottleCount = function(specifyLocation) { var bottleStr, locationStr; // ... } // ...}
  27. 27. Define a variable ata specific scope bygiving it a sensible initial value
  28. 28. a ?= bis one way to give a default
  29. 29. Lastly, wrapping up your code.
  30. 30. Compiled CoffeeScript Example:(function() { console.log “hello world!”;}).call(this);
  31. 31. Simpler Looping
  32. 32. Jeremy Ashkenas’s ExampleLoop over every item in a list, in CoffeeScript: for item in list process itemIntention gets obscured by managing the loop, in JS: for (var i = 0, l = list.length; i < l; i++) { var item = list[i]; process(item); }
  33. 33. 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
  34. 34. Jasmine
  35. 35. (My) History(with Jasmine)
  36. 36. Why Jasmine?
  37. 37. The example on the site:describe("Jasmine", function() { it("makes testing awesome!", function() { expect(yourCode).toBeLotsBetter(); });});
  38. 38. This example sucks!
  39. 39. describe (addition, function () {      it(adds two numbers, function () {          expect(1 + 2).toEqual(3);      });  });
  40. 40. How should we test JS?
  41. 41. Functions should notdepend on the DOM
  42. 42. Our logic needs tobe in separate pieces
  43. 43. Easier to test = better code
  44. 44. Follow TDD/BDD:red, green, refactor
  45. 45. Jasmine is designed to be standalone
  46. 46. Some really coolfeatures of Jasmine:
  47. 47. Matchers:•.toBe()•.toBeNull()•.toBeTruthy()•.toBeDefined()•.toBeUndefined()•etc.
  48. 48. Setup and Teardown:•beforeEach()•afterEach()
  49. 49. Spies: built-inmocking & stubbing
  50. 50. But what aboutlegacy codebases?
  51. 51. Start simple.
  52. 52. Fix one bug.(red, green, refactor)
  53. 53. Rewrite the affectedcode in CoffeeScript.
  54. 54. Start grouping in files / modules.
  55. 55. Keep improving the codebase.
  56. 56. 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!
  57. 57. Thanks!
  58. 58. Resources to learn more:•http://jashkenas.github.com/coffee-script/•http://pivotal.github.com/jasmine/•http://pragprog.com/book/tbcoffee/coffeescript•http://js2coffee.org/

×