<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-15643421</atom:id><lastBuildDate>Fri, 10 Feb 2012 22:52:01 +0000</lastBuildDate><category>Personal</category><category>Visual Studio</category><category>Microsoft</category><category>Behavior-Driven Development</category><category>Architecture</category><category>Family</category><category>SourceControl</category><category>Core Skills</category><category>Castle</category><category>Windows</category><category>ASP.NET</category><category>Web</category><category>C++</category><category>WF</category><category>Node.js</category><category>Community</category><category>DirectoryServices</category><category>Software Design</category><category>StructureMap</category><category>Humor</category><category>AutoMapper</category><category>Design Patterns</category><category>Windows Forms</category><category>Continuous Integration</category><category>Domain-Driven Design</category><category>Music</category><category>ALT.NET</category><category>Java</category><category>NoSQL</category><category>Open Source</category><category>CouchDB</category><category>E-VAN</category><category>Conferences</category><category>CoffeeScript</category><category>NServiceBus</category><category>Agile</category><category>WCF</category><category>Ruby</category><category>Database</category><category>Linux</category><category>Test-Driven Development</category><category>NHibernate</category><category>Tools</category><category>Object-Relational Mapping</category><category>JavaScript</category><category>WPF</category><category>Fluent Interfaces</category><category>.NET</category><category>Concurrent Programming</category><category>Books</category><title>Share the intellectual wealth</title><description /><link>http://vanryswyckjan.blogspot.com/</link><managingEditor>noreply@blogger.com (Jan Van Ryswyck)</managingEditor><generator>Blogger</generator><openSearch:totalResults>407</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ShareTheIntellectualWealth" /><feedburner:info uri="sharetheintellectualwealth" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8855460789148181495</guid><pubDate>Tue, 24 Jan 2012 11:00:00 +0000</pubDate><atom:updated>2012-01-24T12:00:03.582+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Introducing TrackMyRun</title><description>&lt;p align="justify"&gt;I’ve been working on a small pet project for a couple of weeks now, which I named &lt;a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank"&gt;TrackMyRun&lt;/a&gt;. I’m quite fanatic when it comes to running, doing about 130 runs a year. Currently I’m keeping track of all these runs in a simple spreadsheet, but off course, that’s certainly not &lt;em&gt;“the geek way”&lt;/em&gt;. Hence the start of yet another pet project.&lt;/p&gt; &lt;p align="justify"&gt;TrackMyRun is written using &lt;a href="http://coffeescript.org/" target="_blank"&gt;CoffeeScript&lt;/a&gt;, &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt; and &lt;a href="http://expressjs.com/" target="_blank"&gt;Express&lt;/a&gt;. I’m also using the &lt;a href="http://twitter.github.com/bootstrap/" target="_blank"&gt;Bootstrap toolkit&lt;/a&gt; from Twitter for styling. I’m intending to actually use this small application for myself by hosting it on &lt;a href="http://www.heroku.com/" target="_blank"&gt;Heroku&lt;/a&gt; or some other cloud solution as soon as I’m able to finish the most essential features. It’s far from done yet, but you can already have a look at &lt;a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank"&gt;the source code on GitHub&lt;/a&gt;.&amp;nbsp; As always, suggestions are welcome.&lt;/p&gt; &lt;p align="justify"&gt;Happy reading!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-8855460789148181495?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/pDbd9yP1rmg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/pDbd9yP1rmg/introducing-trackmyrun.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/01/introducing-trackmyrun.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-100753416497362106</guid><pubDate>Sat, 21 Jan 2012 00:17:00 +0000</pubDate><atom:updated>2012-01-22T11:48:04.738+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Express Routing</title><description>&lt;p align="justify"&gt;In the previous post I provided a short &lt;a href="http://vanryswyckjan.blogspot.com/2011/12/taking-toddler-steps-with-nodejs.html" target="_blank"&gt;introduction to Express&lt;/a&gt;, a web development framework built on top of &lt;a href="https://github.com/senchalabs/connect" target="_blank"&gt;connect&lt;/a&gt; that is heavily inspired by &lt;a href="http://www.sinatrarb.com/" target="_blank"&gt;Sinatra&lt;/a&gt;. For this post we’ll dive into a couple of styles for dealing with routes in Express. &lt;/p&gt; &lt;p align="justify"&gt;Express simply uses HTTP verbs for its routing API. &lt;/p&gt;&lt;pre style="width: 100%; height: 368px" class="csharpcode"&gt;&lt;span class="rem"&gt;// Index&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });            

&lt;span class="rem"&gt;// New run &lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/new'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Create a new run&lt;/span&gt;
app.post(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Show run&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Edit run&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/:id/edit'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Update run&lt;/span&gt;
app.put(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Delete run&lt;/span&gt;
app.delete(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Well, that’s pretty much all that you need to get started. The routes that we specified here are treated as plain old regular expressions. Note that in order to make the &lt;em&gt;put&lt;/em&gt; and &lt;em&gt;delete&lt;/em&gt; routes work, we have to add a hidden field to the view.&lt;/p&gt;&lt;pre style="width: 100%; height: 23px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="_method"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="PUT"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="hidden"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;But the thing that I personally struggled with the most was finding out a decent way to divide up these routes into separate modules without too much of a hassle. Most sample and demo applications out there that use Express usually have all their routes specified in &lt;a href="https://github.com/alexyoung/nodepad/blob/master/app.js" target="_blank"&gt;a single app.js file&lt;/a&gt;. This is something that I don’t like very much as this can become unmaintainable faster that you might think. Roughly 2000 years ago, there was this great emperor (and many after him) who valued the principle of &lt;a href="http://en.wikipedia.org/wiki/Divide_and_rule" target="_blank"&gt;Divide and Conquer&lt;/a&gt;. In order to create maintainable applications, being able to divide up these routes is quite essential. There are several ways to do this.&lt;/p&gt;
&lt;h4&gt;Express Resource&lt;/h4&gt;
&lt;p&gt;This library enables us provide resourceful routing. As usual, &lt;a href="https://github.com/visionmedia/express-resource" target="_blank"&gt;express-resource&lt;/a&gt; can be installed using npm by using the following command:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;em&gt;npm install express-resource&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;Using express-resource, we can create controller modules and use them from our main module. The following snippet shows how a simple controller looks like:&lt;/p&gt;&lt;pre style="width: 100%; height: 503px" class="csharpcode"&gt;exports.index = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Index runs'&lt;/span&gt;);
};

exports.&lt;span class="kwrd"&gt;new&lt;/span&gt; = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'New run'&lt;/span&gt;);
};

exports.create = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Create run'&lt;/span&gt;);
};

exports.show = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Show run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.edit = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request response){
    response.send(&lt;span class="str"&gt;'Edit run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.update = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Update run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.destroy = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Delete run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Now in the main module (app.js) we just have to add the following code:&lt;/p&gt;&lt;pre style="width: 100%; height: 86px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; resource = require(&lt;span class="str"&gt;'express-resource'&lt;/span&gt;)

&lt;span class="rem"&gt;// ...&lt;/span&gt;

application.resource(&lt;span class="str"&gt;'runs'&lt;/span&gt;, require(&lt;span class="str"&gt;'./routes/runs'&lt;/span&gt;));&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
That’s it! Now all these routes are hooked up and ready to use. Express-resource has a few other neat features as well. Check out &lt;a href="http://www.nodetuts.com/tutorials/23-express-resources.html#video" target="_blank"&gt;this episode from Node Tuts&lt;/a&gt; to learn more. &lt;/p&gt;
&lt;p align="justify"&gt;Although this seems like a good solution to divide up routes into controller modules, somehow it doesn’t resonate with me. All routes for a particular resource need to exist in the same module which still feels a bit unwieldy to me. I want to have an even more granular approach.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;Super-duper Require&lt;/h4&gt;
&lt;p&gt;What I like to do is to separate routes based on their context:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;runs/index.js ( index route ) 
&lt;li&gt;runs/new.js ( new and create routes ) 
&lt;li&gt;runs/show.js ( show route ) 
&lt;li&gt;runs/edit.js ( edit and update routes ) 
&lt;li&gt;runs/delete.js ( delete route )&lt;/li&gt;&lt;/ul&gt;
&lt;p align="justify"&gt;Wouldn’t it be cool if we could just &lt;em&gt;“require”&lt;/em&gt; the &lt;em&gt;runs&lt;/em&gt; directory and hook up all routes exported by all the modules that exist in this directory? Well, meet super_duper_require! While still using express-resource, we can now add all these routes like so:&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 22px" class="csharpcode"&gt;application.resource(&lt;span class="str"&gt;'runs'&lt;/span&gt;, super_duper_require(module, &lt;span class="str"&gt;'./routes/runs/'&lt;/span&gt;));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;This is how the super_duper_require function looks like:&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 197px" class="csharpcode"&gt;_ = require(&lt;span class="str"&gt;'underscore'&lt;/span&gt;);

&lt;span class="kwrd"&gt;function&lt;/span&gt; super_duper_require(mod, path) {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; mixin = {};
      fileSystem.readdirSync(path)
          .forEach(&lt;span class="kwrd"&gt;function&lt;/span&gt;(filename) {
            _.extend(mixin, mod.require(path + filename));
        });

      &lt;span class="kwrd"&gt;return&lt;/span&gt; mixin;
};&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;We just use the magnificent &lt;a href="http://documentcloud.github.com/underscore/" target="_blank"&gt;underscore.js&lt;/a&gt; library here to hook things up. This is just one of the fancy ways to solve the granularity problem. If we don’t want to use the express-resource library, we can always accomplish the same thing by going “plain old school” style.&lt;/p&gt;
&lt;h4 align="justify"&gt;Plain Old School&lt;/h4&gt;
&lt;p align="justify"&gt;This is how I currently set up routing with Express. We no longer need the express-resource library for setting up our routes, but we can still use the same granularity as shown earlier. We also need &lt;em&gt;underscore.js&lt;/em&gt; again, just as in the previous example, in order to stitch things together.&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 329px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; routes = require(&lt;span class="str"&gt;'./routes'&lt;/span&gt;);
&lt;span class="kwrd"&gt;var&lt;/span&gt; routes.runs = _.extend(require(&lt;span class="str"&gt;'./routes/runs'&lt;/span&gt;), 
                           require(&lt;span class="str"&gt;'./routes/runs/new'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/show'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/edit'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/delete'&lt;/span&gt;));
                           
...

&lt;span class="kwrd"&gt;function&lt;/span&gt; bootstrapRoutes(application) {
    app.get(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, routes.runs.index);            
    app.get(&lt;span class="str"&gt;'/runs/new'&lt;/span&gt;, routes.runs.&lt;span class="kwrd"&gt;new&lt;/span&gt;);
    app.post(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, routes.runs.create);
    app.get(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.show);
    app.get(&lt;span class="str"&gt;'/runs/:id/edit'&lt;/span&gt;, routes.runs.edit);
    app.put(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.update);
    app.delete(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.delete);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;Up until now I’m pretty happy with this approach. I would love to hear how others divide up their routes into several modules. So please let me know if there are other awesomely cool ways to deal with this. In the mean time, I hope this helps.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-100753416497362106?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/AOCMhLHFx6E" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/AOCMhLHFx6E/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/01/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1868878734194587635</guid><pubDate>Fri, 30 Dec 2011 20:44:00 +0000</pubDate><atom:updated>2011-12-30T21:44:55.090+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Core Skills</category><category domain="http://www.blogger.com/atom/ns#">Community</category><title>Retrospective of 2011, Looking Ahead to 2012</title><description>&lt;p align="justify"&gt;Yep, that time of the year again. Shiny new calendars and the accompanying festivities are upon us again. I can’t get rid of the feeling that every year passes by a lot faster than the year before. This year was definitely not an exception in that regard and I’m afraid that things are not going to improve in 2012.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Nonetheless, a lot of stuff happened this year, good and not so good. First of all, I changed jobs twice this year. I’ve been working at &lt;a href="http://vanryswyckjan.blogspot.com/2011/09/my-first-day-at-ichoosr.html" target="_blank"&gt;iChoosr&lt;/a&gt; for a couple of months now and it still feels like I’ve finally come home. I really enjoy working there, my colleagues are awesome folks and I learned a ton already. I do hope to have at least the same amount of fun next year.&lt;/p&gt; &lt;p align="justify"&gt;While enjoying my .NET job during the day, I’ve been doing a lot of Node.js hacking in my spare time, which you could probably tell looking the amount of blog posts I’ve been writing on this topic this year. This has all been a lot of good fun. I have a couple of more blog posts on Node.js lined up for next year, so stay tuned.&lt;/p&gt; &lt;p align="justify"&gt;The new programming language I learned this year was &lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html" target="_blank"&gt;CoffeeScript&lt;/a&gt;, which looks a lot like Ruby. Unfortunately, Ruby is still on my wish list for new programming languages to learn as is &lt;a href="http://clojure.org/" target="_blank"&gt;Clojure&lt;/a&gt; and/or &lt;a href="http://www.scala-lang.org/" target="_blank"&gt;Scala&lt;/a&gt;. I wonder which of these I’m going to pick up in 2012?&lt;/p&gt; &lt;p align="justify"&gt;One of the things that I’m eager to learn about is another operating system. I’ve been soaking in a lot of Unix/Linux stuff during the last couple of months. I’ve been using and developing software for the Windows platform like forever and learning an entire new OS has not been easy. But I must say that it’s well worth the time and effort. I’ve been developing Node.js libraries and applications in my spare time entirely on &lt;a href="http://www.ubuntu.com/" target="_blank"&gt;Ubuntu&lt;/a&gt; and I’m looking forward to take a peek at &lt;a href="http://linuxmint.com/" target="_blank"&gt;Linux Mint&lt;/a&gt; as well. Perhaps I’ll completely switch over to Linux next year, I’m not entirely sure about that yet.&lt;/p&gt; &lt;p align="justify"&gt;I’ve been facilitating a good number of &lt;a href="http://europevan.blogspot.com/" target="_blank"&gt;European VAN&lt;/a&gt; sessions throughout the year. 2011 has been the third year for the E-VAN and I must say that it’s been a fun ride. But I do feel that the time has come for me to move on. I must admit that I only opened Visual Studio a couple of times in my spare time, and then only by accident. Being really honest with myself, I’m just not that interested anymore in the latest and greatest in the .NET space compared to only a couple of years ago. I think I somehow hit a saturation point. So I’m no longer going to organize and/or host new E-VAN sessions. I’ll be more than happy to pass the torch to other developers out there who are willing to step up to the plate. I hereby want to thank all the speakers and those who contributed in the discussions for all their efforts. The recordings are still there and I do hope many folks were able to learn something. I know I most certainly did.&lt;/p&gt; &lt;p align="justify"&gt;Having a job closer to home and also being able to work from home at least once a week ensures that I’m able to spend more time with my family. I’m probably most thankful for that. Working out has been a lot of fun this year as well. I was able to drastically improve myself by running longer distances and also running a lot faster. I’m planning to participate in even more street runs during the next year. I’m looking forward to sustainably improve even further without overloading my body. This is definitely one of the biggest challenges for the upcoming year.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;All that’s left for me here is to wish you all the best for the new year!&lt;/p&gt; &lt;p align="justify"&gt;Until next year.&amp;nbsp; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-1868878734194587635?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/LtqnCBlKyzM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/LtqnCBlKyzM/retrospective-of-2011-looking-ahead-to.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/retrospective-of-2011-looking-ahead-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8424735782966451759</guid><pubDate>Fri, 23 Dec 2011 23:12:00 +0000</pubDate><atom:updated>2012-01-20T22:14:21.684+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js – Express</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;There are several frameworks out there for building web applications with Node.js, one being more successful than the other. &lt;a href="http://expressjs.com/"&gt;Express&lt;/a&gt; is probably the most popular and well known web development framework for Node.js. It’s heavily inspired by &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; and built on top of &lt;a href="https://github.com/senchalabs/connect"&gt;connect&lt;/a&gt;, which provides a middleware layer in order to easily add several capabilities and therefore extend your web application. As with most Node.js libraries, Express can be easily installed using &lt;a href="http://npmjs.org/"&gt;npm&lt;/a&gt;.&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;font style="background-color: #ffffff"&gt;&lt;em&gt;npm install express&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;The following code snippet shows the most basic HTTP server example using Express:&lt;/p&gt;&lt;pre style="width: 100%; height: 137px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; express = require(&lt;span class="str"&gt;'express'&lt;/span&gt;);
&lt;span class="kwrd"&gt;var&lt;/span&gt; application = express.createServer();

application.get(&lt;span class="str"&gt;'/'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) {
    response.send(&lt;span class="str"&gt;'Hello Express!!'&lt;/span&gt;);
});

application.listen(2455);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Notice that this doesn’t differ that much from most Node.js “Hello World” examples out there. So if we point our browser to &lt;em&gt;localhost:2455&lt;/em&gt;, then the expected text message appears on the page. Now let’s add some middleware into the mix.&lt;/p&gt;
&lt;p align="justify"&gt;Suppose we want to serve some static content like HTML, CSS and/or JavaScript files. We can simply accomplish that by adding the following line of code.&lt;/p&gt;&lt;pre style="width: 100%; height: 19px" class="csharpcode"&gt;application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Now we simply have to add a new folder named &lt;em&gt;“public”&lt;/em&gt; to the root folder of our application. In this folder we add an HTML file named &lt;em&gt;“index.html”&lt;/em&gt; with the following content:&lt;/p&gt;&lt;pre style="width: 100%; height: 132px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Index&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Hello Express!!&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Now we have to point our browser to &lt;em&gt;localhost:2455/index.html&lt;/em&gt; and our static HTML file is served by Express. That’s how easy we can add capabilities to our web application. Express provides you with several built-in middleware like the one we just used in the last example, but there are also several other open-source middleware in the npm repository to choose from.&lt;/p&gt;
&lt;p align="justify"&gt;Middleware is usually added in a callback function that we provide with the &lt;em&gt;configure&lt;/em&gt; method of the &lt;em&gt;application&lt;/em&gt; object.&lt;/p&gt;&lt;pre style="width: 100%; height: 53px" class="csharpcode"&gt;application.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));    
});&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Express also provides the ability to configure middleware targeted for specific environments like development, staging, production, etc. … .&lt;/p&gt;&lt;pre class="csharpcode"&gt;application.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="rem"&gt;// Shared configuration&lt;/span&gt;
    application.use(express.bodyParser());
});

