Thursday, December 26, 2013

Day 1 -- Setup CoffeeScript



Hacker Workout's goal is to create short daily programming exercises. All the sample code for the next several days will be written in CoffeeScript. Here is why I chose CoffeeScript:

I initially thought JavaScript would be a good starting language, since it has become more important and is so easily accessible. When Google introduced its Chrome browser, one of its distinguishing features was how fast it could interpret the language. Since then, as all browsers have raced to create the fastest JavaScript virtual machine, there has been a movement to start designing webpages using JavaScript as a base language instead of primarily HTML markup. I first noticed this trend when I read Scott Hanselman's JavaScript is Assembly Language For the Web.

JavaScript has some different design characteristics from other languages. It has almost no meta-data, so metaprogramming strategies often do not work. On the other hand, it has strong support for encapsulation and first-class functions, which opens up lots of Object Oriented and functional-style approaches.

For professional development, JavaScript has several gotchas that make it hard to program, such as comparison rules. Probably the most annoying thing about JavaScript is the absence of native namespacing -- named functions are always global, and can easily collide and introduce bugs.

These complexities have lead to several attempts to create a better JavaScript. Some languages, such as closure and C#, try to map their syntax to best-practice JavaScript. Google is working on an actual replacement for JavaScript in a language called Dart. What I really wanted for the first few workouts, though, was a language that would help me learn JavaScript.

This left me with two candidates: Microsoft's superset language TypeScript, and CoffeeScript. What I liked most about CoffeeScript is that its design goal is to make JavaScript easier to write. In just a few minutes on the webpage, I started learning the language better.

Today's workout

Recommended Setup

Language: CoffeeScript 
Tools: Node.js (to make CoffeeScript work in a console window)

Warmup 

Write a function that takes a function as a parameter, paramA, and returns a function, returnA, such that when returnA is executed, it will print a message, then execute the function paramA.

Using Node.js, you can write to your console window by issuing the following command:

console.log "hello world"

Exercise

Write a simple unit test framework in CoffeeScript that handles the following tests:

Test "Addition Test", () -> Assert.Equal(4, 2 + 2)
 
Test "ExpectedFailure", () -> Assert.Exception( () -> throw "failure" )
 
Test "FailedTest", () -> Assert.True(false)

Happy hacking,
-James

No comments:

Post a Comment