application.configure(&lt;span class="str"&gt;'development'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the development environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public_on_development'&lt;/span&gt;));    
});

application.configure(&lt;span class="str"&gt;'staging'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the staging environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public_on_staging'&lt;/span&gt;));    
});

application.configure(&lt;span class="str"&gt;'production'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the production environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));    
});&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Note that we can still provide a separate configuration function for shared configurations between different environments. When we start our web application in the staging environment, we just have to provide the NODE_ENV environment variable.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;NODE_ENV=staging nodemon app.js&lt;/em&gt;&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;Now only the global &lt;em&gt;configure&lt;/em&gt; function and the one for the staging environment are called.&lt;/p&gt;
&lt;p align="justify"&gt;Express also provides an executable for generating boilerplate code and setting up a basic web application. This is probably the quickest way to get up and running with Express.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;node_modules/express/bin$ ./express –h&lt;/em&gt;&lt;/p&gt;&lt;em&gt;&lt;/em&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;em&gt;
&lt;p&gt;Usage: express [options] [path]&lt;/p&gt;
&lt;p&gt;Options:&lt;br&gt;&amp;nbsp; -s, –sessions&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add session support&lt;br&gt;&amp;nbsp; -t, --template &amp;lt;engine&amp;gt;&amp;nbsp; add template &amp;lt;engine&amp;gt; support (jade|ejs). default=jade&lt;br&gt;&amp;nbsp; -c, --css &amp;lt;engine&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add stylesheet &amp;lt;engine&amp;gt; support (stylus). default=plain css&lt;br&gt;&amp;nbsp; -v, –version&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output framework version&lt;br&gt;&amp;nbsp; -h, –help&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output help information&lt;br&gt;&lt;/p&gt;&lt;/em&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;So when I run the &lt;em&gt;express&lt;/em&gt; executable with the default options, I get the following output:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;./express /home/my_user/my_sample_app&lt;br&gt;&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;create : /home/my_user/my_sample_app&lt;br&gt;create : /home/my_user/my_sample_app/package.json&lt;br&gt;create : /home/my_user/my_sample_app/app.js&lt;br&gt;create : /home/my_user/my_sample_app/public&lt;br&gt;create : /home/my_user/my_sample_app/views&lt;br&gt;create : /home/my_user/my_sample_app/views/layout.jade&lt;br&gt;create : /home/my_user/my_sample_app/views/index.jade&lt;br&gt;create : /home/my_user/my_sample_app/routes&lt;br&gt;create : /home/my_user/my_sample_app/routes/index.js&lt;br&gt;create : /home/my_user/my_sample_app/public/javascripts&lt;br&gt;create : /home/my_user/my_sample_app/public/images&lt;br&gt;create : /home/my_user/my_sample_app/public/stylesheets&lt;br&gt;create : /home/my_user/my_sample_app/public/stylesheets/style.css&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;don’t forget to install dependencies:&lt;br&gt;$ cd /home/my_user/my_sample_app &amp;amp;&amp;amp; npm install&lt;br&gt;&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;A &lt;em&gt;package.json&lt;/em&gt; file is created for dependent modules, along with a public folder for JavaScript files and CSS stylesheets and even separate folders for views and routes. This is how the generated &lt;em&gt;app.js&lt;/em&gt; file looks like:&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre style="width: 100%; height: 669px" class="csharpcode"&gt;&lt;span class="rem"&gt;/**&lt;/span&gt;
&lt;span class="rem"&gt; * Module dependencies.&lt;/span&gt;
&lt;span class="rem"&gt; */&lt;/span&gt;

&lt;span class="kwrd"&gt;var&lt;/span&gt; express = require(&lt;span class="str"&gt;'express'&lt;/span&gt;)
  , routes = require(&lt;span class="str"&gt;'./routes'&lt;/span&gt;)

&lt;span class="kwrd"&gt;var&lt;/span&gt; app = module.exports = express.createServer();

&lt;span class="rem"&gt;// Configuration&lt;/span&gt;

app.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.set(&lt;span class="str"&gt;'views'&lt;/span&gt;, __dirname + &lt;span class="str"&gt;'/views'&lt;/span&gt;);
  app.set(&lt;span class="str"&gt;'view engine'&lt;/span&gt;, &lt;span class="str"&gt;'jade'&lt;/span&gt;);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));
});

app.configure(&lt;span class="str"&gt;'development'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.use(express.errorHandler({ dumpExceptions: &lt;span class="kwrd"&gt;true&lt;/span&gt;, showStack: &lt;span class="kwrd"&gt;true&lt;/span&gt; })); 
});

app.configure(&lt;span class="str"&gt;'production'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.use(express.errorHandler()); 
});

&lt;span class="rem"&gt;// Routes&lt;/span&gt;

app.get(&lt;span class="str"&gt;'/'&lt;/span&gt;, routes.index);

app.listen(3000);
console.log(&lt;span class="str"&gt;"Express server listening on port %d in %s mode"&lt;/span&gt;, 
       app.address().port, 
       app.settings.env);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As you can see, it’s very easy to get started with Express and adding more capabilities to your web application as you go along. Make sure to watch this &lt;a href="http://www.screenr.com/mAL"&gt;short screencast&lt;/a&gt; in order to get up and running in no time. &lt;/p&gt;
&lt;p align="justify"&gt;I’m planning to put out a couple more blog posts on Express and some very useful middleware that you can use for your applications. So stay tuned.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-8424735782966451759?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/WbI78mXqhg4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/WbI78mXqhg4/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6107071579664178536</guid><pubDate>Fri, 16 Dec 2011 21:10:00 +0000</pubDate><atom:updated>2011-12-23T21:29:40.923+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – Linking Local Packages with npm</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;I just wanted to share a very neat feature of &lt;a href="http://npmjs.org" target="_blank"&gt;npm&lt;/a&gt; that makes life quite a lot easier when developing libraries for &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt;. Suppose that we’re building our very own ORM library for Node.js (we all build an ORM at some point in our career, right?). In order to have our dog food and eat it too, we want to start using our ORM library in an application as soon as possible. As with all other modules, we want to use npm in order to add it as a dependency of our application. But we also don’t want to publicly publish our half-baked ORM package to the npm repository either. This is where creating an npm link saves the day.&lt;/p&gt; &lt;p align="justify"&gt;In the local folder of our library we have to create a link by issuing the following command:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;~/kick-ass-orm$&amp;nbsp; npm link &lt;/em&gt;&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;After that we can add this package to &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs.html" target="_blank"&gt;the node-modules folder&lt;/a&gt; of our application by executing the following command:&lt;/p&gt; &lt;blockquote&gt;&lt;em&gt;~/facebook-killer$ npm link kick-ass-orm&lt;/em&gt;&lt;br&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;There you have it. Notice that a new folder with the name of the package is added to the &lt;em&gt;node_modules&lt;/em&gt; folder of the application. This is simply a &lt;a href="http://en.wikipedia.org/wiki/Symbolic_link" target="_blank"&gt;symlink&lt;/a&gt; to the package folder of our ORM library. This means that when we make changes to this library, those changes are also exposed to our application as well.&lt;/p&gt; &lt;p align="justify"&gt;As soon as we feel comfortable enough about our ORM library, we can make it publicly available in the npm repository. We can then simply remove the link from our application by issuing the following command:&lt;/p&gt; &lt;blockquote&gt;&lt;em&gt;~/facebook-killer$ npm unlink kick-ass-orm&lt;/em&gt;&lt;br&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;At this point we add our freshly published package to the &lt;em&gt;package.json&lt;/em&gt; file of our application. We can also remove the link from our local package folder by executing the following command:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;~/kick-ass-orm$ npm unlink &lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;This particular feature has been very useful to me over the last couple of weeks. &lt;/p&gt; &lt;p align="justify"&gt;npm – is there anything it can’t do? &lt;/p&gt; &lt;p align="justify"&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-6107071579664178536?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/jCqz8PEhn-Q" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/jCqz8PEhn-Q/taking-baby-steps-with-nodejs-linking.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/taking-baby-steps-with-nodejs-linking.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3809703544417418701</guid><pubDate>Tue, 18 Oct 2011 10:00:00 +0000</pubDate><atom:updated>2011-10-18T12:00:03.362+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – BDD Style Unit Tests with Jasmine and CoffeeScript</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js" target="_blank"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;In a previous blog post, I already wrote about &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs-bdd-style.html" target="_blank"&gt;BDD style unit tests&lt;/a&gt; for testing Node.js modules using Jasmine. I really like the conciseness, simplicity and readability of specification style unit tests using Jasmine. But when we pour &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt; into the mix, then the syntax for BDD style unit tests really starts to get interesting. &lt;/p&gt; &lt;p align="justify"&gt;Here’s a simple example of a Jasmine test suite written in CoffeeScript:&lt;/p&gt;&lt;pre class="csharpcode"&gt;{ BurglarAlarm, BurglarAlarmState } = require(&lt;span class="str"&gt;'burglaralarm'&lt;/span&gt;)
ControlRoom = require(&lt;span class="str"&gt;'controlroom'&lt;/span&gt;)
should = require(&lt;span class="str"&gt;'should'&lt;/span&gt;)
sinon = require(&lt;span class="str"&gt;'sinon'&lt;/span&gt;)

describe &lt;span class="str"&gt;'An armed burglar alarm'&lt;/span&gt;, -&amp;gt;
    _sut = &lt;span class="kwrd"&gt;null&lt;/span&gt;

    beforeEach -&amp;gt;
        _sut = &lt;span class="kwrd"&gt;new&lt;/span&gt; BurglarAlarm()
        _sut.arm()

    describe &lt;span class="str"&gt;'When a break in occurs'&lt;/span&gt;, -&amp;gt;
        beforeEach -&amp;gt;
            _sut.breakIn()

        it &lt;span class="str"&gt;'should indicate that there is a burglary'&lt;/span&gt;, -&amp;gt;
            _sut.state().should.equal(BurglarAlarmState.alarm)

        it &lt;span class="str"&gt;'should trigger the siren'&lt;/span&gt;, -&amp;gt;
            _sut.siren().isRinging().should.be.ok
    
    describe &lt;span class="str"&gt;'When there is being tampered'&lt;/span&gt;, -&amp;gt;
        _mockedControlRoom = &lt;span class="kwrd"&gt;null&lt;/span&gt;

        beforeEach -&amp;gt;
            _mockedControlRoom = sinon.mock(ControlRoom.getInstance())
            _mockedControlRoom.expects(&lt;span class="str"&gt;'notifyTamperAlarm'&lt;/span&gt;).once()
            
            _sut.tamper()
            
        afterEach -&amp;gt;
            _mockedControlRoom.restore()

        it &lt;span class="str"&gt;'should indicate that there is a tamper alarm'&lt;/span&gt;, -&amp;gt;
            _sut.state().should.equal(BurglarAlarmState.tamper)

        it &lt;span class="str"&gt;'should notify the control room'&lt;/span&gt;, -&amp;gt;
            _mockedControlRoom.verify()&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;Personally, I really like this syntax. Notice the &lt;a href="http://vanryswyckjan.blogspot.com/2011/08/exploring-coffeescript-part-6-show-me.html" target="_blank"&gt;destructuring assignment&lt;/a&gt; on the first line. This is a small trick we can use when a particular module exports multiple types. We also used a mocking library called &lt;a href="https://github.com/cjohansen/Sinon.JS" target="_blank"&gt;Sinon.js&lt;/a&gt; which is a very powerful library for test spies, stubs and mocks for JavaScript.&lt;/p&gt;
&lt;p align="justify"&gt;Here’s the code for the &lt;em&gt;BurglarAlarm&lt;/em&gt;, &lt;em&gt;ControlRoom&lt;/em&gt; and &lt;em&gt;Siren&lt;/em&gt; modules. Note that this example code is also written using CoffeeScript, but this can be plain old JavaScript as well if you want. &lt;/p&gt;&lt;pre class="csharpcode"&gt;##############################################
# BurglarAlarm module
##############################################
BurglarAlarmState = 
    normal: 0
    armed: 1
    alarm: 2
    tamper: 3

exports.BurglarAlarmState = BurglarAlarmState

&lt;span class="kwrd"&gt;class&lt;/span&gt; exports.BurglarAlarm
    _siren = &lt;span class="kwrd"&gt;null&lt;/span&gt;
    _state = BurglarAlarmState.normal
    
    constructor: -&amp;gt;
        _siren = &lt;span class="kwrd"&gt;new&lt;/span&gt; Siren()    
        
    siren: -&amp;gt; _siren
    state: -&amp;gt; _state

    arm: -&amp;gt; _state = BurglarAlarmState.armed

    breakIn: -&amp;gt; 
        _state = BurglarAlarmState.alarm
        _siren.makeSomeNoise()    

    tamper: -&amp;gt;
        _state = BurglarAlarmState.tamper
        ControlRoom.getInstance().notifyTamperAlarm()

##############################################
# ControlRoom module
##############################################
&lt;span class="kwrd"&gt;class&lt;/span&gt; ControlRoom
    notifyTamperAlarm: -&amp;gt;
        console.log(&lt;span class="str"&gt;'Tamper alarm noticed in the control room ...'&lt;/span&gt;)

instance = &lt;span class="kwrd"&gt;null&lt;/span&gt;

module.exports = {
    getInstance: -&amp;gt;
        unless instance?
            instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; ControlRoom()
        instance
}

##############################################
# Siren module
##############################################
module.exports = &lt;span class="kwrd"&gt;class&lt;/span&gt; Siren
    ringing = &lt;span class="kwrd"&gt;false&lt;/span&gt;

    isRinging: -&amp;gt; ringing
    makeSomeNoise: -&amp;gt; ringing = true&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;There you go. Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-3809703544417418701?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/ShYynA4aYBg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/ShYynA4aYBg/taking-baby-steps-with-nodejs-bdd-style.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/10/taking-baby-steps-with-nodejs-bdd-style.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-5971142480993110661</guid><pubDate>Fri, 14 Oct 2011 22:26:00 +0000</pubDate><atom:updated>2011-10-15T00:26:45.287+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Open Source</category><title>The Cathedral and the Bazaar</title><description>&lt;p align="justify"&gt;A couple of weeks ago, I ran into &lt;a href="http://catb.org/~esr/writings/homesteading/" target="_blank"&gt;this website from Eric S. Raymond&lt;/a&gt;, the author of the book &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0596001088/elegantcode-20" target="_blank"&gt;The Cathedral and the Bazaar&lt;/a&gt;. There’s a &lt;a href="http://catb.org/~esr/writings/homesteading/linux1_d50_96kbs.mp3" target="_blank"&gt;recording of a great presentation&lt;/a&gt; that anyone involved with software development, and especially open-source, should listen to. I highly recommended that you download this recording and at the very least listen to it twice! You can also &lt;a href="http://catb.org/~esr/writings/homesteading/cathedral-bazaar/" target="_blank"&gt;read the book online&lt;/a&gt;. &lt;/p&gt; &lt;p align="justify"&gt;Enjoy!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-5971142480993110661?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/gMallQlxoxI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/gMallQlxoxI/cathedral-and-bazaar.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/10/cathedral-and-bazaar.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-4223238240508035958</guid><pubDate>Fri, 23 Sep 2011 20:21:00 +0000</pubDate><atom:updated>2011-09-23T22:21:19.260+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Conferences</category><title>Presenting “Taking Baby Steps with Node.js” at Agile.NET 2011 Europe</title><description>&lt;p align="justify"&gt;In a couple of weeks, I’ll be doing an introductory presentation on &lt;a href="http://nodejs.org/"&gt;Node.js&lt;/a&gt; at the &lt;a href="http://www.agileminds.be/event/5"&gt;Agile.NET 2011 conference&lt;/a&gt;. While having a look at the other sessions and speakers, I’m quite honored to be able to join these smart bunch of craftsmen. In fact, I’m still amazed that the organizers accepted my proposal. But hey, they’ll probably let me do my talk somewhere in the basement anyway :-). &lt;/p&gt; &lt;p align="justify"&gt;You probably figured out by now that you can’t afford to miss these two days of high quality learning, especially if you live in the European part of the world. If you live in Belgium or the Netherlands, you simply don’t have any valid excuse for not attending. &lt;/p&gt; &lt;p align="justify"&gt;If you haven’t registered yet, be sure to do this in the upcoming week in order to get an early-bird discount. And in addition, I can even hook you up with an additional 50 Euro discount. Just drop me a line.&lt;/p&gt; &lt;p align="justify"&gt;Hope to see you there.&amp;nbsp; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-4223238240508035958?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/1piPdGUsizA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/1piPdGUsizA/presenting-taking-baby-steps-with.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/09/presenting-taking-baby-steps-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3478873515242489173</guid><pubDate>Mon, 12 Sep 2011 19:23:00 +0000</pubDate><atom:updated>2011-09-12T21:23:18.716+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Personal</category><title>My First Day at iChoosr</title><description>&lt;p align="justify"&gt;Today was my very first day at &lt;a href="http://www.ichoosr.com/nl/index.php?s=about" target="_blank"&gt;iChoosr&lt;/a&gt;, a small internet startup that focuses on &lt;a href="http://en.wikipedia.org/wiki/Vendor_Relationship_Management" target="_blank"&gt;Vendor Relationship Management&lt;/a&gt;. I’ll be working there as a web developer in a small team of very bright people. Working for a small startup is a totally new experience for me. So I’m very excited about what the future will bring.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Pretty sure there’s lots of learning ahead of me &lt;img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://lh6.ggpht.com/-qQDUYvqBPwk/Tm5cJQSW2WI/AAAAAAAABno/4JtjIyiJLsc/wlEmoticon-smile%25255B2%25255D.png?imgmax=800"&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-3478873515242489173?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/jA0LEZ_nxq8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/jA0LEZ_nxq8/my-first-day-at-ichoosr.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-qQDUYvqBPwk/Tm5cJQSW2WI/AAAAAAAABno/4JtjIyiJLsc/s72-c/wlEmoticon-smile%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/09/my-first-day-at-ichoosr.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-5811253626515520200</guid><pubDate>Fri, 09 Sep 2011 18:32:00 +0000</pubDate><atom:updated>2011-09-09T20:32:24.924+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Test-Driven Development</category><title>An Observation about TDD</title><description>&lt;p align="justify"&gt;To me, developers that are not applying TDD practices during their day-to-day job always seem more in a hurry than developers that do apply red-green-refactor. In their hurry, they're the first to cut corners and start making messes while they rush to their goal. A while ago it dawned to me why that is. They subconsciously want to get feedback as soon as possible about the code they're writing. They cut corners and generally mess up their code in order to prevent spending those extra hours and days to keep things clean. Constantly refactoring and cleaning up their code is restraining them from having the feedback they so desperately want.  &lt;p align="justify"&gt;Humans are in fact feedback junkies. We constantly want to know how we're doing what we're doing. I actually wrote &lt;a href="http://vanryswyckjan.blogspot.com/2007/05/agile-is-just-human-nature.html" target="_blank"&gt;a blog post&lt;/a&gt; about this a couple of years ago.  &lt;p align="justify"&gt;Since I adopted TDD as a discipline, I tend to feel less pressured which results in me taking the time to continuously refactor the code I'm working on, trying to keep everything clean. Why? Because I known that the code I wrote a minute ago works. The tests I write constantly give me a shot of feedback so that I'm constantly hooked.&amp;nbsp; &lt;p align="justify"&gt;Just an observation ...&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-5811253626515520200?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/OKxLAK7F5YE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/OKxLAK7F5YE/observation-about-tdd.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/09/observation-about-tdd.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8577748219043107352</guid><pubDate>Tue, 06 Sep 2011 10:00:00 +0000</pubDate><atom:updated>2011-09-06T12:00:16.513+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Book Review: The Clean Coder</title><description>&lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/0137081073/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline; float: left" alt="The Clean Coder: A Code of Conduct for Professional Programmers" align="left" src="http://photo.goodreads.com/books/1295654320l/10284614.jpg" width="185" height="247"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;Earlier this week I finished reading &lt;a href="http://cleancoder.posterous.com/" target="_blank"&gt;Uncle Bob’s&lt;/a&gt; latest book &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0137081073/elegantcode-20" target="_blank"&gt;The Clean Coder&lt;/a&gt;. Robert C. Martin is a great writer and I very much enjoyed reading his &lt;a href="http://vanryswyckjan.blogspot.com/2008/08/book-review-agile-principles-patterns.html" target="_blank"&gt;previous&lt;/a&gt; &lt;a href="http://vanryswyckjan.blogspot.com/2008/10/book-review-clean-code.html" target="_blank"&gt;books&lt;/a&gt;. His latest work is no exception and I found it to be yet again a fascinating read. &lt;/p&gt; &lt;p align="justify"&gt;This book is all about professionalism. This is something that is very much needed in the field of software development. It describes how a professional software craftsman behaves, how he deals with tight schedules, irrational decisions made by managers (for those rare occasions that this happens), conflicts and so forth. The preface of the book takes you right by the throat, talking about the Challenger disaster. This has been applied to the field of software engineering many times already (check out &lt;a href="http://duartes.org/gustavo/blog/post/richard-feynman-challenger-disaster-software-engineering" target="_blank"&gt;this blog post&lt;/a&gt; from &lt;a href="http://duartes.org/gustavo/blog/" target="_blank"&gt;Gustavo Duarte&lt;/a&gt; which is one of my all-time favorites), but still, it definitely never wears off.&lt;/p&gt; &lt;p align="justify"&gt;The book is filled with stories and anecdotes from the rich career of the author and the lessons he learned during these 40+ years in the IT industry. Some of the ideas in there are definitely challenging (like staying out of the zone and building up focus instead), but nonetheless they put a very&amp;nbsp; interesting perspective on things. &lt;/p&gt; &lt;p align="justify"&gt;Don’t let yourself get carried away by some of the hard statements but try to focus on the underlying ideas and try to think back on some of the good and bad situations that you ran into during your own career. Trying to reflect on those moments and considering how we could act more professionally lies at the heart of improving ourselves. I definitely learned a lot while reading this book and I encourage you to pick up a copy as soon as possible and take some time to read it. &lt;/p&gt; &lt;p align="justify"&gt;Two thumbs up!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-8577748219043107352?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/mVJ5JzuVjxM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/mVJ5JzuVjxM/book-review-clean-coder.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/09/book-review-clean-coder.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6521104322417311924</guid><pubDate>Tue, 16 Aug 2011 10:00:00 +0000</pubDate><atom:updated>2011-08-16T12:00:02.897+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Book Reviews: CoffeeScript and JavaScript Web Applications</title><description>&lt;p align="justify"&gt;While catching up on my reading backlog, I particularly enjoyed reading two new books I bought recently which I’m going to briefly discuss in this blog post.&lt;/p&gt; &lt;h3&gt;&lt;/h3&gt; &lt;h3 align="justify"&gt;1. CoffeeScript - Accelerated JavaScript Development&lt;/h3&gt; &lt;p align="justify"&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/1934356786/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 0px 10px 5px 0px; display: inline; float: left" alt="Cover Image For CoffeeScript" align="left" src="http://imagery.pragprog.com/products/219/tbcoffee_xlargecover.jpg?1304278335"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/1934356786/elegantcode-20" target="_blank"&gt;CoffeeScript – Accelerated JavaScript Development&lt;/a&gt;, written by &lt;a href="http://trevorburnham.com/" target="_blank"&gt;Trevor Burnham&lt;/a&gt;, gets you up and running with CoffeeScript in no time. This book was recently released by the excellent &lt;a href="http://pragprog.com/" target="_blank"&gt;Pragmatic Programmers&lt;/a&gt; which only increased my expectations.&amp;nbsp; And I must admit that Trevor did a tremendous job explaining the ins and outs of this fairly new programming language. With only a good 120 pages, it’s a very quick read so one is able to start coding away as soon as possible.&lt;/p&gt; &lt;p align="justify"&gt;The book contains six chapters. The first four chapters basically cover the different language features of CoffeeScript. The last two chapters explain how to use CoffeeScript both on the client-side along with &lt;a href="http://jquery.com/" target="_blank"&gt;jQuery&lt;/a&gt; and the server-side with &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt;. At the end of each chapter there’s an example application (5x5 solitaire) that gets refactored throughout the book as more language features are covered. Every chapter also provides some exercises where you can put your teeth into. &lt;/p&gt; &lt;p align="justify"&gt;Now, I have to warn you that in order to get the most out of this book, you need to have some decent knowledge of JavaScript. Some of this stuff isn’t trivial and being able to imagine how the underlying JavaScript would look like is almost required. &lt;/p&gt; &lt;p align="justify"&gt;I highly recommend this book if you want to learn this great little programming language.&lt;/p&gt; &lt;h3 align="justify"&gt;2. JavaScript Web Applications&lt;/h3&gt; &lt;p align="justify"&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/144930351X/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline; float: left" alt="Book cover of JavaScript Web Applications" align="left" src="http://covers.oreilly.com/images/0636920018421/rc_cat.gif"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;By the time of this writing, JavaScript Web Applications by &lt;a href="http://alexmaccaw.co.uk/"&gt;Alex MacCaw&lt;/a&gt; hasn’t officially been released yet. It’s still in the early release phase which means that there are still a good number of typos in there. That aside, this book contains some rock solid content on how to build client-side JavaScript applications. &lt;/p&gt; &lt;p align="justify"&gt;This book isn’t about server-side applications with the occasional piece of JavaScript code used in only a couple of web pages. As it’s name implies, this book is all about building &lt;a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller"&gt;MVC&lt;/a&gt; applications for the browser using JavaScript.&lt;/p&gt; &lt;p align="justify"&gt;After the obligatory introductory chapter, chapter 2 up to chapter 6 discuss each part of the MVC pattern and events. The author thoroughly explains each part of this design pattern while implementing his own client-side MVC library. Lots and lots of code here.&lt;/p&gt; &lt;p align="justify"&gt;Chapter 7 provides a clear explanation on dependency management in JavaScript and how to use module loaders to mitigate this problem. Chapter 8 and 9 cover some of the API’s that come with the upcoming HTML 5 standard, particularly on how to deal with files using JavaScript as well as WebSockets. Chapter 10 deals with testing and debugging. But chapter 11 is definitely my favorite chapter of the book where the author shows a good number of techniques on deploying a JavaScript web application and how to deal with performance. This chapter alone was a real eye opener. Finally, the last three chapters provide an in-depth introduction to three of the most popular open-source client-side JavaScript libraries out there, namely &lt;a href="http://maccman.github.com/spine/#h-spine"&gt;Spine.js&lt;/a&gt;, &lt;a href="http://documentcloud.github.com/backbone/"&gt;Backbone.js&lt;/a&gt; and &lt;a href="http://javascriptmvc.com/"&gt;JavaScriptMVC&lt;/a&gt;. &lt;/p&gt; &lt;p align="justify"&gt;Again I have to warn you that this book is definitely not for JavaScript beginners or the faint of heart. There were times that it even got me scratching my head from time to time. There’s some really neat JavaScript stuff in there, so I highly recommend that you pick up this book some time on your JavaScript learning path.&lt;/p&gt; &lt;p align="justify"&gt;Happy reading an until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-6521104322417311924?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/IBsJ3F-qLPM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/IBsJ3F-qLPM/book-reviews-coffeescript-and.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/08/book-reviews-coffeescript-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-352794995665478713</guid><pubDate>Thu, 11 Aug 2011 10:00:00 +0000</pubDate><atom:updated>2011-08-11T12:00:16.233+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><title>Dynamic in C# is Broken</title><description>&lt;p align="justify"&gt;&lt;a name="_GoBack"&gt;&lt;/a&gt;Earlier this week, I ran into an issue while using the dynamic keyword in C#. I learned from &lt;a href="http://elegantcode.com/2011/03/22/book-review-c-in-depth2nd-edition/"&gt;C# in Depth&lt;/a&gt; that there are a couple of restrictions with dynamic, most notably when using extension methods or converting lambda expressions. But apparently there are more restrictions than meets the eye, which came as an unpleasant surprise. Let’s look at some code.&lt;/p&gt;&lt;pre style="width: 100%; height: 276px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IPresenter
{
    &lt;span class="kwrd"&gt;void&lt;/span&gt; Start(dynamic startupData);
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ISpecificPresenter : IPresenter
{}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SpecificPresenter : ISpecificPresenter
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Start(dynamic startupData)
    {
        &lt;span class="rem"&gt;// ...&lt;/span&gt;
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Here we have an interface called &lt;i&gt;IPresenter&lt;/i&gt; with a single method on it named &lt;i&gt;Start&lt;/i&gt;. Notice that the &lt;i&gt;Start&lt;/i&gt; method has a single parameter which is defined as dynamic. We also have another interface called &lt;i&gt;ISpecificPresenter&lt;/i&gt; that inherits from &lt;i&gt;IPresenter&lt;/i&gt;. Finally we have a concrete class that implements the &lt;i&gt;ISpecificPresenter&lt;/i&gt; interface. So far, so good. 
&lt;p&gt;Next we have a class called &lt;i&gt;Activator&lt;/i&gt; that we use to kick-off a particular presenter.&lt;/p&gt;&lt;pre style="width: 100%; height: 163px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Activator
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Start&amp;lt;TPresenter&amp;gt;(dynamic startupData)
        &lt;span class="kwrd"&gt;where&lt;/span&gt; TPresenter : IPresenter
    {
        var presenter = ObjectFactory.GetInstance&amp;lt;TPresenter&amp;gt;();
        presenter.Start(startupData);
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The &lt;i&gt;Activator&lt;/i&gt; class has a generic &lt;i&gt;Start&lt;/i&gt; method with a parameter named &lt;i&gt;startupData&lt;/i&gt; that is also dynamic. Here we simply get an instance of a requested presenter from &lt;a href="http://structuremap.net/structuremap/"&gt;StructureMap&lt;/a&gt; and call its &lt;i&gt;Start&lt;/i&gt; method. This is how to call the &lt;i&gt;Start&lt;/i&gt; method of the &lt;i&gt;Activator&lt;/i&gt; class :&lt;/p&gt;&lt;pre style="width: 100%; height: 41px" class="csharpcode"&gt;var activator = &lt;span class="kwrd"&gt;new&lt;/span&gt; Activator();
activator.Start&amp;lt;ISpecificPresenter&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt;{});&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;When we run this code, we get the following exception: 
&lt;blockquote&gt;
&lt;p align="justify"&gt;Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'ISpecificPresenter' does not contain a definition for 'Start'.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;This had me stumped for a while until I added a cast from &lt;i&gt;ISpecificPresenter&lt;/i&gt; to &lt;i&gt;IPresenter&lt;/i&gt; in the Start method of the Activator class.&lt;/p&gt;&lt;pre style="width: 100%; height: 159px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Activator
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Start&amp;lt;TPresenter&amp;gt;(dynamic startupData)
        &lt;span class="kwrd"&gt;where&lt;/span&gt; TPresenter : IPresenter
    {
        var presenter = (IPresenter)ObjectFactory.GetInstance&amp;lt;TPresenter&amp;gt;();
        presenter.Start(startupData);
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Now everything works as expected. But I’m not entirely sure why this isn’t behaving as I expected. I guess it has something to do with the fact that the &lt;i&gt;Start&lt;/i&gt; method is defined on the &lt;i&gt;IPresenter&lt;/i&gt; interface and not on the &lt;i&gt;ISpecificPresenter&lt;/i&gt; interface. 
&lt;p align="justify"&gt;&lt;a href="http://lh4.ggpht.com/-Re0FMkzle_0/TjyCfyRkp7I/AAAAAAAABnY/iBEAGKx8Td8/s1600-h/image%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-S-NN286VWIk/TjyCgqCxnaI/AAAAAAAABnc/0NfSYp44nEU/image_thumb%25255B2%25255D.png?imgmax=800" width="447" height="112"&gt;&lt;/a&gt; 
&lt;p align="justify"&gt;Although I very much like the flexibility that dynamic objects bring to the C# language, I’m also starting to think that it might not be such a good idea to bring dynamic concepts to a statically typed language. Running into these kind of issues and limitations reinforces this growing opinion. 
&lt;p&gt;I would much appreciate it if anyone could enlighten me with a good explanation. 
&lt;p&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-352794995665478713?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/dqnfeX8ltD4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/dqnfeX8ltD4/dynamic-in-c-is-broken.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-S-NN286VWIk/TjyCgqCxnaI/AAAAAAAABnc/0NfSYp44nEU/s72-c/image_thumb%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/08/dynamic-in-c-is-broken.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3396493932832834407</guid><pubDate>Tue, 09 Aug 2011 10:00:00 +0000</pubDate><atom:updated>2011-08-09T12:00:06.053+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 6 - Show Me the Goodies !</title><description>&lt;p align="justify"&gt;For the final blog post in this series, we’re going to go over a couple of cool little nuggets that one can use to write some elegant &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt; code.&lt;/p&gt; &lt;p&gt;Also check out the previous installments:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html"&gt;And Then There Was Coffee&lt;/a&gt;  &lt;li&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-2-variables.html"&gt;Variables and Functions&lt;/a&gt;  &lt;li&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html"&gt;More on Functions&lt;/a&gt;  &lt;li&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-4-objects.html"&gt;Objects and Classes&lt;/a&gt;  &lt;li&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/08/exploring-coffeescript-part-5-ranges.html" target="_blank"&gt;Ranges, Loops and Comprehensions&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h4&gt;Destructuring Assignments&lt;/h4&gt; &lt;p align="justify"&gt;Destructuring assignments are a great syntactic enhancement for those cases where we want to extract (deeply) nested property values from arrays or objects and store these values into variables.&lt;/p&gt; &lt;p align="justify"&gt;First let’s have a look at a simple code example written in plain JavaScript where we simply assign some property values of an object to a couple of variables.&lt;/p&gt;&lt;pre style="width: 100%; height: 318px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; podcast = {
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;,
    description: &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;,
    details: {
        homepage: &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;,
        rss: &lt;span class="str"&gt;"http://www.astronomycast.com/feed/"&lt;/span&gt;,
        atom: &lt;span class="str"&gt;"http://www.astronomycast.com/feed/atom/"&lt;/span&gt;    
    }
};

&lt;span class="kwrd"&gt;var&lt;/span&gt; title = podcast.title;
&lt;span class="kwrd"&gt;var&lt;/span&gt; description = podcast.description;
&lt;span class="kwrd"&gt;var&lt;/span&gt; homepage = podcast.details.homepage;

console.log(title);
console.log(description);
console.log(homepage);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Using destructuring assignments in CoffeeScript, we can assign the property values of the object to the variables using a single line of code.&lt;/p&gt;&lt;pre style="width: 100%; height: 246px" class="csharpcode"&gt;podcast =
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    description: &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;
    details:
        homepage: &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;
        rss: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/'&lt;/span&gt;
        atom: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/atom/'&lt;/span&gt;

{title, description, details: { homepage } } = podcast

console.log title
console.log description
console.log homepage&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Besides objects, destructuring assignments also works for arrays as well.&lt;/p&gt;&lt;pre style="width: 100%; height: 151px" class="csharpcode"&gt;favoritePodcasts = [&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;, 
                    &lt;span class="str"&gt;'Talking Shop Down Under'&lt;/span&gt;, &lt;span class="str"&gt;'The Changelog'&lt;/span&gt;, &lt;span class="str"&gt;'Pluralcast'&lt;/span&gt;];
[favorite01, favorite02, favorite03, favorite04, favorite05] = favoritePodcasts

console.log favorite01
console.log favorite02
console.log favorite03
console.log favorite04
console.log favorite05&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;And of course, this can also be used when combine objects with arrays and vice versa.&lt;/p&gt;&lt;pre style="width: 100%; height: 341px" class="csharpcode"&gt;customer =
    firstName: &lt;span class="str"&gt;'Chuck'&lt;/span&gt;
    lastName: &lt;span class="str"&gt;'Norris'&lt;/span&gt;
    orders: [
        { 
            Id: 1 
            Item: &lt;span class="str"&gt;'Knuckle Duster'&lt;/span&gt;
        },
        { 
            Id: 2
            Item: &lt;span class="str"&gt;'Shuriken'&lt;/span&gt; 
        }
    ]

{orders: [ {Item: item1}, {Item: item2}]} = customer

console.log item1
console.log item2&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
This small little language nugget has definitely come in handy quite more often than I first anticipated. So do keep the availability of destructuring assignments in the back of your head while developing CoffeeScript code.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4 align="justify"&gt;String Interpolation&lt;/h4&gt;
&lt;p align="justify"&gt;The syntax for string interpolation is heavily inspired on the Ruby syntax. Let’s have a look.&lt;/p&gt;&lt;pre style="width: 100%; height: 102px" class="csharpcode"&gt;podcast = 
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    download: (episode) -&amp;gt;
        console.log &lt;span class="str"&gt;"Downloading #{episode} of #{@title}."&lt;/span&gt;

podcast.download &lt;span class="str"&gt;'the first episode'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;In this example we fill in the values of the &lt;em&gt;episode&lt;/em&gt; parameter and the &lt;em&gt;title&lt;/em&gt; property into the string that we send to the &lt;em&gt;console.log&lt;/em&gt; method. Unfortunately string interpolation only works for double quoted strings. I personally prefer single-quoted strings when I’m writing CoffeeScript code. But I gladly make an exception when I want to use string interpolation.&lt;/p&gt;
&lt;h4 align="justify"&gt;Strict Equality&lt;/h4&gt;
&lt;p align="justify"&gt;In JavaScript, we have the == and the === operator. Both equality operators behave the same way only the first performs type coercion while the latter enforces type equality. It's a common best practice in JavaScript to always use the strict equality operator and explicitly perform type casts if needed. CoffeeScript on the other hand only has only one equality operator which always translates to JavaScript's strict equality operator behind the scenes. &lt;/p&gt;
&lt;p align="justify"&gt;So the following equality comparison written in CoffeeScript translates to the JavaScript code shown bellow:&lt;/p&gt;&lt;pre style="width: 100%; height: 204px" class="csharpcode"&gt;# CoffeeScript
x = &lt;span class="str"&gt;'str'&lt;/span&gt;
y = 12
console.log x == y        # Outputs &lt;span class="str"&gt;'false'&lt;/span&gt;


&lt;span class="rem"&gt;// JavaScript&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; x, y;
x = &lt;span class="str"&gt;'str'&lt;/span&gt;;
y = 12;
console.log(x === y);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As I already mentioned in one of the previous blog posts, CoffeeScript emits JavaScript that follows best practices and complies to &lt;a href="http://www.jslint.com/" target="_blank"&gt;JSLint&lt;/a&gt; without warnings. Always using the === operator is part of being compliant.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4 align="justify"&gt;Conditionals&lt;/h4&gt;
&lt;p align="justify"&gt;CoffeeScript adds some nicely readable syntactic sugar to be used with conditionals using keywords like &lt;em&gt;unless&lt;/em&gt;, &lt;em&gt;is&lt;/em&gt;, &lt;em&gt;isnt&lt;/em&gt;, &lt;em&gt;and&lt;/em&gt; and &lt;em&gt;or&lt;/em&gt;. Let’s look at an example.&lt;/p&gt;&lt;pre style="width: 100%; height: 207px" class="csharpcode"&gt;cobol = vb = &lt;span class="kwrd"&gt;false&lt;/span&gt;
cool = &lt;span class="kwrd"&gt;true&lt;/span&gt;

unless cobol &lt;span class="kwrd"&gt;is&lt;/span&gt; cool
    console.log &lt;span class="str"&gt;'CoffeeScript is awesome!'&lt;/span&gt;

&lt;span class="kwrd"&gt;if&lt;/span&gt; vb isnt cool
    console.log &lt;span class="str"&gt;'CoffeeScript is awesome!'&lt;/span&gt;

&lt;span class="kwrd"&gt;if&lt;/span&gt; cobol isnt cool and vb isnt cool
    console.log &lt;span class="str"&gt;'CoffeeScript is awesome!'&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
I really like how these keywords enable us to write fluently readable conditional statements in CoffeeScript.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4 align="justify"&gt;The Existential Operator&lt;/h4&gt;
&lt;p align="justify"&gt;The existential operator in CoffeeScript, expressed using the ? symbol, returns true except when a particular variable is null or undefined. Let’s look at some code.&lt;/p&gt;&lt;pre style="width: 100%; height: 126px" class="csharpcode"&gt;someVariable = &lt;span class="kwrd"&gt;null&lt;/span&gt;
someOtherVariable = 12

console.log someVariable ? someOtherVariable        # Outputs 12

unless someVariable?
    console.log &lt;span class="str"&gt;'someVariable is null'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;But the existential operator can also be used to check for null or undefined before accessing a property or the result of a function. This is also called a &lt;em&gt;soak&lt;/em&gt; which is the accessor variant of the existential operator. Again some code to clarify things.&lt;/p&gt;&lt;pre style="width: 100%; height: 396px" class="csharpcode"&gt;podcast =
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    description: &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;
    details:
        homepage: &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;
        rss: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/'&lt;/span&gt;
        atom: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/atom/'&lt;/span&gt;

    download: -&amp;gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;

# Outputs &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;
console.log podcast.details.homepage    

# TypeError: Cannot read property &lt;span class="str"&gt;'publishingDate'&lt;/span&gt; of undefined            
console.log podcast.moreDetails.publishingDate        

# Outputs undefined
console.log podcast.moreDetails?.publishingDate     

# Also outputs undefined
console.log podcast.download()?.data&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;When the property in question has a valid value, then the expected result is returned. In case the property does not exist or contains null, then undefined is returned instead of a TypeError being thrown. &lt;/p&gt;
&lt;p align="justify"&gt;So that’s it for now. I hope that you enjoyed reading this blog series on CoffeeScript and perhaps you are now also convinced that using CoffeeScript is a viable and productive alternative for writing JavaScript based applications.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-3396493932832834407?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/3QM5ATIK-OI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/3QM5ATIK-OI/exploring-coffeescript-part-6-show-me.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/08/exploring-coffeescript-part-6-show-me.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-7207405055317279094</guid><pubDate>Fri, 05 Aug 2011 23:46:00 +0000</pubDate><atom:updated>2011-08-06T01:46:15.199+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Moving from E-TextEditor to Sublime Text 2</title><description>&lt;p align="justify"&gt;I’ve been using &lt;a href="http://www.e-texteditor.com/" target="_blank"&gt;E-TextEditor&lt;/a&gt; for more than a year now for doing all my JavaScript, Node.js and CoffeeScript development. You can think of E-TextEditor as &lt;a href="http://macromates.com/" target="_blank"&gt;TextMate&lt;/a&gt; for the Windows platform. I’ve been pretty happy with it throughout this period as it can be extended pretty easily with a whole slew of bundles that are already available for TextMate. E-TextEditor requires you to install &lt;a href="http://www.cygwin.com/" target="_blank"&gt;Cygwin&lt;/a&gt; which happens automatically during installation. Using the Cygwin command-line gave me enough (re-)exposure to a Linux-like environment in order for me wanting to (re-)learn about this other platform. More on that in later blog posts. Although there haven’t been any new versions of E-TextEditor for quite some time now, the current version enabled me to write JavaScript code in a productive way.&lt;/p&gt; &lt;p align="justify"&gt;Only recently I found out about another editor called &lt;a href="http://www.sublimetext.com/2" target="_blank"&gt;Sublime Text 2&lt;/a&gt; (thanks to &lt;a href="http://twitter.com/#!/casualjim" target="_blank"&gt;Ivan Porto Carrero&lt;/a&gt;). I downloaded this text editor in order to give it a try and I must say that I haven’t opened E-TextEditor ever since. This second version of Sublime Text is still in development but it’s pretty stable as I haven’t run into any issues so far. The cool part is that this editor is available for both Windows, Linux and OS X which enabled me to move all my Node.js and CoffeeScript development from Cygwin on Windows to Ubuntu Linux.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;CoffeeScript isn’t supported out-of-the-box (yet), so I installed the &lt;a href="https://github.com/jashkenas/coffee-script-tmbundle" target="_blank"&gt;CoffeeScript&amp;nbsp; TextMate bundle&lt;/a&gt; written by &lt;a href="http://ashkenas.com/" target="_blank"&gt;Jeremy Ashkenas&lt;/a&gt; (yes, the same one from the &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt; and &lt;a href="http://documentcloud.github.com/backbone/" target="_blank"&gt;Backbone.js&lt;/a&gt; fame). Also check out &lt;a href="https://github.com/jashkenas/coffee-script/wiki/Text-editor-plugins" target="_blank"&gt;this page on the CoffeeScript wiki&lt;/a&gt; for more information on how to bring some CoffeeScript happiness to your own favorite text editor.&lt;/p&gt; &lt;p align="justify"&gt;Setting up this TextMate bundle for use with Sublime Text 2 was actually pretty easy. Just navigate to &lt;em&gt;/home/&amp;lt;my_user_name&amp;gt;/.config/sublime-text-2/Packages&lt;/em&gt; in a terminal (or the corresponding file path on Windows) and clone the source of the CoffeeScript TextMate bundle using git: &lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;git clone git://github.com/jashkenas/coffee-script-tmbundle CoffeeScript&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;Next close all the .coffee files and restart Sublime Text 2. Now we have some nice syntax highlighting and some useful code snippets at our disposal.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-pHrNbg1llp8/TjyAwdbrTxI/AAAAAAAABnM/tPv6lU7xock/s1600-h/image%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-V2MBO5O4CdQ/TjyAxCFU8lI/AAAAAAAABnQ/eCEcFe0AXFg/image_thumb%25255B1%25255D.png?imgmax=800" width="600" height="396"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;It's also possible to hook into &lt;a href="http://sublimetext.info/docs/en/reference/build_systems.html" target="_blank"&gt;the build system&lt;/a&gt; of Sublime Text 2. You just need to create a new file named &lt;em&gt;CoffeeScript.sublime-build&lt;/em&gt; at the following location: &lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;/home/&amp;lt;my_user_name&amp;gt;/.config/sublime-text-2/Packages/User&amp;nbsp; &lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;You can add the following to this new file for compiling CoffeeScript&lt;/p&gt;&lt;pre style="width: 100%; height: 85px" class="csharpcode"&gt;{
     &lt;span class="str"&gt;"cmd"&lt;/span&gt;: [&lt;span class="str"&gt;"coffee"&lt;/span&gt;, &lt;span class="str"&gt;"-c"&lt;/span&gt;, &lt;span class="str"&gt;"$file"&lt;/span&gt;],
     &lt;span class="str"&gt;"selector"&lt;/span&gt; : &lt;span class="str"&gt;"source.coffee"&lt;/span&gt;,
     &lt;span class="str"&gt;"path"&lt;/span&gt; : &lt;span class="str"&gt;"/usr/local/bin"&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
or the following for executing/running CoffeeScript code.&lt;pre style="width: 100%; height: 85px" class="csharpcode"&gt;{
     &lt;span class="str"&gt;"cmd"&lt;/span&gt;: [&lt;span class="str"&gt;"coffee"&lt;/span&gt;, &lt;span class="str"&gt;"$file"&lt;/span&gt;],
     &lt;span class="str"&gt;"selector"&lt;/span&gt; : &lt;span class="str"&gt;"source.coffee"&lt;/span&gt;,
     &lt;span class="str"&gt;"path"&lt;/span&gt; : &lt;span class="str"&gt;"/usr/local/bin"&lt;/span&gt;
}&lt;/pre&gt;
&lt;p align="justify"&gt;Now when you hit F7, this fresh build command is executed when a CoffeeScript source file is showing in the active tab. It’s also possible to change the build short-key to your own taste through the &lt;em&gt;Preferences&lt;/em&gt; menu.&amp;nbsp; &lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p align="justify"&gt;Unfortunately, there’s only one build command as I would love to create a separate command for compiling and running CoffeeScript code (or perhaps I missed this feature during the excitement &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://lh5.ggpht.com/-PaksebwgLrw/TjyAxnsqsDI/AAAAAAAABnU/a8EOv8OU07k/wlEmoticon-winkingsmile%25255B2%25255D.png?imgmax=800"&gt;). &lt;/p&gt;
&lt;p align="justify"&gt;Some more usage is definitely needed for me to learn more about this excellent text editor, but currently I’m pretty happy with it. I also need to find out how well JavaScript is supported and perhaps see how far one can go when doing some C# development using Mono. I expect that there are a good number of productive features in there for me to discover.&lt;/p&gt;
&lt;p align="justify"&gt;I encourage you have a decent look at this excellent tool. &lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-7207405055317279094?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/a1wA4lys4Ek" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/a1wA4lys4Ek/moving-from-e-texteditor-to-sublime.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-V2MBO5O4CdQ/TjyAxCFU8lI/AAAAAAAABnQ/eCEcFe0AXFg/s72-c/image_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/08/moving-from-e-texteditor-to-sublime.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3907115817986185221</guid><pubDate>Tue, 02 Aug 2011 10:00:00 +0000</pubDate><atom:updated>2011-08-02T12:00:06.463+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 5 - Ranges, Loops and Comprehensions</title><description>&lt;p align="justify"&gt;For this blog post, we’re going to discuss ranges, loops and comprehensions in &lt;a href="http://jashkenas.github.com/coffee-script/"&gt;CoffeeScript&lt;/a&gt;. &lt;/p&gt; &lt;p align="justify"&gt;Also check out the previous installments:&lt;/p&gt; &lt;ol&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html" target="_blank"&gt;And Then There Was Coffee&lt;/a&gt;&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-2-variables.html" target="_blank"&gt;Variables and Functions&lt;/a&gt;&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html" target="_blank"&gt;More on Functions&lt;/a&gt;&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-4-objects.html" target="_blank"&gt;Objects and Classes&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h4 align="justify"&gt;Ranges&lt;/h4&gt;  &lt;p align="justify"&gt;Again a feature that is heavily inspired by Ruby. Using a range we can define an array of consecutive numbers by only specifying the first and the last number.&lt;/p&gt;&lt;pre class="csharpcode"&gt;numbers = [1..10]
console.log numbers        # Outputs [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]&lt;/pre&gt;
&lt;ol&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ol&gt;
&lt;p align="justify"&gt;This is a so-called inclusive range which is denoted by the two dots separating the bounding numbers. CoffeeScript also provides the concept of an exclusive range which is denoted by three dots instead of two.&lt;/p&gt;&lt;pre class="csharpcode"&gt;numbers = [1...10]
console.log numbers        # Outputs [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;In this case the number of the upper boundary is excluded. Ranges are a small addition to the syntax that can come in handy from time to time.&lt;/p&gt;
&lt;h4&gt;Loops&lt;/h4&gt;
&lt;p align="justify"&gt;As with JavaScript, CoffeeScript also provides us with two different kinds of &lt;em&gt;for&lt;/em&gt;&amp;nbsp;&lt;em&gt;loops&lt;/em&gt;: one for iterating over an array and one for iterating over the properties of an object. &lt;/p&gt;&lt;pre style="width: 100%; height: 147px" class="csharpcode"&gt;download = (podcast) -&amp;gt;
    console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + podcast

favoritePodcasts = [&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;, 
                    &lt;span class="str"&gt;'Talking Shop Down Under'&lt;/span&gt;, &lt;span class="str"&gt;'Pluralcast'&lt;/span&gt;]

&lt;span class="kwrd"&gt;for&lt;/span&gt; podcast &lt;span class="kwrd"&gt;in&lt;/span&gt; favoritePodcasts
    download podcast&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The &lt;em&gt;for .. in&lt;/em&gt; syntax in CoffeeScript is used for iterating over an array while the next example show how a &lt;em&gt;for .. of&lt;/em&gt; loop is used for iterating over the properties of an object.&lt;/p&gt;&lt;pre style="width: 100%; height: 150px" class="csharpcode"&gt;podcasts = 
    &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;: &lt;span class="str"&gt;'http://www.astronomycast.com/'&lt;/span&gt;,
    &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;: &lt;span class="str"&gt;'http://www.dancarlin.com/disp.php/hh'&lt;/span&gt;,
    &lt;span class="str"&gt;'Talking Shop Down Under'&lt;/span&gt;: &lt;span class="str"&gt;'http://www.talkingshopdownunder.com/'&lt;/span&gt;,
    &lt;span class="str"&gt;'Pluralcast'&lt;/span&gt;: &lt;span class="str"&gt;'http://www.pluralsight-training.net/microsoft/pluralcast/default.aspx'&lt;/span&gt;

&lt;span class="kwrd"&gt;for&lt;/span&gt; key, value of podcasts
    console.log key + &lt;span class="str"&gt;' - '&lt;/span&gt; + value    &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;A &lt;em&gt;for .. in&lt;/em&gt; loop translates to a traditional &lt;em&gt;for&lt;/em&gt; loop in JavaScript while the &lt;em&gt;for .. of&lt;/em&gt; loop translates to a &lt;em&gt;for .. in&lt;/em&gt; loop in JavaScript.&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;pre style="width: 100%; height: 184px" class="csharpcode"&gt;&lt;span class="rem"&gt;// Iterating over an array in JavaScript&lt;/span&gt;
&lt;span class="kwrd"&gt;for&lt;/span&gt; (_i = 0, _len = favoritePodcasts.length; _i &amp;lt; _len; _i++) {
    ...
}

&lt;span class="rem"&gt;// Iterating over the properties of an object in JavaScript&lt;/span&gt;
&lt;span class="kwrd"&gt;for&lt;/span&gt; (key &lt;span class="kwrd"&gt;in&lt;/span&gt; podcasts) {
    value = podcasts[key];
    ...
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;In the example where we iterate over an array, the &lt;em&gt;for .. in&lt;/em&gt; loop can be more compact using the postfix notation.&amp;nbsp; &lt;/p&gt;&lt;pre style="width: 100%; height: 20px" class="csharpcode"&gt;download podcast &lt;span class="kwrd"&gt;for&lt;/span&gt; podcast &lt;span class="kwrd"&gt;in&lt;/span&gt; favoritePodcasts&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
How nice is that! Let’s look at some more sweet syntax that CoffeeScript brings us. We can use the &lt;em&gt;‘when’&lt;/em&gt; keyword for filtering.&lt;/p&gt;&lt;pre style="width: 100%; height: 34px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;for&lt;/span&gt; number &lt;span class="kwrd"&gt;in&lt;/span&gt; [1..10] when number &amp;gt; 5
    console.log number        # Outputs &lt;span class="str"&gt;'6, 7, 8, 9, 10'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Filters are supported by both &lt;em&gt;for .. in&lt;/em&gt; and &lt;em&gt;for .. of&lt;/em&gt; loops. We can also obtain the current index when looping over an array.&lt;/p&gt;&lt;pre style="width: 100%; height: 69px" class="csharpcode"&gt;favoritePodcasts = [&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;, 
                    &lt;span class="str"&gt;'Talking Shop Down Under'&lt;/span&gt;, &lt;span class="str"&gt;'Pluralcast'&lt;/span&gt;]
&lt;span class="kwrd"&gt;for&lt;/span&gt; podcast, i &lt;span class="kwrd"&gt;in&lt;/span&gt; favoritePodcasts
    console.log (i+1) + &lt;span class="str"&gt;'/ '&lt;/span&gt; + podcast&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;This is only possible when using &lt;em&gt;for .. in&lt;/em&gt; loops. We can also use the &lt;em&gt;'by'&lt;/em&gt; keyword to loop over an array with fixed-size steps. &lt;/p&gt;&lt;pre style="width: 100%; height: 36px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;for&lt;/span&gt; number &lt;span class="kwrd"&gt;in&lt;/span&gt; [0..100] by 10
    console.log number        # Outputs &lt;span class="str"&gt;'10, 20, 30, ..., 100'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Again, this is only possible when using &lt;em&gt;for .. in&lt;/em&gt; loops. Besides all these nice syntax additions for &lt;em&gt;for … in&lt;/em&gt; loops, there is also a quite helpful keyword that’s only available when using &lt;em&gt;for .. of&lt;/em&gt; loops. We can use the &lt;em&gt;'own'&lt;/em&gt; keyword for iterating over properties that are only defined on the object itself and not on any prototype(s).&lt;/p&gt;&lt;pre style="width: 100%; height: 188px" class="csharpcode"&gt;Podcast = -&amp;gt;
    &lt;span class="preproc"&gt;@name&lt;/span&gt; = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    &lt;span class="preproc"&gt;@url&lt;/span&gt; = &lt;span class="str"&gt;'http://www.astronomycast.com/'&lt;/span&gt;

Podcast::download = -&amp;gt;
    console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt; + &lt;span class="str"&gt;' from '&lt;/span&gt; + &lt;span class="preproc"&gt;@url&lt;/span&gt;

podcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Podcast()
&lt;span class="kwrd"&gt;for&lt;/span&gt; own key, value of podcast
    console.log key + &lt;span class="str"&gt;' : '&lt;/span&gt; + value&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Note that we use the :: operator as a shorthand for denoting the prototype property. In this example we used the &lt;em&gt;‘own’&lt;/em&gt; keyword to skip over any properties defined on the prototype objects, which is simply some syntactic sugar for a &lt;em&gt;hasOwnProperty&lt;/em&gt; check in JavaScript.&amp;nbsp; &lt;/p&gt;&lt;pre style="width: 100%; height: 357px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; Podcast, key, podcast, value;
&lt;span class="kwrd"&gt;var&lt;/span&gt; __hasProp = Object.prototype.hasOwnProperty;

Podcast = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.url = &lt;span class="str"&gt;'http://www.astronomycast.com/'&lt;/span&gt;;
};

Podcast.prototype.download = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name + &lt;span class="str"&gt;' from '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.url);
};

podcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Podcast();
&lt;span class="kwrd"&gt;for&lt;/span&gt; (key &lt;span class="kwrd"&gt;in&lt;/span&gt; podcast) {
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!__hasProp.call(podcast, key)) &lt;span class="kwrd"&gt;continue&lt;/span&gt;;
    
    value = podcast[key];
    console.log(key + &lt;span class="str"&gt;' : '&lt;/span&gt; + value);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Let’s take this a bit further by diving into comprehensions.&lt;/p&gt;
&lt;h4&gt;Comprehenions&lt;/h4&gt;
&lt;p align="justify"&gt;Everything is an expression in CoffeeScript, which means that this also applies to loops. What better way to demonstrate this than a simple code example? Suppose we want to create an array with even numbers by looping over another array of numbers. This is how it can be solved using &lt;em&gt;the CoffeeScript way&lt;/em&gt;.&lt;/p&gt;&lt;pre style="width: 100%; height: 38px" class="csharpcode"&gt;evenNumbers = (number &lt;span class="kwrd"&gt;for&lt;/span&gt; number &lt;span class="kwrd"&gt;in&lt;/span&gt; [1..10] when number % 2 &lt;span class="kwrd"&gt;is&lt;/span&gt; 0)
console.log evenNumbers        # Outputs [ 2, 4, 6, 8, 10 ]&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;A &lt;em&gt;for&lt;/em&gt; loop in CoffeeScript always returns an array with the result of every iteration which in this case are the even numbers from the first array. Note that we need to enclose this comprehension with parentheses because otherwise we only get the last even number instead of an array with all the even numbers. &lt;/p&gt;
&lt;p align="justify"&gt;If you’re not yet impressed by this language feature, then checkout the following one-line &lt;a href="http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html" target="_blank"&gt;FizzBuzz&lt;/a&gt; implementation.&lt;/p&gt;&lt;pre style="width: 100%; height: 35px" class="csharpcode"&gt;fizzbuzz = ([&lt;span class="str"&gt;'fizz'&lt;/span&gt; unless i%3] + [&lt;span class="str"&gt;'buzz'&lt;/span&gt; unless i%5] or i &lt;span class="kwrd"&gt;for&lt;/span&gt; i &lt;span class="kwrd"&gt;in&lt;/span&gt; [1..100])
console.log fizzbuzz&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The implementation comes from &lt;a href="http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html" target="_blank"&gt;this blog post&lt;/a&gt; of &lt;a href="http://ricardo.cc/" target="_blank"&gt;Ricardo Tomasi&lt;/a&gt; which is highly recommended. &lt;/p&gt;
&lt;h4 align="justify"&gt;While&lt;/h4&gt;
&lt;p&gt;To make things complete, CoffeeScript also provides a while loop.&lt;/p&gt;&lt;pre style="width: 100%; height: 93px" class="csharpcode"&gt;count = 0
tenfolds = &lt;span class="kwrd"&gt;while&lt;/span&gt; (count += 1) &amp;lt;= 10
    count * 10
    
console.log tenfolds    &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The while loop in CoffeeScript behaves the same as the one in JavaScript except that it also returns an array of values.&lt;/p&gt;
&lt;p align="justify"&gt;That’s it for now. Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-3907115817986185221?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/5E24sFRkaqE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/5E24sFRkaqE/exploring-coffeescript-part-5-ranges.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/08/exploring-coffeescript-part-5-ranges.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6443902520417213414</guid><pubDate>Tue, 26 Jul 2011 10:00:00 +0000</pubDate><atom:updated>2011-07-28T18:29:21.484+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 4 - Objects and Classes</title><description>&lt;p align="justify"&gt;For this blog post, we’re going to continue our journey through the wonderful world of &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt;, exploring objects and classes.&lt;/p&gt; &lt;p align="justify"&gt;Also make sure to check out the previous installments:&lt;/p&gt; &lt;ol&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html" target="_blank"&gt;And Then There Was Coffee&lt;/a&gt;&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-2-variables.html" target="_blank"&gt;Variables and Functions&lt;/a&gt;&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html" target="_blank"&gt;More on Functions&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h4&gt;Objects&lt;/h4&gt; &lt;p align="justify"&gt;Most of us have know how the object literal notation looks like using plain old JavaScript. Here’s a simple example: &lt;/p&gt;&lt;pre style="width: 100%; height: 241px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; podcast = {
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;,
    description: &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;,
    details: {
        homepage: &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;,
        rss: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/'&lt;/span&gt;,
        atom: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/atom/'&lt;/span&gt;    
    },
    
    toString: &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
       &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;'Title: '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.title;
    }
};&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Lets have a look at how define the exact same object using CoffeeScript.&lt;/p&gt;&lt;pre style="width: 100%; height: 182px" class="csharpcode"&gt;podcast =
    title: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    description: &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;
    details:
        homepage: &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;
        rss: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/'&lt;/span&gt;
        atom: &lt;span class="str"&gt;'http://www.astronomycast.com/feed/atom/'&lt;/span&gt;
    
    toString: -&amp;gt;
       &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Title: "&lt;/span&gt; + @title&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;As you can see, the CoffeeScript equivalent involves less overhead which naturally results in a more compact syntax. For object literals we don't have to provide curly braces and commas, taking full advantage of significant whitespace.&lt;/p&gt;
&lt;p align="justify"&gt;In this basic example, we directly assign values for the different properties which is not very common. Suppose that we want to create &lt;em&gt;podcast&lt;/em&gt; objects by calling a factory method specifying the necessary parameter values:&lt;/p&gt;&lt;pre style="width: 100%; height: 392px" class="csharpcode"&gt;createPodcast = (title, description, homepage, rss, atom) -&amp;gt;
    title: title
    description: description
    details: 
        homepage: homepage
        rss: rss
        atom: atom    
        
    toString: -&amp;gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Title: "&lt;/span&gt; + @title    

title = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
description = &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;
homepage = &lt;span class="str"&gt;'http://www.astronomycast.com'&lt;/span&gt;
rss = &lt;span class="str"&gt;'http://www.astronomycast.com/feed/'&lt;/span&gt;
atom = &lt;span class="str"&gt;'http://www.astronomycast.com/feed/atom/'&lt;/span&gt;

podcast = createPodcast title, description, homepage, rss, atom

console.log podcast
console.log podcast.toString()&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Again, CoffeeScript can save us some typing here as we don’t need to provide both the property name and the corresponding variable name if they are both the same. In this case we can rely on conventions to create a new &lt;em&gt;podcast&lt;/em&gt; object.&lt;/p&gt;&lt;pre style="width: 100%; height: 222px" class="csharpcode"&gt;createPodcast = (title, description, homepage, rss, atom) -&amp;gt;
    {   
        title
        description
        details: {
            homepage
            rss
            atom
        } 
        
        toString: -&amp;gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Title: "&lt;/span&gt; + @title    
    }&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Note that we need to specify curly braces for both the resulting &lt;em&gt;podcast&lt;/em&gt; object as for the nested &lt;em&gt;details&lt;/em&gt; object because otherwise we’ll get some parser errors. Either way, CoffeeScript gets the most out of every keystroke :-).&lt;/p&gt;
&lt;h4 align="justify"&gt;Classes&lt;/h4&gt;
&lt;p align="justify"&gt;JavaScript is not a ‘classical’ language but a &lt;a href="http://vanryswyckjan.blogspot.com/2010/12/basic-javascript-part-3-prototypes.html" target="_blank"&gt;prototypal object language&lt;/a&gt;. There are several JavaScript libraries out there that have made an attempt to bring some form of class like syntax into the language. It’s my personal belief that by fully embracing the prototypical nature of JavaScript that we can gain the true benefits of this great language. However I do recognize that a class like syntax can save us from the syntactical and mental burden that is often seen with prototypical inheritance in JavaScript. Again, CoffeeScript is here to save the day. &lt;/p&gt;
&lt;p align="justify"&gt;By providing a class-style syntax, CoffeeScript effectively provides us with a useful abstraction on top of JavaScript’s prototype capabilities, also applying all the well-known best practices in the process. This enables us to write code that is much more concise and therefore also more clear while still taking advantage of the benefits of the JavaScript object model.&lt;/p&gt;
&lt;p align="justify"&gt;Let’s talk code, shall we?&lt;/p&gt;&lt;pre style="width: 100%; height: 657px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Medium
    constructor: (@name) -&amp;gt;
    
    # Prototype method
    download: (episode) -&amp;gt;
        console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt;
    
    # Static method
    @play: (episode, name) -&amp;gt;
        console.log &lt;span class="str"&gt;'Playing '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name         
        
    playOn: -&amp;gt; 
        console.log &lt;span class="str"&gt;'unknown'&lt;/span&gt;    
                
        
&lt;span class="kwrd"&gt;class&lt;/span&gt; Podcast extends Medium
    constructor: (name, @description) -&amp;gt;
        super name
                
    listen: -&amp;gt; 
        console.log &lt;span class="str"&gt;'Listening to '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt;
    
    playOn: -&amp;gt;
        console.log &lt;span class="str"&gt;'iPod'&lt;/span&gt;
        
        
&lt;span class="kwrd"&gt;class&lt;/span&gt; Screencast extends Medium
    constructor: (name, @description, @author) -&amp;gt;
        super name
        
    watch: -&amp;gt;
        console.log &lt;span class="str"&gt;'Watching '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt;
        
    playOn: -&amp;gt;
        console.log &lt;span class="str"&gt;'iPad'&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;
Here we have three classes. Both the &lt;em&gt;Podcast&lt;/em&gt; and the &lt;em&gt;Screencast&lt;/em&gt; class derive from the &lt;em&gt;Medium&lt;/em&gt; class using the &lt;em&gt;extends&lt;/em&gt; keyword. First have a look at the constructor functions. CoffeeScript lets us define dedicated functions that will be executed upon instantiation. Notice the shorthand syntax for property initialization. Instead of &lt;em&gt;constructor: (name) –&amp;gt; @name = name&lt;/em&gt; we can simply use the equivalent &lt;em&gt;constructor: (@name)&lt;/em&gt;. This shorthand will also work for normal functions outside of classes. Remember, every keystroke counts! The constructor functions of the derived classes invoke the constructor of the base class by simply calling &lt;em&gt;super, &lt;/em&gt;which translates into a call to an ancestor method of the same name. &lt;/p&gt;
&lt;p align="justify"&gt;Instance methods and properties can be defined exactly the same way as we saw earlier when we discussed objects. Obviously, these can only be used after we create an instance of a class.&lt;/p&gt;&lt;pre style="width: 100%; height: 131px" class="csharpcode"&gt;podcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Podcast(&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;)
podcast.download &lt;span class="str"&gt;'the first episode'&lt;/span&gt;
podcast.listen()

screencast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Screencast(&lt;span class="str"&gt;'RailsCasts'&lt;/span&gt;, &lt;span class="str"&gt;'Ruby on Rails Screencasts'&lt;/span&gt;, &lt;span class="str"&gt;'Ryan Bates'&lt;/span&gt;)
screencast.download &lt;span class="str"&gt;'the 267th episode'&lt;/span&gt;
screencast.watch()&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;But we can also have static methods and properties. These cannot be invoked on instances of a class but only directly on the class object itself.&lt;/p&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;
&lt;pre style="width: 100%; height: 207px" class="csharpcode"&gt;# Static method
@play: (episode, name) -&amp;gt;
    console.log &lt;span class="str"&gt;'Playing '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name

...

# Object #&amp;lt;Podcast&amp;gt; has no method play
podcast.play &lt;span class="str"&gt;'the first episode'&lt;/span&gt;

Medium.play(&lt;span class="str"&gt;'the third episode'&lt;/span&gt;, &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;)
Podcast.play(&lt;span class="str"&gt;'the fourth episode'&lt;/span&gt;, &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;)&lt;/pre&gt;
&lt;p align="justify"&gt;We’ve already used the shorthand syntax here. We could also use the following notation for defining this static method:&lt;/p&gt;&lt;pre style="width: 100%; height: 54px" class="csharpcode"&gt;# Static method
&lt;span class="kwrd"&gt;this&lt;/span&gt;.play: (episode, name) -&amp;gt;
    console.log &lt;span class="str"&gt;'Playing '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Remember from &lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html" target="_blank"&gt;the previous post&lt;/a&gt; that CoffeeScript uses the &lt;a href="mailto:&amp;lsquo;@&amp;rsquo;"&gt;‘@’&lt;/a&gt; symbol to alias &lt;em&gt;this&lt;/em&gt;. Every keystroke counts!&lt;/p&gt;
&lt;p align="justify"&gt;One of the key tenets of object-oriented programming using a classical language is polymorphism. Don’t worry, CoffeeScript got this covered as well.&lt;/p&gt;&lt;pre style="width: 100%; height: 84px" class="csharpcode"&gt;play = (medium) -&amp;gt;
    medium.playOn()

play(podcast);       # Outputs &lt;span class="str"&gt;'iPod'&lt;/span&gt;
play(screencast);    # Outputs &lt;span class="str"&gt;'iPad'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Here we have a function that takes in a &lt;em&gt;Medium&lt;/em&gt; object and simply invokes the &lt;em&gt;playOn()&lt;/em&gt; function. The &lt;em&gt;playOn()&lt;/em&gt; functions of the derived classes will be invoked and not the one defined in the base class.&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;In &lt;a href="http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html" target="_blank"&gt;the previous post&lt;/a&gt; we mentioned that we can reuse methods from other objects using the &lt;em&gt;call()&lt;/em&gt;/&lt;em&gt;apply()&lt;/em&gt; methods defined on the prototype of &lt;em&gt;Function&lt;/em&gt;. This can also be done when we’re using classes in CoffeeScript. &lt;/p&gt;&lt;pre style="width: 100%; height: 110px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; PodcastStub
    constructor: -&amp;gt;
        &lt;span class="preproc"&gt;@name&lt;/span&gt; = &lt;span class="str"&gt;'some funcky stub podcast'&lt;/span&gt;
        
podcastStub = &lt;span class="kwrd"&gt;new&lt;/span&gt; PodcastStub()
podcast.listen.apply podcastStub    # Outputs &lt;span class="str"&gt;'Listening to some funcky stub podcast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;We can use the hash rocket operator (or fat arrow) to prevent this behavior if we want. By using =&amp;gt; instead of –&amp;gt; we can define a function to the current value of &lt;em&gt;this&lt;/em&gt;.&lt;/p&gt;&lt;pre style="width: 100%; height: 244px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; ContextSafePodcast extends Medium
    constructor: (name, @description) -&amp;gt;
        super name
                
    listen: =&amp;gt; 
        console.log &lt;span class="str"&gt;'Listening to '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt;
    
    playOn: -&amp;gt;
        console.log &lt;span class="str"&gt;'iPod'&lt;/span&gt;

contextSafePodcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; ContextSafePodcast(&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;,
                                            &lt;span class="str"&gt;'A fact-based journey through the galaxy.'&lt;/span&gt;)
contextSafePodcast.listen.apply podcastStub    # Outputs &lt;span class="str"&gt;'Listening to Astronomy Cast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;I’ll round of this post by showing you the equivalent JavaScript code for the small class hierarchy shown in our example. &lt;/p&gt;&lt;pre style="width: 100%; height: 1064px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; Medium, Podcast, Screencast;
&lt;span class="kwrd"&gt;var&lt;/span&gt; __hasProp = Object.prototype.hasOwnProperty, __extends = &lt;span class="kwrd"&gt;function&lt;/span&gt;(child, parent) {
&lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; key &lt;span class="kwrd"&gt;in&lt;/span&gt; parent) { &lt;span class="kwrd"&gt;if&lt;/span&gt; (__hasProp.call(parent, key)) child[key] = parent[key]; }
&lt;span class="kwrd"&gt;function&lt;/span&gt; ctor() { &lt;span class="kwrd"&gt;this&lt;/span&gt;.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = &lt;span class="kwrd"&gt;new&lt;/span&gt; ctor;
child.__super__ = parent.prototype;
&lt;span class="kwrd"&gt;return&lt;/span&gt; child;
};

Medium = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
&lt;span class="kwrd"&gt;function&lt;/span&gt; Medium(name) {
  &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = name;
}
Medium.prototype.download = &lt;span class="kwrd"&gt;function&lt;/span&gt;(episode) {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
};
Medium.play = &lt;span class="kwrd"&gt;function&lt;/span&gt;(episode, name) {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Playing '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name);
};
Medium.prototype.playOn = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'unknown'&lt;/span&gt;);
};
&lt;span class="kwrd"&gt;return&lt;/span&gt; Medium;
})();

Podcast = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
__extends(Podcast, Medium);
&lt;span class="kwrd"&gt;function&lt;/span&gt; Podcast(name, description) {
  &lt;span class="kwrd"&gt;this&lt;/span&gt;.description = description;
  Podcast.__super__.constructor.call(&lt;span class="kwrd"&gt;this&lt;/span&gt;, name);
}
Podcast.prototype.listen = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Listening to '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
};
Podcast.prototype.playOn = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'iPod'&lt;/span&gt;);
};
&lt;span class="kwrd"&gt;return&lt;/span&gt; Podcast;
})();

Screencast = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
__extends(Screencast, Medium);
&lt;span class="kwrd"&gt;function&lt;/span&gt; Screencast(name, description, author) {
  &lt;span class="kwrd"&gt;this&lt;/span&gt;.description = description;
  &lt;span class="kwrd"&gt;this&lt;/span&gt;.author = author;
  Screencast.__super__.constructor.call(&lt;span class="kwrd"&gt;this&lt;/span&gt;, name);
}
Screencast.prototype.watch = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Watching '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
};
Screencast.prototype.playOn = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'iPad'&lt;/span&gt;);
};
&lt;span class="kwrd"&gt;return&lt;/span&gt; Screencast;
})();&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Did I mention that every keystroke counts? ;-)&lt;/p&gt;Until next time. 
  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-6443902520417213414?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/fP8ksuUrTs4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/fP8ksuUrTs4/exploring-coffeescript-part-4-objects.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-4-objects.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8666333624089374108</guid><pubDate>Wed, 13 Jul 2011 10:00:00 +0000</pubDate><atom:updated>2011-07-13T12:00:14.413+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 3 - More on Functions</title><description>&lt;p align="justify"&gt;For this blog post we’re going to continue where we &lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-2-variables.html" target="_blank"&gt;previously left off&lt;/a&gt;, talking a bit more about functions in &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt;. Let’s get things started by talking a bit about function scope.&lt;/p&gt; &lt;h4 align="justify"&gt;Function scope&lt;/h4&gt; &lt;p align="justify"&gt;Like Ruby, CoffeeScript uses lexical scope. This means that a variable that is declared inside a function is invisible to outside code. From there on, a variable lives inside the scope in which a value is assigned to it without being able to be redeclared. Because we don't have access to the &lt;em&gt;var&lt;/em&gt; keyword, we can't simply declare a variable and use it in a different scope without assigning a value to it. &lt;/p&gt; &lt;p align="justify"&gt;Consider the following code:&lt;/p&gt;&lt;pre style="width: 100%; height: 103px" class="csharpcode"&gt;someFunction = -&amp;gt; podcast = &lt;span class="str"&gt;'Hardcore history'&lt;/span&gt;

podcast = &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;
someFunction()

console.log podcast        # This outputs &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
The output might come as a surprise to you, but it actually makes a lot of sense. A variable that is declared inside an inner scope cannot shadow a variable with the same name that is declared in an outer scope. I highly recommend that you avoid reusing variable names as shown by this example. &lt;/p&gt;
&lt;h4 align="justify"&gt;Function context&lt;/h4&gt;
&lt;p align="justify"&gt;In JavaScript, the &lt;em&gt;this&lt;/em&gt; keyword inside a function does not necessarily point to the object for which the function is declared. The object that is referenced by &lt;em&gt;this&lt;/em&gt; depends on how the function is called. The same applies to CoffeeScript as well, only it uses the '@' symbol for denoting the &lt;em&gt;this&lt;/em&gt; keyword in JavaScript. Let’s look at a couple of scenarios.&lt;/p&gt;&lt;pre style="width: 100%; height: 105px" class="csharpcode"&gt;download = (podcast) -&amp;gt; 
    &lt;span class="preproc"&gt;@podcast&lt;/span&gt; = podcast
    console.log &lt;span class="preproc"&gt;@podcast&lt;/span&gt;    # This outputs &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    console.log &lt;span class="preproc"&gt;@&lt;/span&gt;           # This outputs all properties of the global &lt;span class="kwrd"&gt;object&lt;/span&gt;
    
download &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Compiling this down to JavaScript results in the following code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; download;
download = &lt;span class="kwrd"&gt;function&lt;/span&gt;(podcast) {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.podcast = podcast;
    console.log(&lt;span class="kwrd"&gt;this&lt;/span&gt;.podcast);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
};

download(&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;);&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
As you can see, &lt;em&gt;@podcast&lt;/em&gt; is the shorthand notation for&lt;em&gt; this.podcast&lt;/em&gt; in JavaScript. For this example, &lt;em&gt;this&lt;/em&gt; references the global object. Let’s have a look at another scenario.&lt;/p&gt;&lt;pre style="width: 100%; height: 171px" class="csharpcode"&gt;Podcast = () -&amp;gt;
    &lt;span class="preproc"&gt;@name&lt;/span&gt; = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    &lt;span class="preproc"&gt;@download&lt;/span&gt; = (episode) -&amp;gt;
        console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="preproc"&gt;@&lt;/span&gt;

# Outputs &lt;span class="str"&gt;'Downloading the first episode of Astronomy Cast'&lt;/span&gt;
podcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Podcast
podcast.download &lt;span class="str"&gt;'the first episode'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;This results in the following JavaScript code:&lt;/p&gt;&lt;pre style="width: 100%; height: 222px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; Podcast, podcast;

Podcast = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;;
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.download = &lt;span class="kwrd"&gt;function&lt;/span&gt;(episode) {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
    };
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
};

podcast = &lt;span class="kwrd"&gt;new&lt;/span&gt; Podcast;
podcast.download(&lt;span class="str"&gt;'the first episode'&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;When calling a constructor function using the &lt;em&gt;new&lt;/em&gt; operator, &lt;em&gt;this&lt;/em&gt; references the new object being created. We've also explicitly added 'return @' in order to return the new object. Otherwise the function assigned to the &lt;em&gt;download&lt;/em&gt; property is implicitly returned. Let’s move on to the next case.&lt;/p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre style="width: 100%; height: 188px" class="csharpcode"&gt;podcast = 
    name: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    download: (episode) -&amp;gt;
        console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt; 

otherPodcast = 
    name: &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;

# Outputs &lt;span class="str"&gt;'Downloading the first episode of Hardcore History'&lt;/span&gt;
podcast.download.apply otherPodcast, [&lt;span class="str"&gt;'the first episode'&lt;/span&gt;]        &lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Compiling this to JavaScript results into the following code:&lt;/p&gt;&lt;pre style="width: 100%; height: 242px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; otherPodcast, podcast;
podcast = {
    name: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;,
    download: &lt;span class="kwrd"&gt;function&lt;/span&gt;(episode) {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
    }
};

otherPodcast = {
    name: &lt;span class="str"&gt;'Hardcore History'&lt;/span&gt;
};

podcast.download.apply(otherPodcast, [&lt;span class="str"&gt;'the first episode'&lt;/span&gt;]);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As I already mentioned in a &lt;a href="http://vanryswyckjan.blogspot.com/2011/01/basic-javascript-part-9-reusing-methods.html" target="_blank"&gt;previous blog post&lt;/a&gt;, we can reuse methods from other objects using the &lt;em&gt;call()&lt;/em&gt;/&lt;em&gt;apply()&lt;/em&gt; methods defined on the prototype of &lt;em&gt;Function&lt;/em&gt;. In this example we reuse the &lt;em&gt;download&lt;/em&gt; function in the context of the &lt;em&gt;otherPodcast&lt;/em&gt; object. This means that &lt;em&gt;this&lt;/em&gt; now references &lt;em&gt;otherPodcast &lt;/em&gt;instead of the &lt;em&gt;podcast&lt;/em&gt; object. Let’s dive into the final and most common scenario.&lt;/p&gt;&lt;pre style="width: 100%; height: 118px" class="csharpcode"&gt;podcast = 
    name: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
    download: (episode) -&amp;gt;
        console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="preproc"&gt;@name&lt;/span&gt; 
        
# Outputs &lt;span class="str"&gt;'Downloading the first episode of Astronomy Cast'&lt;/span&gt;
podcast.download(&lt;span class="str"&gt;'the first episode'&lt;/span&gt;)&lt;/pre&gt;
&lt;p&gt;The equivalent JavaScript code looks like this: 
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;&lt;pre style="width: 100%; height: 165px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; podcast;
podcast = {
    name: &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;,
    download: &lt;span class="kwrd"&gt;function&lt;/span&gt;(episode) {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + &lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
    }
};

podcast.download(&lt;span class="str"&gt;'the first episode'&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Here &lt;em&gt;this&lt;/em&gt; simply references the current object to which the executing function belongs to.&lt;/p&gt;
&lt;h4&gt;Function binding&lt;/h4&gt;
&lt;p align="justify"&gt;Going over these different scenarios regarding the function context, it might be useful to be able to define a function that binds to the current value of &lt;em&gt;this&lt;/em&gt;. In CoffeeScript we can use the =&amp;gt; symbol instead of –&amp;gt; to accomplish just that. Let’s look at the code example from the &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript website&lt;/a&gt;.&lt;/p&gt;&lt;pre style="width: 100%; height: 103px" class="csharpcode"&gt;Account = (customer, cart) -&amp;gt;
  &lt;span class="preproc"&gt;@customer&lt;/span&gt; = customer
  &lt;span class="preproc"&gt;@cart&lt;/span&gt; = cart

  $(&lt;span class="str"&gt;'.shopping_cart'&lt;/span&gt;).bind &lt;span class="str"&gt;'click'&lt;/span&gt;, (&lt;span class="kwrd"&gt;event&lt;/span&gt;) =&amp;gt;
    @customer.purchase &lt;span class="preproc"&gt;@cart&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;This is especially helpful for event callbacks. The reason you might want to consider the &lt;em&gt;‘fat arrow’&lt;/em&gt; in this case is that callbacks are executed in the context of the element, i.e. &lt;em&gt;this &lt;/em&gt;references the element. When you want to keep &lt;em&gt;this&lt;/em&gt; equal to the local context, without doing the &lt;em&gt;self = this&lt;/em&gt; dance, then &lt;em&gt;‘fat arrows’&lt;/em&gt; are the way to go.&lt;/p&gt;
&lt;h4&gt;Splats&lt;/h4&gt;
&lt;p align="justify"&gt;Splats enable us to easily take a variable number of parameters in a function. JavaScript has a similar construct with the &lt;em&gt;arguments&lt;/em&gt; object that is available in every function. But the major downside of the &lt;em&gt;arguments&lt;/em&gt; object is that it lacks the semantics of an array which makes it pretty cumbersome to use. With splats, CoffeeScript provides us with a much better way to deal with a varying number of parameters.&amp;nbsp; &lt;/p&gt;&lt;pre style="width: 100%; height: 147px" class="csharpcode"&gt;download = (name, episodes..., epilogue) -&amp;gt;
    console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episodes.join(&lt;span class="str"&gt;', '&lt;/span&gt;) + &lt;span class="str"&gt;' of '&lt;/span&gt; + name;
    console.log epilogue

# Outputs &lt;span class="str"&gt;'Downloading the first, the second, and third episode of Astronomy Cast'&lt;/span&gt;
#          &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;
download(&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'the first'&lt;/span&gt;, &lt;span class="str"&gt;'the second'&lt;/span&gt;, &lt;span class="str"&gt;'and third episode'&lt;/span&gt;, 
         &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;)&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
This boils down to the following (nasty) JavaScript code:&lt;/p&gt;&lt;pre style="width: 100%; height: 278px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; download;
&lt;span class="kwrd"&gt;var&lt;/span&gt; __slice = Array.prototype.slice;

download = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; epilogue, episodes, name, _i;
    name = arguments[0], 
    episodes = 3 &amp;lt;= arguments.length ? 
        __slice.call(arguments, 1, _i = arguments.length - 1) 
      : (_i = 1, []), epilogue = arguments[_i++];
    
    console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episodes.join(&lt;span class="str"&gt;', '&lt;/span&gt;) + &lt;span class="str"&gt;' of '&lt;/span&gt; + name);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(epilogue);
};
download(&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'the first'&lt;/span&gt;, &lt;span class="str"&gt;'the second'&lt;/span&gt;, &lt;span class="str"&gt;'and third episode'&lt;/span&gt;, 
         &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As you can see, splats don't have to be defined at the end of the parameter list. For this example, the first and last value get to be assigned to the regular parameters after which the remaining values in the middle of the list get assigned to the splat parameter. When we only specify two values, then these will be used for the regular parameters while the splat parameter will be null. &lt;/p&gt;
&lt;p align="justify"&gt;The values for the regular parameters get to be assigned first after which the remaining values all get to be passed to the splat parameter. This also implies that there can be only a single splat parameter for a given function definition.&lt;/p&gt;
&lt;p align="justify"&gt;Splats are not only available for function definitions, but also for calling functions as well.&amp;nbsp; &lt;/p&gt;&lt;pre style="width: 100%; height: 150px" class="csharpcode"&gt;download = (name, episode, epilogue) -&amp;gt;
    console.log &lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name;
    console.log epilogue

# Outputs &lt;span class="str"&gt;'Downloading the first episode of Astronomy Cast'&lt;/span&gt;
#          &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;
arguments = [&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'the first episode'&lt;/span&gt;, &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;]
download(arguments...)&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This simply boils down to calling the apply() method of the download function in the resulting JavaScript code:&lt;/p&gt;&lt;pre style="width: 100%; height: 145px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; download;
download = &lt;span class="kwrd"&gt;function&lt;/span&gt;(name, episode, epilogue) {
    console.log(&lt;span class="str"&gt;'Downloading '&lt;/span&gt; + episode + &lt;span class="str"&gt;' of '&lt;/span&gt; + name);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(epilogue);
};

arguments = [&lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;, &lt;span class="str"&gt;'the first episode'&lt;/span&gt;, &lt;span class="str"&gt;'What a hoot!'&lt;/span&gt;];
download.apply(&lt;span class="kwrd"&gt;null&lt;/span&gt;, arguments);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;No doubt that CoffeeScript brings a lot of goodness to the table when it comes to functions. But there’s also a lot more. Stay tuned for the next couple of blog posts. 
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-8666333624089374108?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/Xiqd_HfHIh0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/Xiqd_HfHIh0/exploring-coffeescript-part-3-more-on.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/07/exploring-coffeescript-part-3-more-on.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6705546016919464168</guid><pubDate>Tue, 05 Jul 2011 10:00:00 +0000</pubDate><atom:updated>2011-07-05T12:00:10.213+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – Don’t Block The Event Loop</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js" target="_blank"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;The basic premise of &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt; is that all I/O operations are expensive. That is why all I/O should be carried out asynchronously, at least according to the philosophy behind Node.js. What this basically boils down to is that we need to specify a callback function or bind to one or more events in order to get the outcome for a particular I/O related function that we want to execute. This means that things like file access, database operations, communication over HTTP, TCP or UDP, etc. … don’t block the main execution of a Node.js application.  &lt;p align="justify"&gt;One of the components that lies at the heart of Node.js is &lt;a href="http://software.schmorp.de/pkg/libev.html" target="_blank"&gt;the event loop&lt;/a&gt;. This is basically a component that processes a queue of events and invokes an associated callback for each of these events. &lt;a href="http://tomhughescroucher.com/" target="_blank"&gt;Tom Hughes-Croucher&lt;/a&gt; provides a very nice explanation in &lt;a href="http://developer.yahoo.com/blogs/ydn/posts/2010/10/understanding-the-event-loops-and-writing-great-code-for-node-js-part-1/" target="_blank"&gt;this article&lt;/a&gt; where he provides an analogy between the event loop in Node.js and a mail man:  &lt;blockquote&gt; &lt;p&gt;&lt;i&gt;To our event-loop postman, each letter is an event. He has a stack of events to deliver in order. For each letter (event) the postman gets, he walks to the route to deliver the letter. The route is the callback function assigned to that event (sometimes more than one). However, critically, since our mailman only has a single set of legs, he can only walk a single code path at once.&lt;/i&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;&amp;nbsp; &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-woXDWV9EU5Q/Tg4XIQRhnRI/AAAAAAAABmM/6m_eoaokEXU/s1600-h/image%25255B7%25255D.png"&gt;&lt;img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://lh5.ggpht.com/-yD1BpVq0j6I/Tg4XI_CjqFI/AAAAAAAABmQ/MnGWjoqjjKk/image_thumb%25255B5%25255D.png?imgmax=800" width="234" height="309"&gt;&lt;/a&gt; &lt;p align="justify"&gt;Since the event loop is running on a single thread, it is very important that we do not block it’s execution by doing heavy computations in callback functions or synchronous I/O. Going over a large collection of values/objects or performing time-consuming computations in a callback function prevents the event loop from further processing other events in the queue.  &lt;p align="justify"&gt;Doing synchronous I/O is also a bad thing for the same obvious reason that it blocks the event loop. There are a couple of synchronous I/O functions available in the Node.js built-in API. A few of these are exported by the &lt;a href="http://nodejs.org/docs/v0.4.9/api/fs.html" target="_blank"&gt;file system (fs) module&lt;/a&gt; (the ones that end with …&lt;i&gt;Sync&lt;/i&gt;). I strongly advise you to stay away from these functions.  &lt;p align="justify"&gt;There’s one synchronous, pseudo-global function that you probably have to use in any Node.js application besides the typical “hello world” example, and that is the &lt;i&gt;require &lt;/i&gt;function. By executing the &lt;i&gt;require&lt;/i&gt; function we can load another module into the process of our application. This means that the content of the corresponding JavaScript file for a requested module is read from disk. By caching this JavaScript content, Node.js ensures that such an expensive synchronous read doesn’t happen more than once when the &lt;i&gt;require&lt;/i&gt; function is issued multiple times for the same module. Because we typically make a call to the &lt;i&gt;require&lt;/i&gt; function at the beginning of a module, executing this synchronous function only affects the startup time of our application. Make sure that you do not call the &lt;i&gt;require&lt;/i&gt; function inside a callback. In this scenario, the event loop is blocked until the requested JavaScript file is loaded from disk (unless the requested module is already in the cache, but don’t count on it).  &lt;p align="justify"&gt;The event loop that is baked into Node.js is a wonderful thing when building real-time applications. But it can turn into a nightmare when holding on to the paradigms of synchronous I/O. Node.js stands for asynchronous I/O and the event loop is the pumping heart that makes it happen.  &lt;p&gt;Until next time. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-6705546016919464168?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/G521Y2TfB24" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/G521Y2TfB24/taking-baby-steps-with-nodejs-dont.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-yD1BpVq0j6I/Tg4XI_CjqFI/AAAAAAAABmQ/MnGWjoqjjKk/s72-c/image_thumb%25255B5%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/07/taking-baby-steps-with-nodejs-dont.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-749443348904285495</guid><pubDate>Thu, 30 Jun 2011 10:00:00 +0000</pubDate><atom:updated>2011-06-30T12:00:06.984+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 2 - Variables and Functions</title><description>&lt;p align="justify"&gt;As I mentioned in &lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html" target="_blank"&gt;the previous blog post&lt;/a&gt;, &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt; is a neat little language that compiles down to JavaScript code. Its syntax is heavily inspired by Ruby and next to bringing a lot of nice language features to the table, it also requires less amount of code than writing the equivalent directly in JavaScript. For this blog post, we’ll dive into some of this syntactic sugar by discussing variables and functions.&lt;/p&gt; &lt;h3&gt;Variables&lt;/h3&gt; &lt;p&gt;Declaring a variable in CoffeeScript is done like this:&lt;/p&gt;&lt;pre style="width: 100%; height: 20px" class="csharpcode"&gt;podcast = &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;There’s no need to use the &lt;em&gt;var&lt;/em&gt; keyword like in JavaScript. In fact, using the &lt;em&gt;var&lt;/em&gt; keyword results in a compiler error. We also don’t need to provide a semi-colon at the end of the statement. These are added automatically for you during compilation. When we omit semi-colons in JavaScript, the engine also tries to add them at runtime but this behavior can cause &lt;a href="http://vanryswyckjan.blogspot.com/2011/01/basic-javascript-part-6-automatic.html" target="_blank"&gt;a lot of grief&lt;/a&gt;. CoffeeScript correctly adds them for us, and although we no longer need to type in semi-colons, we’re still able to do so if we want to.&lt;/p&gt;
&lt;p align="justify"&gt;The variable declaration from above translates into the following JavaScript:&lt;/p&gt;&lt;pre style="width: 100%; height: 70px" class="csharpcode"&gt;(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  &lt;span class="kwrd"&gt;var&lt;/span&gt; podcast;
  podcast = &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;;
}).call(&lt;span class="kwrd"&gt;this&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;The resulting code here is actually quite interesting. When we want to declare a local variable in JavaScript and we forget to specify the &lt;em&gt;var&lt;/em&gt; keyword, then this variable ends up becoming a global variable instead.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/*var*/&lt;/span&gt; f  = &lt;span class="str"&gt;'fubar'&lt;/span&gt;;
console.log(window.f);    // Outputs &lt;span class="str"&gt;'fubar'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;This can no longer happen just by accident when using CoffeeScript as it wraps all generated JavaScript in a &lt;a href="http://vanryswyckjan.blogspot.com/2010/10/basic-javascript-part-1-functions.html" target="_blank"&gt;self-executing function&lt;/a&gt; and by automatically emitting the var keyword before the variable name. We’re still able to create global variables but the only way is to declare them explicitly.&lt;/p&gt;&lt;pre style="width: 100%; height: 22px" class="csharpcode"&gt;window.f = &lt;span class="str"&gt;'fubar'&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;The use of global variables is strongly discouraged when using JavaScript. As a result, the same goes for CoffeeScript as well.&lt;/p&gt;
&lt;h3 align="justify"&gt;Functions&lt;/h3&gt;
&lt;p align="justify"&gt;The tedious &lt;em&gt;function&lt;/em&gt; keyword in JavaScript is not part of the CoffeeScript syntax. Instead it uses an arrow to define a function.&lt;/p&gt;&lt;pre style="width: 100%; height: 36px" class="csharpcode"&gt;log = (message) -&amp;gt; console.log message
log &lt;span class="str"&gt;'Podcast downloader 2000'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;The syntax for defining a function in CoffeeScript is an (optional) list of parameters, followed by an arrow and then the function body. In this case, the code for the entire function is put on the same line. Multiple lines are also possible off course, but then the code for the function body needs to be properly indented as the CoffeeScript compiler uses significant whitespace to delimit blocks of code. This is not only for functions, but also for conditions, loops, try/catch statements, etc. … . Also notice that we no longer need to specify any parenthesis for the passed arguments when calling our &lt;em&gt;log&lt;/em&gt; function.&lt;/p&gt;
&lt;p align="justify"&gt;The code for this simple example is translated into the following JavaScript code:&lt;/p&gt;&lt;pre style="width: 100%; height: 99px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; log;
log = &lt;span class="kwrd"&gt;function&lt;/span&gt;(message) {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(message);
};

log(&lt;span class="str"&gt;'Podcast downloader 2000'&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Did you notice that the result of the call to &lt;em&gt;console.log&lt;/em&gt; is returned to the caller of our log function? CoffeeScript simply returns the last expression of each function by default unless we explicitly use the &lt;em&gt;return&lt;/em&gt; keyword. &lt;/p&gt;
&lt;p align="justify"&gt;CoffeeScript also provides a nice and useful keyword in case that we have a function without any parameters that we want to invoke directly.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;do&lt;/span&gt; -&amp;gt; console.log &lt;span class="str"&gt;'Podcast downloader 2000'&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;The &lt;em&gt;do&lt;/em&gt; keyword just executes a function directly without having to assign it to a variable and invoke it on the next line. The resulting JavaScript code is a call to console.log wrapped in a self-executing function:&lt;/p&gt;&lt;pre style="width: 100%; height: 51px" class="csharpcode"&gt;(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; console.log(&lt;span class="str"&gt;'Podcast downloader 2000'&lt;/span&gt;);
})();&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;In JavaScript we have both function expressions and function declarations. You can read &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/basic-javascript-part-12-function.html" target="_blank"&gt;this blog post&lt;/a&gt; to learn more about both these concepts. CoffeeScript only supports function expressions.&lt;/p&gt;&lt;pre style="width: 100%; height: 69px" class="csharpcode"&gt;download &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;

download = (podcast) -&amp;gt; 
    console.log podcast&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;The first line of code for this example will give us the following compiler error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p align="justify"&gt;&lt;em&gt;TypeError: undefined is not a function&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;In order to get this code to compile, we need to change this code to the following:&lt;/p&gt;&lt;pre class="csharpcode"&gt;download = (podcast) -&amp;gt; 
    console.log podcast
    
download &lt;span class="str"&gt;'Astronomy cast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;CoffeeScript also supports default parameter values, but this feature is implemented in a different way than what we’ve been used to see to in other programming languages.&lt;/p&gt;&lt;pre style="width: 100%; height: 144px" class="csharpcode"&gt;download = (podcast = &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;) -&amp;gt;
    console.log podcast

download &lt;span class="str"&gt;'Hardcore history'&lt;/span&gt;        # Outputs &lt;span class="str"&gt;'Hardcore history'&lt;/span&gt;
download()                         # Outputs &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
&lt;span class="kwrd"&gt;do&lt;/span&gt; download                        # Another way of calling download, but without ()
download &lt;span class="kwrd"&gt;null&lt;/span&gt;                      # Still outputs &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;
download undefined                 # Still outputs &lt;span class="str"&gt;'Astronomy Cast'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;In other programming languages, a default value for a parameter is used when a value is omitted in the function call. CoffeeScript takes this a bit further and also uses a default value if null or undefined is explicitly specified in the function call. This might look weird at first, but this also implies that the optional parameters do not have to appear last in the parameter list.&lt;/p&gt;&lt;pre style="width: 100%; height: 69px" class="csharpcode"&gt;foo = (arg1 = &lt;span class="str"&gt;'default1'&lt;/span&gt;, arg2 = &lt;span class="str"&gt;'default2'&lt;/span&gt;, arg3 = &lt;span class="str"&gt;'default3'&lt;/span&gt;) -&amp;gt; 
    ...
    
foo &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="str"&gt;'not_default'&lt;/span&gt;, null&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p align="justify"&gt;Fascinating language, isn’t it?&lt;/p&gt;
&lt;p align="justify"&gt;There is much more to be said about functions in CoffeeScript, but I will discuss those features in the next couple of blog posts.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-749443348904285495?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/PIVNTJs4bHU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/PIVNTJs4bHU/exploring-coffeescript-part-2-variables.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-2-variables.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8045977179882097230</guid><pubDate>Tue, 28 Jun 2011 10:00:00 +0000</pubDate><atom:updated>2011-06-28T12:00:02.414+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Book Review: Hands-on Node.js</title><description>&lt;p&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline; float: left" align="left" src="https://lh3.googleusercontent.com/OYprr2lLB-Noh-UI986pGyxEWeh9kM95RZI-ZnzssKg4zApQDAdDGqraIOd9pYL_Yh8pPIxdSZ4rk6UfXWB0TFSHPuWl7zhXYFIhdkjEK6GeWQz6tM0" width="230" height="256"&gt;&lt;/p&gt; &lt;p align="justify"&gt;Last week, I finished reading &lt;a href="http://nodetuts.com/handson-nodejs-book.html" target="_blank"&gt;Hands-on Node.js&lt;/a&gt; written by &lt;a href="http://metaduck.com/" target="_blank"&gt;Pedro Teixeira&lt;/a&gt;. I very much enjoyed reading this concise introduction to Node.js, and although it’s targeted towards beginners, I certainly managed to pick up a few nuggets myself here and there.  &lt;p align="justify"&gt;After the obligatory introduction, the first chapter opens up with an explanation of why Node.js runs on an event loop and why JavaScript eventually got chosen as the preferred language.  &lt;p align="justify"&gt;The next chapter shows how to install Node.js on different platforms and also how to get &lt;a href="http://npmjs.org/" target="_blank"&gt;npm&lt;/a&gt; up and running.  &lt;p align="justify"&gt;Chapter 3 discusses the event loop and modules. These first three chapters make up the first part of the book, which provide you with a more theoretical understanding of what Node.js is all about.  &lt;p align="justify"&gt;The next part of the book contains the chapters that go over the different aspects of the 25 built-in modules that come with Node.js. As the title implies, these chapters contain a lot of code examples and also provide the reader with a good amount of exercises (and also solutions). There are even a chapters that discuss how to debug and unit test Node.js applications.  &lt;p align="justify"&gt;So, if you want to start out learning about Node.js then this book is your first stop. You can &lt;a href="http://nodetuts.com/handson-nodejs-book.html" target="_blank"&gt;buy the ebook directly from the author&lt;/a&gt; for only $ 4.99! Pedro certainly knows his stuff when it comes to Node.js, so this is a real bargain. Also make sure to check out his &lt;a href="http://nodetuts.com/" target="_blank"&gt;excellent screencast series&lt;/a&gt; on this very topic which are free to watch and download.  &lt;p&gt;Happy learning!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-8045977179882097230?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/hwOt4HYkAOE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/hwOt4HYkAOE/book-review-hands-on-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/06/book-review-hands-on-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-480077168527872627</guid><pubDate>Tue, 21 Jun 2011 10:00:00 +0000</pubDate><atom:updated>2011-06-22T22:00:18.690+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><title>Exploring CoffeeScript Part 1 – And Then There Was Coffee</title><description>&lt;p align="justify"&gt;Those of you who have been reading this blog lately know that I’m quite enthusiastic when it comes to JavaScript. As the common behavioral language of the web, JavaScript has been making its way into other aspects of application programming over the last couple of years, notably with server-side frameworks like &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js" target="_blank"&gt;Node.js&lt;/a&gt; and NoSQL databases like &lt;a href="http://vanryswyckjan.blogspot.com/search/label/CouchDB" target="_blank"&gt;CouchDB&lt;/a&gt;. I personally find JavaScript to be an awesome programming language, especially when you stick with &lt;a href="http://elegantcode.com/2010/05/24/book-review-javascript-the-good-parts/" target="_blank"&gt;the good parts&lt;/a&gt;. So I’m more than pleased that JavaScript is finally making its way out of the browser to fulfill other purposes as well.&lt;/p&gt; &lt;p align="justify"&gt;But a couple of months ago I ran into another programming language called &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt;. This neat little language, created by &lt;a href="http://ashkenas.com/" target="_blank"&gt;Jeremy Ashkenas&lt;/a&gt;, simply compiles its syntax into JavaScript code. In fact, there’s no interpretation of CoffeeScript code at runtime. CoffeeScript essentially builds on top of JavaScript as the core language while adding some syntactic niceties that are heavily inspired by Ruby. The CoffeeScript compiler emits very clean JavaScript code that not only follows best practices but also complies to &lt;a href="http://www.jslint.com/" target="_blank"&gt;JSLint&lt;/a&gt; without warnings. This all basically means that wherever you can use JavaScript, whether it’s in the browser, on the server-side or for a particular database, you can use CoffeeScript as well. In fact, you can use any JavaScript library right in your CoffeeScript code!&lt;/p&gt; &lt;p align="justify"&gt;But why should one use CoffeeScript over JavaScript? Well, for starters, the CoffeeScript syntax is much more concise than its JavaScript equivalent. There’s a lot less typing involved for getting the equivalent JavaScript code. Check out &lt;a href="http://jashkenas.github.com/coffee-script/#overview" target="_blank"&gt;this example&lt;/a&gt; on the CoffeeScript wiki.&lt;/p&gt; &lt;p align="justify"&gt;Second, the CoffeeScript language is much richer than JavaScript. It add splats, classes, destructuring assignments and many more small details that makes your code more readable and fun.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;And thirdly, because the creator of &lt;a href="http://rubyonrails.org/" target="_blank"&gt;Ruby on Rails&lt;/a&gt;, &lt;a href="http://www.loudthinking.com/" target="_blank"&gt;David Heinemeier Hansson&lt;/a&gt;, says so :-). The upcoming 3.1 release of Ruby on Rails &lt;a href="http://www.rubyinside.com/rails-3-1-adopts-coffeescript-jquery-sass-and-controversy-4669.html" target="_blank"&gt;will include CoffeeScript in the box&lt;/a&gt;, which means that other web frameworks will probably also add support for CoffeeScript in the future. &lt;/p&gt; &lt;p align="justify"&gt;Enough with the marketing talk already. Let’s have a quick look at how to get CoffeeScript up and running. For starters, you need to have &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt; and &lt;a href="http://npmjs.org/" target="_blank"&gt;npm&lt;/a&gt; installed. Check out the &lt;a href="https://github.com/joyent/node/wiki/Installation" target="_blank"&gt;installation guide&lt;/a&gt; or &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/" target="_blank"&gt;Matthew Podwysocki's&lt;/a&gt; on &lt;a href="http://codebetter.com/matthewpodwysocki/2010/09/08/getting-started-with-node-js-on-windows/" target="_blank"&gt;how to get Node.js up and running on Windows using Cygwin&lt;/a&gt;.&lt;/p&gt; &lt;p align="justify"&gt;When you have node.js all set up, you can simply use the following command to install the CoffeeScript compiler using npm:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;npm install -g coffee-script&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;Now that we have CoffeeScript installed, we can compile .coffee files into .js files or we can use the interactive REPL. Let’s go for the canonical “Hello Pluto” example here and create a file named &lt;em&gt;hello.coffee&lt;/em&gt; with the following code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;console.log &lt;span class="str"&gt;'Hello CoffeeScript'&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;Notice that we don’t have to provide parentheses here in order to call the &lt;em&gt;console.log&lt;/em&gt; function. Next we can use the following command to print the compiled JavaScript directly to our console window:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;coffee -p hello.coffee&lt;/em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;or we can use this next command to compile our &lt;em&gt;hello.coffee&lt;/em&gt; file to a corresponding &lt;em&gt;hello.js&lt;/em&gt; file at the same location in the file system:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;coffee -c hello.coffee&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The compiled JavaScript output looks like this:&lt;/p&gt;&lt;pre class="csharpcode"&gt;(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
  console.log(&lt;span class="str"&gt;'Hello CoffeeScript'&lt;/span&gt;);
}).call(&lt;span class="kwrd"&gt;this&lt;/span&gt;);&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
You might wonder why the call to &lt;em&gt;console.log&lt;/em&gt; is wrapped into a self-executing function. The reason for this is to &lt;a href="http://vanryswyckjan.blogspot.com/2011/01/basic-javascript-part-8-namespaces.html" target="_blank"&gt;prevent naming collisions&lt;/a&gt; when using our code in conjunction with other JavaScript libraries. You can tell the CoffeeScript compiler to omit this self-executing function by specifying the &lt;em&gt;–b, –bare&lt;/em&gt; command-line option.&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;We’re also able to keep the CoffeeScript compiler running with the –w, --watch command-line option. This basically lets the CoffeeScript compiler watch for the specified &lt;em&gt;.coffee&lt;/em&gt; files and recompile them as soon as they’re changed. &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;coffee -cw hello.coffee&lt;/em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;So these are the very basics on how to get up and running with CoffeeScript. Expect to see more of the interesting stuff that this amazing programming language has to offer as I’m going to be writing a couple of more blog posts about CoffeeScript. I certainly recommend that you have a decent knowledge of JavaScript before you pick up CoffeeScript. Being able to understand the JavaScript code that is emitted by the CoffeeScript compiler can be crucial at times.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-480077168527872627?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/p25rfhfQhFo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/p25rfhfQhFo/exploring-coffeescript-part-1-and-then.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1468626379675176982</guid><pubDate>Wed, 15 Jun 2011 21:26:00 +0000</pubDate><atom:updated>2011-06-15T23:26:23.603+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – The E-VAN Presentation</title><description>&lt;p align="justify"&gt;The recording of &lt;a href="http://europevan.blogspot.com/2011/06/recording-of-me-myself-and-i-on-taking.html" target="_blank"&gt;my very first E-VAN talk on Node.js&lt;/a&gt; is now available. If you want to hear me blabbering on about Node.js then I suggest you have a look.&amp;nbsp; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-1468626379675176982?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/TMuDc7hsshY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/TMuDc7hsshY/taking-baby-steps-with-nodejs-e-van.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/06/taking-baby-steps-with-nodejs-e-van.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1290059026642881766</guid><pubDate>Fri, 27 May 2011 23:09:00 +0000</pubDate><atom:updated>2011-05-31T20:31:43.948+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><title>A Quartet of Book Reviews</title><description>&lt;p align="justify"&gt;Since a couple of months or so, the amount of time spent for my daily commute to and from work nearly tripled. I also decided to travel by train instead of using my car (which kind of explains the increase of my travelling time). So, trying to make the best of things, I decided to catch up on my reading backlog. For this blog post, I’m going to briefly discuss the books I’ve read so far. &lt;/p&gt; &lt;h3&gt;1. The 48 Laws of Power&lt;/h3&gt; &lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/9780140280197/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 10px 10px 0px 0px; display: inline; float: left" align="left" src="http://thefuturebuzz.com/pics/power.jpg" width="161" height="227"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;The &lt;a href="http://www.amazon.com/exec/obidos/ASIN/9780140280197/elegantcode-20" target="_blank"&gt;48 Laws of Power&lt;/a&gt;, written by &lt;a href="http://en.wikipedia.org/wiki/Robert_Greene_(author)" target="_blank"&gt;Robert Greene&lt;/a&gt;, totally blew me away. I can’t remember where I picked it up or who brought this book to my attention, but kudos anyway. The basic premise of this book is that some strategies keep you in control, while other acts of human nature decrease your influence. This book isn’t necessarily about you gaining power over others, as it is more about actions that prevent others from manipulating or gaining control over you or your close environment. Every law is illustrated with real-life stories about historic figures and how their actions put them in a powerful position or how their mistakes drove them to a cliff. &lt;/p&gt; &lt;p align="justify"&gt;I wish I had read this book many years ago, but better late than never, right? I’m definitely going to reread several portions of this book in the near future in order to get a better grasp of some these laws. I have to admit that sometimes, this book is a bit much when you read it for the first time. &lt;/p&gt; &lt;h3&gt;2. Dinosaur Brains – Dealing with All Those Impossible People at Work &lt;/h3&gt; &lt;p align="justify"&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/0345410211/elegantcode-20" target="_blank"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 10px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" align="left" src="http://lh3.ggpht.com/-aW8JzYxR0oA/TeAvRf6NG1I/AAAAAAAABl0/big2J1Zpthw/image%25255B11%25255D.png?imgmax=800" width="159" height="244"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;I had this book literally collecting dust on my bookshelf for several years now, so finally being able to read this book has been well overdue. &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0345410211/elegantcode-20" target="_blank"&gt;Dinosaur Brains&lt;/a&gt;, written by &lt;a href="http://www.albernstein.com/id23.htm" target="_blank"&gt;Albert J. Bernstein&lt;/a&gt; and Sydney Craft Rozen, is all about how we sometimes react and behave purely by our primal instincts, when the cortex in our brain loses control and falls back on lizard logic in some situations. Funny enough, this book has taught me more about myself than about my current co-workers or former colleagues. This metaphor of a prehistoric creature illustrates how our brain triggers a fight, flight or freight response that is sometimes well beyond our control. But this isn’t a lost cause either. This book not only provides the rules of the dinosaur brain, but it also contains a lot of advice on how to use this knowledge to your own advantage. So, overall a very interesting read.&lt;/p&gt; &lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;&amp;nbsp;&lt;/h3&gt; &lt;h3&gt;3. Drive - The Surprising Truth About What Motivates Us&lt;/h3&gt; &lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/1594484805/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 10px 10px 0px 0px; display: inline; float: left" align="left" src="http://www.carsoncoots.com/wp-content/uploads/2010/10/drive.png" width="158" height="238"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;I picked up &lt;a href="http://www.amazon.com/exec/obidos/ASIN/1594484805/elegantcode-20" target="_blank"&gt;Drive – The Surprising Truth About What Motivates Us&lt;/a&gt; after I watched &lt;a href="http://www.ted.com/talks/dan_pink_on_motivation.html" target="_blank"&gt;this inspiring talk&lt;/a&gt; by &lt;a href="http://www.danpink.com/" target="_blank"&gt;Daniel Pink&lt;/a&gt;, which I briefly mentioned in &lt;a href="http://vanryswyckjan.blogspot.com/2010/10/laborers-versus-professionals.html" target="_blank"&gt;this blog post&lt;/a&gt;. The author makes a very strong case for what he calls Motivation 3.0, which is based on three concepts: &lt;/p&gt; &lt;ol&gt; &lt;li&gt; &lt;div align="justify"&gt;Autonomy – based on the principles of self-direction, this lets knowledge workers decide how, when and where to do their job. &lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Mastery – getting people in a state of “flow” by letting them work on stuff that they’re passionate about. “Flow” is a state of mind that where time seems to be passing by without noticing. The author clearly explains why a restrictive working environment prevents people from getting into their “flow”.&amp;nbsp; &lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Purpose – the believe that there’s more to work than just making money. The fact that there’s meaning in what you do day in and day out, enables our intrinsic motivation.&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p align="justify"&gt;I also couldn’t help but notice that this book tends to lay out the basics of &lt;a href="http://www.systemswiki.org/index.php?title=Systems_Thinking" target="_blank"&gt;systems thinking&lt;/a&gt;, which is a topic that I definitely want to learn more about.&lt;/p&gt; &lt;p align="justify"&gt;Do yourself a favor, pick up and read this book or at the very least, watch &lt;a href="http://www.ted.com/talks/dan_pink_on_motivation.html" target="_blank"&gt;this excellent talk&lt;/a&gt;. Buy this book as a gift for your boss and tell him that all the cool managers are reading this book ;-).&lt;/p&gt; &lt;h3&gt;4. Pragmatic Thinking and Learning – Refactor You Wetware&lt;/h3&gt; &lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/9781934356050/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 10px 10px 0px 0px; display: inline; float: left" align="left" src="http://i43.tower.com/images/mm112034452/pragmatic-thinking-learning-refactor-your-wetware-andy-hunt-paperback-cover-art.jpg" width="155" height="185"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/9781934356050/elegantcode-20" target="_blank"&gt;Pragmatic Thinking and Learning&lt;/a&gt;, written by &lt;a href="http://blog.toolshed.com/" target="_blank"&gt;Andy Hunt&lt;/a&gt;, is also a book that I wanted to read for quite some time now. As he is one of the co-authors of &lt;a href="http://vanryswyckjan.blogspot.com/2008/04/book-review-pragmatic-programmer.html" target="_blank"&gt;The Pragmatic Programmer&lt;/a&gt;, I had some very high expectations. And I must say that those expectations were only partially fulfilled. The first few chapters were a reiteration of Daniel Pink’s book &lt;a href="http://vanryswyckjan.blogspot.com/2010/10/book-review-whole-new-mind-why-right.html" target="_blank"&gt;A Whole New Mind&lt;/a&gt;, but then applied to the world of software development. There was even some content in there from the book Dinosaurs Brains, which I had just finished reading at the time. But if you haven’t read these two books before, then these first chapters will definitely be an eye opener.&lt;/p&gt; &lt;p align="justify"&gt;Nonetheless, I did manage to pick up a few neat ideas about learning in general and how to apply those to my own learning activities. But I couldn’t get rid of the feeling that most of the content was just an iteration of what I already picked up from other books and articles. &lt;/p&gt; &lt;p align="justify"&gt;Don’t get me wrong here. This is truly a great manuscript with a lot of gems in there. If you’re not already familiar with this stuff, then this book will definitely rock your boat and I would highly recommend it.&lt;/p&gt; &lt;p align="justify"&gt;So, happy reading and until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-1290059026642881766?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/aVz30rV45U8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/aVz30rV45U8/quartet-of-book-reviews.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-aW8JzYxR0oA/TeAvRf6NG1I/AAAAAAAABl0/big2J1Zpthw/s72-c/image%25255B11%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/05/quartet-of-book-reviews.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1673076075079954468</guid><pubDate>Tue, 10 May 2011 10:00:00 +0000</pubDate><atom:updated>2011-05-10T12:00:07.556+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – npm 1.0</title><description>&lt;p align="justify"&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js" target="_blank"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://blog.izs.me/" target="_blank"&gt;Isaac&lt;/a&gt; recently released &lt;a href="http://npmjs.org/" target="_blank"&gt;version 1.0 of npm&lt;/a&gt;, which is a package manager for Node.js. I’ve been using npm very early on when I started fiddling with Node.js and I’ve come to rely on it quite heavily. After I upgraded to one of the release candidates, I noticed a big improvement in how and where packages are being installed.&lt;/p&gt; &lt;p align="justify"&gt;With the old version that I was using (0.3.15), all packages where installed globally. You could install multiple versions of the same package, but this implied that we also had to take this into account when we loaded a particular module in our application:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;var step = require(&lt;/em&gt;&lt;a href="mailto:&amp;lsquo;connect@0.2.7&amp;rsquo;"&gt;&lt;em&gt;‘connect@0.2.7’&lt;/em&gt;&lt;/a&gt;&lt;em&gt;);&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;Although this worked great, it’s still my humble opinion that all third-party modules should be right along the source code of the application that I’m building. With the npm 1.0 release, this has now become the default behavior.&lt;/p&gt; &lt;p align="justify"&gt;Installing npm 1.0 is as simple as executing the following in the command-line: &lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;code&gt;&lt;em&gt;curl http://npmjs.org/install.sh | sh &lt;/em&gt;&lt;/code&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;Executing this command asks the user whether any old versions of npm should be removed. You can also execute the following if you don’t like to be prompted:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;curl http://npmjs.org/install.sh&lt;/em&gt;&lt;a href="http://npmjs.org/install.sh"&gt;&lt;em&gt;&lt;/em&gt;&lt;/a&gt;&lt;em&gt; | clean=yes sh&lt;/em&gt; &lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;&lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs.html" target="_blank"&gt;In a previous post&lt;/a&gt;, I discussed a new convention introduced in version 4.0.x of Node.js for loading third-party modules from a folder named &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs.html" target="_blank"&gt;node_modules&lt;/a&gt; that is typically created in the root folder of a project. When installing a package, npm adheres to this convention by locally installing this package into this &lt;em&gt;node_modules&lt;/em&gt; folder.&lt;/p&gt; &lt;p align="justify"&gt;Say, for example, that we want to use &lt;a href="http://vanryswyckjan.blogspot.com/2011/05/taking-baby-steps-with-nodejs.html" target="_blank"&gt;Socket.IO&lt;/a&gt; in our application. We just need to run the following command from the root folder of our application:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;npm install socket.io&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;This will locally install the requested third-party module into &lt;em&gt;./node_modules/socket.io&lt;/em&gt;. If a particular package includes a binary, then these will go into &lt;em&gt;./node_modules/.bin/&lt;/em&gt;. Now we can simply load the installed module without specifying a specific version number:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;var socketIO = require('socket.io');&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;So what do we do if we want to install packages that are typically used from the shell and not by the source code of the application, like &lt;a href="http://elegantcode.com/2011/02/09/taking-baby-steps-with-node-js-node-version-management-with-n/" target="_blank"&gt;n&lt;/a&gt;, &lt;a href="http://elegantcode.com/2011/01/14/taking-baby-steps-with-node-js-debugging-with-node-inspector/" target="_blank"&gt;node-inspector&lt;/a&gt; or &lt;a href="http://vanryswyckjan.blogspot.com/2011/04/taking-baby-steps-with-nodejs-some.html" target="_blank"&gt;nodemon&lt;/a&gt;? Well, we can still get them installed globally using the &lt;em&gt;–g&lt;/em&gt; command-line switch:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;npm install -g n&lt;br&gt;npm install -g node-inspector&lt;br&gt;npm install -g nodemon&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;This ability to install packages either locally or globally is probably the most obvious new feature that has been shipped with the 1.0 release. There are several &lt;a href="http://blog.nodejs.org/2011/05/01/npm-1-0-released/" target="_blank"&gt;other new features and enhancements&lt;/a&gt; as well, but I haven’t ran into those (yet). &lt;/p&gt; &lt;p align="justify"&gt;Apparently, there will also be no new major features or architectural changes for quite some time. So this gives us plenty of time to get to the bottom of all the new capabilities of npm. This local/global installation of packages alone is certainly worth upgrading to the 1.0 release.&lt;/p&gt; &lt;p&gt;Until next time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15643421-1673076075079954468?l=vanryswyckjan.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/8XddaB8UvaQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/8XddaB8UvaQ/taking-baby-steps-with-nodejs-npm-10.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/05/taking-baby-steps-with-nodejs-npm-10.html</feedburner:origLink></item></channel></rss>

