<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; Anders Janmyr</title>
	<atom:link href="http://blog.jayway.com/author/andersjanmyr/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 28 Jan 2012 15:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Finding with Git</title>
		<link>http://blog.jayway.com/2012/01/25/finding-with-git/</link>
		<comments>http://blog.jayway.com/2012/01/25/finding-with-git/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 16:23:07 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=11903</guid>
		<description><![CDATA[Git is an amazing version control system that never loses anything, but sometimes it can be hard to find out where things are. Most of the time it is going to be git log that is our friend, but not all the time. Where is my file? Sometimes you know that you have a file [...]]]></description>
			<content:encoded><![CDATA[<p>Git is an amazing version control system that never loses anything, but<br />
sometimes it can be hard to find out where things are. Most of the time<br />
it is going to be <code>git log</code> that is our friend, but not all the time.</p>
<h2>Where is my file?</h2>
<p>Sometimes you know that you have a file in your repository but, you<br />
don&#39;t know exactly where it is. <code>git ls-files</code> is the answer.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Find all files with the name security in the path.</font></i>
$ git ls-files <font color="#990000">|</font> grep security
lib/dynamo-db/security<font color="#990000">.</font>js
test/security-test<font color="#990000">.</font>js
</tt></pre>
<p>Obviously, you can use any particular <code>grep</code> options you prefer, like <code>-i</code><br />
to ignore case.</p>
<h2>In what files does <em>word</em> exist?</h2>
<p>If you want to find information inside files <code>git grep</code> is your friend.<br />
<code>git grep</code> works similar to a recursive grep <code>grep -r .</code> but it only<br />
searches files that are managed by git.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Find all lines matching *crypt* in current version.</font></i>
$ git grep crypt
lib/dynamo-db/security<font color="#990000">.</font>js<font color="#990000">:</font>var crypto <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'crypto'</font><font color="#990000">);</font>
lib/dynamo-db/security<font color="#990000">.</font>js<font color="#990000">:</font>   var hmac <font color="#990000">=</font> crypto<font color="#990000">.</font>createHmac<font color="#990000">(</font><font color="#FF0000">'sha256'</font><font color="#990000">,</font> key<font color="#990000">);</font>

<i><font color="#9A1900"># Also give me the line numbers</font></i>
 git grep -n crypt
lib/dynamo-db/security<font color="#990000">.</font>js<font color="#990000">:</font><font color="#993399">2</font><font color="#990000">:</font>var crypto <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'crypto'</font><font color="#990000">);</font>
lib/dynamo-db/security<font color="#990000">.</font>js<font color="#990000">:</font><font color="#993399">15</font><font color="#990000">:</font>   var hmac <font color="#990000">=</font> crypto<font color="#990000">.</font>createHmac<font color="#990000">(</font><font color="#FF0000">'sha256'</font><font color="#990000">,</font> key<font color="#990000">);</font>

<i><font color="#9A1900"># List only the file names</font></i>
 git grep -l crypt
lib/dynamo-db/security<font color="#990000">.</font>js

<i><font color="#9A1900"># Also list how many times (count) it matched.</font></i>
$ git grep -c crypt
lib/dynamo-db/security<font color="#990000">.</font>js<font color="#990000">:</font><font color="#993399">2</font>
</tt></pre>
<p>It is also possible to give versions to <code>grep</code> to find out what has<br />
changed between revisions.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Find all files with lines matching *type* in revisions `master` and `8f0fb7f`.</font></i>
git grep -l <b><font color="#0000FF">type</font></b>  master 8f0fb7f
master<font color="#990000">:</font>lib/dynamo-db/index<font color="#990000">.</font>js
master<font color="#990000">:</font>lib/dynamo-db/security<font color="#990000">.</font>js
master<font color="#990000">:</font>package<font color="#990000">.</font>json
8f0fb7f<font color="#990000">:</font>lib/dynamo-db/index<font color="#990000">.</font>js
8f0fb7f<font color="#990000">:</font>package<font color="#990000">.</font>json
</tt></pre>
<p>Maybe this is not that impressive. Most of the above can be accomplished<br />
with standard <code>grep</code>, <code>find</code>, <code>ack</code>, and friends. But Git is a version<br />
control system. How do I find out about things that happened in the past?</p>
<h2>Who deleted my file?</h2>
<p>You know how it is, you are working in some project and, your dog gets<br />
sick and you have to stay home from work, when you come back someone has<br />
deleted your file! Where is it and who did it? <code>git log</code> to the rescue.</p>
<p><code>git log</code> shows you the commit logs. It is your eye into the past.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># When (in what commit) was my file deleted?</font></i>
$ git log --diff-filter<font color="#990000">=</font>D -- test/create-table-test<font color="#990000">.</font>js
commit ba6c4d8bc165b8fb8208979c3e5513bd53477d51
Author<font color="#990000">:</font> Anders Janmyr <font color="#990000">&lt;</font>anders@janmyr<font color="#990000">.</font>com<font color="#990000">&gt;</font>
Date<font color="#990000">:</font>   Wed Jan <font color="#993399">25</font> <font color="#993399">09</font><font color="#990000">:</font><font color="#993399">46</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#993399">2012</font> <font color="#990000">+</font><font color="#993399">0100</font>

    Removed the stupid failing <b><font color="#0000FF">test</font></b><font color="#990000">.</font>
</tt></pre>
<p>Looks like I found the culprit, was I working from home? But is the file<br />
really deleted here. To get some more information about files add the<br />
<code>--summary</code> option.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># When (in what commit) was my file deleted?</font></i>
$ git log --diff-filter<font color="#990000">=</font>D --summary -- test/create-table-test<font color="#990000">.</font>js
commit ba6c4d8bc165b8fb8208979c3e5513bd53477d51
Author<font color="#990000">:</font> Anders Janmyr <font color="#990000">&lt;</font>anders@janmyr<font color="#990000">.</font>com<font color="#990000">&gt;</font>
Date<font color="#990000">:</font>   Wed Jan <font color="#993399">25</font> <font color="#993399">09</font><font color="#990000">:</font><font color="#993399">46</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#993399">2012</font> <font color="#990000">+</font><font color="#993399">0100</font>

    Removed the stupid failing <b><font color="#0000FF">test</font></b><font color="#990000">.</font>

 delete mode <font color="#993399">100644</font> test/create-table-test<font color="#990000">.</font>js
</tt></pre>
<p>Yes, it looks like the file is really deleted. Stupid bastard! Let me<br />
break this command down starting with the last command.</p>
<ul>
<li><code>test/create-table-test.js</code> - The filename has to be the relative path<br />
to the file (from your current directory).</li>
<li><code>--</code> - The double-dash is used to tell Git that this is not a branch<br />
or an option.</li>
<li><code>--summary</code> - Show me what files were deleted or added.<br />
<code>--name-status</code> is similar.</li>
<li><code>--diff-filter</code> - This is a real beauty, it allows me to limit the log<br />
to show me only the specified kind of change, in this case <code>D</code>, for<br />
Deleted. Other options are: Added (A), Copied (C), Modified (M) and Renamed (R)</li>
</ul>
<h2>When was a file added?</h2>
<p>This uses the same technique as above, but I will vary it since I don&#39;t<br />
want to type the full path of the file.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Find out when the integration tests where added.</font></i>
$ git log --diff-filter<font color="#990000">=</font>A --name-status <font color="#990000">|</font>grep -C <font color="#993399">6</font> integ
commit 09420cfea8c7b569cd47f690104750fec358a10a
Author<font color="#990000">:</font> Anders Janmyr <font color="#990000">&lt;</font>anders@janmyr<font color="#990000">.</font>com<font color="#990000">&gt;</font>
Date<font color="#990000">:</font>   Tue Jan <font color="#993399">24</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">23</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#993399">2012</font> <font color="#990000">+</font><font color="#993399">0100</font>

    Extracted integration <b><font color="#0000FF">test</font></b>

A	integration-test/sts-test<font color="#990000">.</font>js

commit 205db3965dec6c2c4c7b2bb75387a591d49e1951
Author<font color="#990000">:</font> Anders Janmyr <font color="#990000">&lt;</font>anders@janmyr<font color="#990000">.</font>com<font color="#990000">&gt;</font>
Date<font color="#990000">:</font>   Sat Jan <font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#993399">2012</font> <font color="#990000">+</font><font color="#993399">0100</font>
</tt></pre>
<p>As you can see here I am using <code>--name-status</code> as a variation on<br />
<code>--summary</code>, it uses the same notation as the <code>--diff-filter</code>.</p>
<p>I am using <code>grep -C 6</code> to get some context around the found element, in<br />
this case six lines before and after the match. Very useful!</p>
<h2>Who changed that line?</h2>
<p>As you probably know it is to see who has done something in a file by<br />
using <code>git blame</code></p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>$ git blame test/security-test<font color="#990000">.</font>js
205db396 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">1</font><font color="#990000">)</font> var vows <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'vows'</font><font color="#990000">),</font>
205db396 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">2</font><font color="#990000">)</font>     assert <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'assert'</font><font color="#990000">);</font>
205db396 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">3</font><font color="#990000">)</font>
09420cfe <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">24</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">23</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">4</font><font color="#990000">)</font> var access <font color="#990000">=</font> <font color="#FF0000">'access'</font><font color="#990000">;</font>
09420cfe <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">24</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">23</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">5</font><font color="#990000">)</font> var secret <font color="#990000">=</font> <font color="#FF0000">'secret'</font><font color="#990000">;</font>
90b65208 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">11</font><font color="#990000">:</font><font color="#993399">58</font><font color="#990000">:</font><font color="#993399">21</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">6</font><font color="#990000">)</font>
205db396 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">7</font><font color="#990000">)</font> var security <font color="#990000">=</font> new Security<font color="#990000">(</font>{
90b65208 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">11</font><font color="#990000">:</font><font color="#993399">58</font><font color="#990000">:</font><font color="#993399">21</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">8</font><font color="#990000">)</font>   access<font color="#990000">:</font> access<font color="#990000">,</font>
90b65208 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">11</font><font color="#990000">:</font><font color="#993399">58</font><font color="#990000">:</font><font color="#993399">21</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">9</font><font color="#990000">)</font>   secret<font color="#990000">:</font> secret
205db396 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">10</font><font color="#990000">:</font><font color="#993399">03</font><font color="#990000">:</font><font color="#993399">59</font> <font color="#990000">+</font><font color="#993399">0100</font> <font color="#993399">10</font><font color="#990000">)</font> }<font color="#990000">);</font>
<font color="#990000">...</font>
</tt></pre>
<p>Every line gets annotated with the commit who introduced it and by whom.<br />
Very helpful.</p>
<h2>Who deleted that line?</h2>
<p>Another feature, which is not as well known, is <code>git blame --reverse</code>.<br />
It allows you to see the file as it was before, annotated to show you<br />
where it has been changed.</p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Check the what lines have been changed since the last 6 commits.</font></i>
$ git blame --reverse head<font color="#990000">~</font><font color="#993399">6</font><font color="#990000">..</font>head security-test<font color="#990000">.</font>js
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">1</font><font color="#990000">)</font> var vows <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'vows'</font><font color="#990000">),</font>
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">2</font><font color="#990000">)</font>     assert <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'assert'</font><font color="#990000">);</font>
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">3</font><font color="#990000">)</font>
093c13e9 <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">24</font> <font color="#993399">17</font><font color="#990000">:</font><font color="#993399">26</font><font color="#990000">:</font><font color="#993399">09</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">4</font><font color="#990000">)</font> var Security <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'dynamo-db'</font><font color="#990000">).</font>Security<font color="#990000">;</font>
ba6c4d8b <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">09</font><font color="#990000">:</font><font color="#993399">46</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">5</font><font color="#990000">)</font>
<font color="#990000">^</font>b96c68b <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">12</font><font color="#990000">:</font><font color="#993399">33</font><font color="#990000">:</font><font color="#993399">50</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">6</font><font color="#990000">)</font> var access <font color="#990000">=</font> process<font color="#990000">.</font>env<font color="#990000">[</font><font color="#FF0000">'S3_KEY'</font><font color="#990000">];</font>
<font color="#990000">^</font>b96c68b <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">21</font> <font color="#993399">12</font><font color="#990000">:</font><font color="#993399">33</font><font color="#990000">:</font><font color="#993399">50</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">7</font><font color="#990000">)</font> var secret <font color="#990000">=</font> process<font color="#990000">.</font>env<font color="#990000">[</font><font color="#FF0000">'S3_SECRET'</font><font color="#990000">];</font>
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">8</font><font color="#990000">)</font>
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font>  <font color="#993399">9</font><font color="#990000">)</font> var security <font color="#990000">=</font> new Security<font color="#990000">(</font>{
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font> <font color="#993399">10</font><font color="#990000">)</font>   access<font color="#990000">:</font> access<font color="#990000">,</font>
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font> <font color="#993399">11</font><font color="#990000">)</font>   secret<font color="#990000">:</font> secret
558b8e7f <font color="#990000">(</font>Anders Janmyr <font color="#993399">2012</font>-<font color="#993399">01</font>-<font color="#993399">25</font> <font color="#993399">16</font><font color="#990000">:</font><font color="#993399">53</font><font color="#990000">:</font><font color="#993399">52</font> <font color="#990000">+</font><font color="#993399">0100</font> <font color="#993399">12</font><font color="#990000">)</font> }<font color="#990000">);</font>
</tt></pre>
<p>In the output you can see that most of the lines are still the same at<br />
HEAD (558b8e7f). But the fourth and fifth line <code>093c13e9</code> and <code>ba6c4d8b</code><br />
don&#39;t exist anymore. And the sixth and seventh lines <code>^b96c68b</code> have<br />
been changed after this commit.</p>
<h2>What commits contain the string?</h2>
<p>Another thing I find very useful is to find out when certain words or<br />
sentences are removed or added. For this you can use <code>git log -S&lt;string&gt;</code> or<br />
<code>git log -G&lt;regex&gt;</code></p>
<p><!-- Generator: GNU source-highlight 3.1.5<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Find commits that modified the string aws and display the full diff.</font></i>
$ git log -Saws --diff-filter<font color="#990000">=</font>M --patch
commit b96c68b839f204b310b79570bc3d27dc93cff588
Author<font color="#990000">:</font> Anders Janmyr <font color="#990000">&lt;</font>anders@janmyr<font color="#990000">.</font>com<font color="#990000">&gt;</font>
Date<font color="#990000">:</font>   Sat Jan <font color="#993399">21</font> <font color="#993399">12</font><font color="#990000">:</font><font color="#993399">33</font><font color="#990000">:</font><font color="#993399">50</font> <font color="#993399">2012</font> <font color="#990000">+</font><font color="#993399">0100</font>

    We have a valid request<font color="#990000">,</font> tjohoo

diff --git a/lib/dynamo-db/security<font color="#990000">.</font>js b/lib/dynamo-db/security<font color="#990000">.</font>js
index bee6936<font color="#990000">..</font><font color="#993399">8471527</font> <font color="#993399">100644</font>
--- a/lib/dynamo-db/security<font color="#990000">.</font>js
<font color="#990000">+++</font> b/lib/dynamo-db/security<font color="#990000">.</font>js
@@ -<font color="#993399">2</font><font color="#990000">,</font><font color="#993399">6</font> <font color="#990000">+</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">7</font> @@
 var crypto <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'crypto'</font><font color="#990000">);</font>
 var _ <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'underscore'</font><font color="#990000">);</font>
 var request <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">"request"</font><font color="#990000">);</font>
<font color="#990000">+</font>var xml2js <font color="#990000">=</font> require<font color="#990000">(</font><font color="#FF0000">'xml2js'</font><font color="#990000">);</font>

 <b><font color="#000000">function Security</font></b><font color="#990000">(</font>options<font color="#990000">)</font> {
   this<font color="#990000">.</font>options <font color="#990000">=</font> options<font color="#990000">;</font>
@@ -<font color="#993399">23</font><font color="#990000">,</font><font color="#993399">7</font> <font color="#990000">+</font><font color="#993399">24</font><font color="#990000">,</font><font color="#993399">7</font> @@ mod<font color="#990000">.</font>timestamp <font color="#990000">=</font> <b><font color="#000000">function()</font></b> {
 mod<font color="#990000">.</font>defaultParams <font color="#990000">=</font> <b><font color="#000000">function()</font></b> {
   <b><font color="#0000FF">return</font></b> {
     AWSAccessKeyId<font color="#990000">:</font> this<font color="#990000">.</font>options<font color="#990000">.</font>access<font color="#990000">,</font>
-    Version<font color="#990000">:</font> <font color="#FF0000">'2010-05-08'</font><font color="#990000">,</font>
<font color="#990000">+</font>    Version<font color="#990000">:</font> <font color="#FF0000">'2011-06-15'</font><font color="#990000">,</font>
     Timestamp<font color="#990000">:</font> this<font color="#990000">.</font><b><font color="#000000">timestamp()</font></b><font color="#990000">,</font>
     SignatureVersion<font color="#990000">:</font> <font color="#993399">2</font><font color="#990000">,</font>
     SignatureMethod<font color="#990000">:</font> <font color="#FF0000">'HmacSHA256'</font>
@@ -<font color="#993399">57</font><font color="#990000">,</font><font color="#993399">9</font> <font color="#990000">+</font><font color="#993399">58</font><font color="#990000">,</font><font color="#993399">10</font> @@ mod<font color="#990000">.</font>url <font color="#990000">=</font> function<font color="#990000">(</font>host<font color="#990000">,</font> path<font color="#990000">,</font> params<font color="#990000">)</font> {

 mod<font color="#990000">.</font>makeRequest <font color="#990000">=</font> function<font color="#990000">(</font>method<font color="#990000">,</font> host<font color="#990000">,</font> path<font color="#990000">,</font> params<font color="#990000">,</font> callback<font color="#990000">)</font> {
   var extParams <font color="#990000">=</font> _<font color="#990000">.</font>extend<font color="#990000">(</font>{}<font color="#990000">,</font> this<font color="#990000">.</font><b><font color="#000000">defaultParams()</font></b><font color="#990000">,</font> params<font color="#990000">);</font>
-  var signedParams <font color="#990000">=</font> this<font color="#990000">.</font>signedParams<font color="#990000">(</font><font color="#FF0000">'GET'</font><font color="#990000">,</font> <font color="#FF0000">'iam.amazonaws.com'</font><font color="#990000">,</font> <font color="#FF0000">'/'</font><font color="#990000">,</font> extParams<font color="#990000">);</font>
-  console<font color="#990000">.</font>log<font color="#990000">(</font>extParams<font color="#990000">,</font>signedParams<font color="#990000">);</font>
-  <b><font color="#0000FF">return</font></b> request<font color="#990000">(</font>{ method<font color="#990000">:</font> method<font color="#990000">,</font> url<font color="#990000">:</font> this<font color="#990000">.</font>url<font color="#990000">(</font>host<font color="#990000">,</font> path<font color="#990000">,</font> signedParams<font color="#990000">)</font> }<font color="#990000">,</font>
<font color="#990000">+</font>  var signedParams <font color="#990000">=</font> this<font color="#990000">.</font>signedParams<font color="#990000">(</font>method<font color="#990000">,</font> host<font color="#990000">,</font> path<font color="#990000">,</font> extParams<font color="#990000">);</font>
<font color="#990000">+</font>  var url <font color="#990000">=</font> this<font color="#990000">.</font>url<font color="#990000">(</font>host<font color="#990000">,</font> path<font color="#990000">,</font> signedParams<font color="#990000">);</font>
<font color="#990000">+</font>  console<font color="#990000">.</font>log<font color="#990000">(</font>url<font color="#990000">,</font>signedParams<font color="#990000">);</font>
<font color="#990000">+</font>  <b><font color="#0000FF">return</font></b> request<font color="#990000">(</font>{ method<font color="#990000">:</font> method<font color="#990000">,</font> url<font color="#990000">:</font> url }<font color="#990000">,</font>
<font color="#990000">...</font>
</tt></pre>
<p>That&#39;s all folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2012/01/25/finding-with-git/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>7 Reasons to go to &#216;redev 2011</title>
		<link>http://blog.jayway.com/2011/10/04/7-reasons-to-go-to-redev-2011/</link>
		<comments>http://blog.jayway.com/2011/10/04/7-reasons-to-go-to-redev-2011/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 15:54:24 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[oredev]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=9959</guid>
		<description><![CDATA[I’m am looking forward to Øredev 2011 more than I have looked forward to any of the previous ones. The reason for this is that Øredev has finally become a leading conference for dynamic programming. Øredev has always been good in the enterprise sphere led by Java, .Net and mobile tracks, but it has been [...]]]></description>
			<content:encoded><![CDATA[<p>I’m am looking forward to Øredev 2011 more than I have looked forward to<br />
any of the previous ones. The reason for this is that Øredev has finally<br />
become a leading conference for dynamic programming.</p>
<p>Øredev has always been good in the enterprise sphere led by Java, .Net and<br />
mobile tracks, but it has been weaker in the area of dynamic programming<br />
languages. Last year was better than before, but this year is going<br />
to be great. Here are some speakers that you should not miss.</p>
<h2>Yehuda Katz</h2>
<p>Yehuda Katz was one of the driving forces behind the great Rails<br />
3 refactoring, that made sure that Rails will remain the most productive<br />
web development environment for many years to come. Yehuda just released<br />
a new book, <a href="http://www.amazon.com/Rails-3-Action-Yehuda-Katz/dp/1935182277?tag=thtasta-20">Rails 3 in Action</a>, Its the first book about Rails 3.1, including the awesome <a href="http://guides.rubyonrails.org/asset_pipeline.html">Asset Pipeline</a>, <a href="http://asciicasts.com/episodes/266-http-streaming">streaming</a>,and <a href="http://guides.rubyonrails.org/migrations.html#writing-your-change-method">reversible migrations</a>.</p>
<p>Yehuda has also been involved with jQuery and written <a href="http://www.amazon.com/jQuery-Action-Second-Bear-Bibeault/dp/1935182323?tag=thtasta-20">jQuery in Action</a>.</p>
<p>He will be speaking about <a href="http://oredev.org/2011/sessions/rails">Rails</a> and <a href="http://oredev.org/2011/sessions/sproutcore">Sproutcore</a>.</p>
<h2>Felix Geisendörfer</h2>
<p>Felix Geisendörfer is a core Node.js developer and he will of course be<br />
talking about <a href="http://oredev.org/2011/sessions/node-js--a-practical-introduction">Node</a>.<br />
Node.js is a set of Javascript libraries that runs on top of the Google<br />
V8 virtual machine. What is interesting about Node, apart from being<br />
server-side Javascript is that it uses asynchronous programming as the<br />
default. This default makes Node extremely interesting for developing<br />
solutions involving multiple open connections, such as websockets, and<br />
for streaming video and audio. Node is definitely part of the future of<br />
the web. I have written <a href="http://anders.janmyr.com/2011/05/not-very-short-introduction-to-nodejs.html">extensively about it</a> in the past.</p>
<h2>Corey Haines</h2>
<p>Cory Haines is a legend in the TDD community. He is also famous for his <a href="http://coderetreat.com/">Code Retreats</a>. He will give one workshop, <a href="http://oredev.org/2011/sessions/improving-your-tdd">Improving your TDD</a>, and two presentations,<a href="http://oredev.org/2011/sessions/fast-ruby-on-rails-tests">Fast Ruby on Rails Tests</a> and <a href="http://oredev.org/2011/sessions/come-introduce-yourself-to-the-concepts-and-fundamental-technique-behind-tdd">Come introduce yourself to the concepts and fundamental technique behind TDD</a></p>
<h2>Ilya Grigorik</h2>
<p>Ilya Grigorik is the founder of <a href="http://www.postrank.com/">PostRank</a>, that was recently acquired by Google. He is now working on Social Analytics at Google. At PostRank he used Ruby to perform analysis on very large amounts of data. While doing this he developed<br />
<a href="http://postrank-labs.github.com/goliath/">Goliath</a>, a high-performance non-blocking web server using Ruby 1.9 and fibers.</p>
<p>He will be talking about <a href="http://oredev.org/2011/sessions/0-60-with-goliath-building-high-performance-ruby-web-services">Goliath</a>, <a href="http://oredev.org/2011/sessions/modeling-concurrency-in-ruby-and-beyond">Concurrency</a> and <a href="http://oredev.org/2011/sessions/intelligent-code-getting-started-with-machine-learning">Machine Learning</a></p>
<p>I can recommend that you <a href="https://twitter.com/#!/igrigorik">follow Ilya on Twitter</a> since his tweets has the highest signal-to-noise ratio I know of.</p>
<p>And, finally, make sure to check out <a href="http://vimgolf.com/">Vim Golf</a>, a really cool way to become a Vim wizard.</p>
<h2>Trevor Burnham</h2>
<p><a href="http://jashkenas.github.com/coffee-script/">Coffeescript</a> is the new way to write Javascript without actually writing it <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Coffeescript is an elegant language, created by Jeremy Ashkenas, with features from Ruby and Python. The language is very pure and removes a lot of clutter. Coffeescript is compiled into good, efficient Javascript. Trevor Burnham has written a book, <a href="http://www.amazon.com/CoffeeScript-Accelerated-JavaScript-Development-Pragmatic/dp/1934356786?tag=thtasta-20">CoffeeScript: Accelerated JavaScript Development</a>, on the subject and he will be giving two presentations about it,<br />
<a href="http://oredev.org/2011/sessions/coffeescript-design-patterns-for-the-new-javascript">CoffeeScript: Design Patterns for the New JavaScript</a> and <a href="http://oredev.org/2011/sessions/transforming-data-into-pixels-visualization-with-canvas-and-coffeescript">Transforming Data into Pixels: Visualization with Canvas and CoffeeScript</a></p>
<h2>Charles Nutter</h2>
<p>Charles Nutter is the man behind JRuby. He has also create another<br />
language called <a>Mirah</a>, which he will be talking about in <a href="http://oredev.org/2011/sessions/have-you-tried-mirah-yet">Have you tried Mirah yet?</a>.</p>
<p>While doing all this he has obviously learned a thing or two about the<br />
JVM and about bytecodes. Who could be better to teach us about the<br />
internals of the JVM. Charles will be giving another talk about this in <a href="http://oredev.org/2011/sessions/what-the-jvm-does-with-your-bytecode-when-nobodys-looking">What the JVM Does With your Bytecode when Nobody’s Looking</a>.</p>
<h2>Simon Peyton Jones</h2>
<p>Even though this list of people is mostly about dynamic programming<br />
languages, it has to include <a href="http://oredev.org/2011/speakers/simon-peyton-jones">Simon Peyton Jones</a>.</p>
<p>Haskell is one of the most statically typed languages there is. It<br />
is, probably, also the most elegant programming language in the world.<br />
It is purely functional, has lazy evaluation, pattern matching, and<br />
currying by default. Even if you never use Haskell in a real-life<br />
project learning Haskell will be worth your while. If you want to get<br />
a good introduction to Haskell I can highly recommend <a href="http://www.amazon.com/Programming-Haskell-Graham-Hutton/dp/0521871727?tag=thtasta-20">Programming in Haskell</a> by Graham Hutton.</p>
<h2>Summary</h2>
<p>As you can see, <a href="http://oredev.org/2011/">this years Øredev</a> is looking better than ever before and I have only included a select part of it in this post. Missing it should be considered professional misconduct!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/10/04/7-reasons-to-go-to-redev-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Changes From 1.4.2 to 1.6</title>
		<link>http://blog.jayway.com/2011/08/09/jquery-changes-from-1-4-2-to-1-6/</link>
		<comments>http://blog.jayway.com/2011/08/09/jquery-changes-from-1-4-2-to-1-6/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 12:32:13 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=9163</guid>
		<description><![CDATA[jQuery is a powerful library and it is possible to get by without using any of the new features. That&#8217;s why many of us just upgrade to a new version assuming that it is mostly bug and performance fixes. This is not the case. jQuery 1.4.2 was released in February 2010 and it&#8217;s been one [...]]]></description>
			<content:encoded><![CDATA[
<p>jQuery is a powerful library and it is possible to get by without<br />
using any of the new features. That&rsquo;s why many of us<br />
just upgrade to a new version assuming that it is mostly bug and<br />
performance fixes. This is not the case. jQuery 1.4.2 was released in<br />
February 2010 and it&rsquo;s been one and a half years and a number of<br />
releases since then.</p>
<p>I was going to write about the changes in 1.5 and 1.6, but I have<br />
noticed that many people have missed some of the new features of the 1.4<br />
releases. And by the way, all examples are written in Coffeescript.</p>
<h2>Selected changes from 1.4.2+</h2>
<h3><code>delegate()</code></h3>
<p>Most people know about <code>live()</code> and how it can be used to attach<br />
listeners to elements that don&rsquo;t yet exist in the DOM. <code>live()</code> has<br />
a younger brother that was born in 1.4.2 and he is called <code>delegate()</code>.<br />
<code>delegate()</code> is more powerful. It gives you more precision in<br />
where to attach the listener.  </p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$<font color="#990000">(</font><font color="#FF0000">'.main-content'</font><font color="#990000">).</font><b><font color="#000000">find</font></b><font color="#990000">(</font><font color="#FF0000">'section'</font><font color="#990000">).</font>delegate <font color="#FF0000">'p'</font><font color="#990000">,</font> <font color="#FF0000">'click'</font><font color="#990000">,</font> <font color="#990000">-&gt;</font>
  $<font color="#990000">(</font><b><font color="#0000FF">this</font></b><font color="#990000">).</font>addClass <font color="#FF0000">'highlight'</font>
</tt></pre>
<p>As you can see above, <code>delegate()</code>, unlike <code>live()</code>, can be chained like<br />
normal jQuery calls.</p>
<h3><code>jQuery.now()</code>, <code>jQuery.type()</code> and <code>jQuery.parseXML()</code></h3>
<p>Nothing special here, just some utilities that are good to know about.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$<font color="#990000">.</font><b><font color="#000000">now</font></b><font color="#990000">()</font> <b><font color="#000000">is</font></b> <font color="#990000">(</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">Date</font></b><font color="#990000">()).</font><b><font color="#000000">getTime</font></b><font color="#990000">()</font> 

$<font color="#990000">.</font><b><font color="#000000">type</font></b><font color="#990000">(</font><font color="#FF0000">'hello'</font><font color="#990000">)</font> is <font color="#FF0000">'string'</font> 

xml <font color="#990000">=</font> <font color="#FF0000">"""</font>
<font color="#FF0000">&lt;rss version='2.0'&gt;</font>
<font color="#FF0000">&lt;channel&gt;</font>
<font color="#FF0000">&lt;title&gt;RSS Title&lt;/title&gt;</font>
<font color="#FF0000">&lt;/channel&gt;</font>
<font color="#FF0000">&lt;/rss&gt;</font>
<font color="#FF0000">"""</font>
xmlDoc <font color="#990000">=</font> $<font color="#990000">.</font>parseXML xml
<font color="#990000">(</font>$<font color="#990000">(</font>xmlDoc<font color="#990000">).</font>find <font color="#FF0000">'title'</font><font color="#990000">).</font><b><font color="#000000">text</font></b><font color="#990000">()</font> is <font color="#FF0000">'RSS Title'</font>
</tt></pre>
<p>All these utilities are interesting, but the truly good thing that came<br />
out of jQuery-1.5+ is <code>Deferred()</code>.</p>
<h3><code>Deferred()</code></h3>
<p>With the release of jQuery 1.5 the internal implementation of <code>$.ajax</code><br />
was changed to use <code>Deferred()</code>, and, even better, the implementation<br />
was deemed so useful, that it became part of the public API.</p>
<p>Here is how you can use it via the <code>$.ajax</code> method.</p>
<p>Declare a function hex that calls the <code>/hex</code> url on the server, which<br />
will return a hex value between <strong>00</strong> and <strong>FF</strong>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>  hex <font color="#990000">=</font> <font color="#990000">-&gt;</font>
    $<font color="#990000">.</font>ajax <font color="#FF0000">{</font> url<font color="#990000">:</font> <font color="#FF0000">'/hex'</font> <font color="#FF0000">}</font>
</tt></pre>
<p>Call the function multiple times, and you get a new <code>Deferred</code> back for<br />
each call. Notice the lack of handlers for the calls.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>  red <font color="#990000">=</font> <b><font color="#000000">hex</font></b><font color="#990000">()</font>
  green <font color="#990000">=</font> <b><font color="#000000">hex</font></b><font color="#990000">()</font>
  blue <font color="#990000">=</font> <b><font color="#000000">hex</font></b><font color="#990000">()</font>
</tt></pre>
<p>Attach handlers to each of the <code>Deferred</code>s to do something useful with<br />
the returned value. Notice the use of <code>done</code> instead of <code>success</code>.<br />
<code>success</code> is deprecated and will be removed in 1.8.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>  red<font color="#990000">.</font><b><font color="#000000">done</font></b> <font color="#990000">(</font>hh<font color="#990000">)-&gt;</font>
    $<font color="#990000">(</font><font color="#FF0000">"#r"</font><font color="#990000">).</font>css <font color="#FF0000">'background-color'</font><font color="#990000">,</font> <font color="#FF0000">"##{hh}0000"</font>
  green<font color="#990000">.</font><b><font color="#000000">done</font></b> <font color="#990000">(</font>hh<font color="#990000">)-&gt;</font>
    $<font color="#990000">(</font><font color="#FF0000">"#g"</font><font color="#990000">).</font>css <font color="#FF0000">'background-color'</font><font color="#990000">,</font> <font color="#FF0000">"#00#{hh}00"</font>
  blue<font color="#990000">.</font><b><font color="#000000">done</font></b> <font color="#990000">(</font>hh<font color="#990000">)-&gt;</font>
    $<font color="#990000">(</font><font color="#FF0000">"#b"</font><font color="#990000">).</font>css <font color="#FF0000">'background-color'</font><font color="#990000">,</font> <font color="#FF0000">"#0000#{hh}"</font>
</tt></pre>
<p>I am not limited to adding one handler, I can attach as many as I like.<br />
Here I attach another one for logging the success of the <code>red</code>-call.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>  red<font color="#990000">.</font><b><font color="#000000">done</font></b> <font color="#990000">(</font>hh<font color="#990000">)</font> <font color="#990000">-&gt;</font>
    console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font>hh<font color="#990000">)</font>
</tt></pre>
<p>If this was all there was to it, I would be happy, but it is not by<br />
using the <code>$.when</code> method I get a new <code>Deferred</code> object that orchestrates<br />
multiple <code>Deferred</code>s in a very simple way. Let me create a color-<code>Deferred</code><br />
that waits for the others to return and then calls a new callback when<br />
they are all done.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>  color <font color="#990000">=</font> $<font color="#990000">.</font><b><font color="#000000">when</font></b><font color="#990000">(</font>red<font color="#990000">,</font> green<font color="#990000">,</font> blue<font color="#990000">)</font> 

  color<font color="#990000">.</font><b><font color="#000000">done</font></b> <font color="#990000">(</font>r<font color="#990000">,</font> g<font color="#990000">,</font> b<font color="#990000">)</font> <font color="#990000">-&gt;</font>
    $<font color="#990000">(</font><font color="#FF0000">"#color"</font><font color="#990000">).</font>css <font color="#FF0000">'background-color'</font><font color="#990000">,</font> <font color="#FF0000">"##{r[0]}#{g[0]}#{b[0]}"</font>
</tt></pre>
<p>The results from the requests are given in the same order as the<br />
<code>Deferred</code> objects are passed in. The results are given as an array with<br />
three arguments <code>[ data, &lsquo;success&rsquo; , deferred ]</code>.</p>
<p>You can see the example, a simple Sinatra app, in action on <a href="http://colors-deferred.herokuapp.com/">Heroku</a> and the source code can be found on <a href="https://github.com/andersjanmyr/colors">Github</a></p>
<p>Very nice! Apart from the examples I have shown, there are methods<br />
working with <code>Deferred</code>. Methods for creating, <code>jQuery.Deferred()</code>,<br />
resolving, <code>resolve(), resolveWith()</code>, and rejecting, <code>reject(),<br />
rejectWith()</code>.</p>
<p>To attach handlers to the events, you can use <code>done()</code> for resolve,<br />
<code>fail</code> for reject, <code>always()</code> for both resolve and reject. You can also<br />
use <code>then(doneCallback, failCallback)</code> to attach both a <code>done</code> and a<br />
<code>fail</code> handler with one call.</p>
<p><code>Deferred</code>s are really cool, check them out <a href="http://api.jquery.com/category/deferred-object/">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/08/09/jquery-changes-from-1-4-2-to-1-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips from Rails Anti-Patterns</title>
		<link>http://blog.jayway.com/2011/06/05/tips-from-rails-anti-patterns/</link>
		<comments>http://blog.jayway.com/2011/06/05/tips-from-rails-anti-patterns/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 13:56:43 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8654</guid>
		<description><![CDATA[Another good Ruby book is out, Rails Anti-Patterns. The book is loaded with good tips on everything from following the Law of Demeter to cleaning up your views with the use of helper methods. Here are some things I picked up from the book. delegate can take a :prefix argument The delegate method from active_support [...]]]></description>
			<content:encoded><![CDATA[<p>Another good Ruby book is out, <a href="http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814">Rails Anti-Patterns</a>. The book is loaded with good tips on everything from following the <em>Law of Demeter</em> to cleaning up your views with the use of helper methods.</p>
<p>Here are some things I picked up from the book.</p>
<h2><code>delegate</code> can take a <code>:prefix</code> argument</h2>
<p>The <code>delegate</code> method from <code>active_support</code> is used for delegating calls<br />
to another object without having to write out the full delegating<br />
methods. It can take a prefix option to customize the name of the<br />
delegating methods.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Address
  <font color="#009900">@attr_accessors</font> <font color="#990000">:</font>street<font color="#990000">,</font> <font color="#990000">:</font>zip
<b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">class</font></b> Person
  attr_reader <font color="#990000">:</font>address
  <i><font color="#9A1900"># prefix =&gt; true, results in the model name being used as prefix</font></i>
  delegate <font color="#990000">:</font>street<font color="#990000">,</font> <font color="#990000">:</font>zip<font color="#990000">,</font> <font color="#990000">:</font>to <font color="#990000">=&gt;</font> <font color="#990000">:</font>address<font color="#990000">,</font> <font color="#990000">:</font>prefix <font color="#990000">=&gt;</font> <b><font color="#0000FF">true</font></b>
  <i><font color="#9A1900">#@person.address_street, @person.address_zip</font></i>
<b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">class</font></b> Person
  attr_reader <font color="#990000">:</font>billing_address<font color="#990000">,</font> <font color="#990000">:</font>delivery_address
  <i><font color="#9A1900"># prefix =&gt; string, uses the string as prefix</font></i>
  delegate <font color="#990000">:</font>street<font color="#990000">,</font> <font color="#990000">:</font>zip<font color="#990000">,</font> <font color="#990000">:</font>to <font color="#990000">=&gt;</font> <font color="#990000">:</font>address<font color="#990000">,</font> <font color="#990000">:</font>prefix <font color="#990000">=&gt;</font> delivery
  <i><font color="#9A1900">#@person.delivery_street, @person.delivery_zip</font></i>
  delegate <font color="#990000">:</font>street<font color="#990000">,</font> <font color="#990000">:</font>zip<font color="#990000">,</font> <font color="#990000">:</font>to <font color="#990000">=&gt;</font> <font color="#990000">:</font>address<font color="#990000">,</font> <font color="#990000">:</font>prefix <font color="#990000">=&gt;</font> billing
  <i><font color="#9A1900">#@person.billing_street, @person.billing_zip</font></i>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<h2>Transaction Scope</h2>
<p>The code executed in the ActiveRecord callbacks execute in the same<br />
transaction as the actual call to save, create, update, or delete.<br />
Knowing this helps to eliminate unneccessary explicit transactions.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Using a before filter</font></i>
<b><font color="#0000FF">class</font></b> Drink
  before_create <font color="#990000">:</font>remove_ingredients_from_bar

  <b><font color="#0000FF">def</font></b> remove_ingredients
    ingredients<font color="#990000">.</font>each <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>ingredient<font color="#990000">|</font>
      Bar<font color="#990000">.</font>remove<font color="#990000">(</font>ingredient<font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># Is better than using an explicit transaction</font></i>
<b><font color="#0000FF">class</font></b> Drink
  <b><font color="#0000FF">def</font></b> create_drink
    transaction <b><font color="#0000FF">do</font></b>
      remove_ingredients
      create
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> remove_ingredients
    ingredients<font color="#990000">.</font>each <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>ingredient<font color="#990000">|</font>
      Bar<font color="#990000">.</font>remove<font color="#990000">(</font>ingredient<font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<h2>Association Methods</h2>
<p>It is possible to add methods directly on the activerecord associations.<br />
This is especially handy if the method uses information from both sides<br />
of the relation.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Drink
  <i><font color="#9A1900">#has_field :minimum_drinking_age</font></i>
<b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">class</font></b> Customer
  <i><font color="#9A1900">#has_field :age</font></i>

  has_many <font color="#990000">:</font>drinks <b><font color="#0000FF">do</font></b>
    <b><font color="#0000FF">def</font></b> allowed
      <i><font color="#9A1900"># proxy_owner is the object defining the relation, Customer</font></i>
      where<font color="#990000">([</font><font color="#FF0000">'minimum_drinking_age &lt; ?'</font><font color="#990000">,</font> proxy_owner<font color="#990000">.</font>age<font color="#990000">])</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<h2>When to make a model active</h2>
<blockquote>
<p>If there is no user interface for adding, removing, or managing data,<br />
there is no need for an active model. A denormalized column populated<br />
by a hash or array of possible values is fine. </p>
</blockquote>
<p>This is really just the application of the KISS principle, Keep It<br />
Simple Stupid, but I have never seen it as clearly described before<br />
reading this book.</p>
<h2>Haml []</h2>
<p>A nice feature of Haml that I didn&rsquo;t know about, is the [] operator.<br />
When given an object, such as [record], [] acts as a combination of<br />
<code>div_for</code> and <code>content_for</code>, outputting a tag with the id and class<br />
attributes set appropriately.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><font color="#990000">-</font><i><font color="#9A1900"># This Haml</font></i>
<font color="#990000">%</font>span<font color="#990000">[</font>@team<font color="#990000">]</font>
</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- Results in this HTML --&gt;</font></i>
<b><font color="#0000FF">&lt;span</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">'team'</font> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">'team_1'</font><b><font color="#0000FF">&gt;&lt;/span&gt;</font></b>
</tt></pre>
<h2>RESTful actions</h2>
<p>When using resources in Rails there are seven methods that are used.</p>
<p><code>index, create, show, update, delete, edit</code>, and <code>new</code>. The first five<br />
naturally map to <code>get(collection), post(collection), get(singular),<br />
put(singular)</code>, and <code>delete(singular)</code>, but what isn&rsquo;t as obvious is<br />
that.</p>
<blockquote>
<p>The new and edit actions are really just different ways of representing<br />
the show action.</p>
</blockquote>
<p>This is of course obvious when you think about it, but once again Chad<br />
and Tammer has written it down in plain simple English.</p>
<h2>Rake Tasks</h2>
<p>How should you treat your application specific Rake tasks on order to<br />
test them easily. Once again the solution is very simple.</p>
<blockquote>
<p>Write the domain specific code as a class method on the appropriate<br />
model associated with the task.</p>
</blockquote>
<p>Then all you have to do is call the method from the task.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>task <font color="#990000">:</font>fill_bar_with_ingredients <b><font color="#0000FF">do</font></b>
  Bar<font color="#990000">.</font>fill_with_ingredients
<b><font color="#0000FF">end</font></b>
</tt></pre>
<p>It is not always appropriate to add this functionality to the existing<br />
models. This is a clue that another model is needed in your domain.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>task <font color="#990000">:</font>fill_bar_with_ingredients <b><font color="#0000FF">do</font></b>
  LiquorStore<font color="#990000">.</font>order_ingredients
<b><font color="#0000FF">end</font></b>
</tt></pre>
<h2>Database Index</h2>
<p>Since most applications have far more reads than writes, you should add<br />
indexes to every field that appears in a <code>WHERE</code> clause or an <code>ORDER</code><br />
clause. You should also add indexes for every combination of fields that<br />
are used that are combined with <code>AND</code>.</p>
<p>As always, don&rsquo;t follow this advice blindly, if a table only has three<br />
rows then perhaps the index is overkill&hellip;</p>
<h2>Conclusion</h2>
<p>Apart from these tips, there are tons of other useful information,<br />
making this book a must-read if you are doing Rails development.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/06/05/tips-from-rails-anti-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, an Exceptional Language</title>
		<link>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/</link>
		<comments>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/#comments</comments>
		<pubDate>Wed, 25 May 2011 09:47:59 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[continuations]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8536</guid>
		<description><![CDATA[Based on the book Exceptional Ruby by Avdi Grimm, I have developed a strategy for how I should deal with exceptions in Ruby. Being a very dynamic language, Ruby allows very flexible coding techniques. Exceptions are not an exception . When I am developing a library in Ruby I typically create one Error module and [...]]]></description>
			<content:encoded><![CDATA[<p>Based on the book <a href="http://exceptionalruby.com/">Exceptional Ruby</a> by Avdi Grimm, I have developed a strategy for how I should deal with exceptions in Ruby. </p>
<p>Being a very dynamic language, Ruby allows very flexible coding techniques.  Exceptions are not an exception <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>When I am developing a library in Ruby I typically create one <code>Error</code> module and one <code>StdError</code> class. The <code>Error</code> module is a typical <em>tag module</em> and does not contain any methods.</p>
<h2>Tag Module</h2>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Tag module for the Tapir library</font></i>
<b><font color="#0000FF">module</font></b> Tapir
  <b><font color="#0000FF">module</font></b> Error
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>The reason for the tag module is that I can use it to tag exceptions<br />
occurring inside my library without having to wrap them in a nested<br />
exception.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir
  <b><font color="#0000FF">class</font></b> Downloader

    <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>get url
      HTTP<font color="#990000">.</font>get url
    <b><font color="#0000FF">rescue</font></b> StandardError <font color="#990000">-&gt;</font> error   <i><font color="#9A1900"># Rescue the error</font></i>
      <i><font color="#9A1900"># Namespace the error by tagging it with ::Tapir::Error</font></i>
      error<font color="#990000">.</font>extend<font color="#990000">(::</font>Tapir<font color="#990000">::</font>Error<font color="#990000">)</font>
      <b><font color="#0000FF">raise</font></b>                         <i><font color="#9A1900"># And raise it again</font></i>
    <b><font color="#0000FF">end</font></b> 

  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client usage</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">::</font>Downloader<font color="#990000">.</font>get <font color="#FF0000">'http://non.existent.url/'</font>
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>Error <font color="#990000">=&gt;</font> error
  puts <font color="#FF0000">"Stupid tapir, gave me error #{error.message}"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>This is beautiful. I am scoping an internal error as my own. Since Ruby<br />
is dynamic there is no need to declare a new class that wraps all the<br />
methods in the <code>StandarError</code> I have access to them anyway. Duck typing<br />
for the win!</p>
<h2>A Nested Exception Class</h2>
<p>In some cases the tag module is not enough. Perhaps the exception was<br />
not created by another exception. In that case I need a real class since<br />
it is not possible to raise modules. But while I am at it I usually make<br />
the class a nested exception in order to simplify wrapping of other<br />
exceptions if the need comes up. This is how I do that.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir
  <i><font color="#9A1900"># I usually call the class `StdError` since it prevents the user of</font></i>
  <i><font color="#9A1900"># the library from rescuing the global `StandardError`.</font></i>
  <b><font color="#0000FF">class</font></b> StdError <font color="#990000">&lt;</font> StandardError
    extend Error             <i><font color="#9A1900"># Extend the Error tag module</font></i>
    attr_reader <font color="#990000">:</font>original    <i><font color="#9A1900"># Add an accessor for the original, if one exists</font></i> 

    <i><font color="#9A1900"># Create the error with a message and an original that defaults to</font></i>
    <i><font color="#9A1900"># the exception that is currently active, in this thread, if one exists</font></i>
    <b><font color="#0000FF">def</font></b> initialize<font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">=</font>$<font color="#990000">!)</font>
      <b><font color="#0000FF">super</font></b><font color="#990000">(</font>msg<font color="#990000">)</font>
      <font color="#009900">@original</font> <font color="#990000">=</font> original<font color="#990000">;</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client Usage</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">.</font>do_something_that_fails
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>Error <font color="#990000">=&gt;</font> error      <i><font color="#9A1900"># rescue the tag module</font></i>
  puts <font color="#FF0000">"Bad tapir #{error.message}, due to #{error.original.message}"</font>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># or if I want to be more specific</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">.</font>do_something_that_fails
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>StdError <font color="#990000">=&gt;</font> error   <i><font color="#9A1900"># rescue the specific error</font></i>
  puts <font color="#FF0000">"Bad tapir #{error.message}, due to #{error.original.message}"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>Notice that I don&rsquo;t have to wrap the exception explicitly, since<br />
I default the Exception to the last error that is stored in <code>$!</code>. </p>
<p>Now the only reason for me to want to create a <code>Tapir::StdError</code> apart<br />
from it being misuse of my library is if I want to add additional information<br />
to the exception that already occurred. In that case I may also want to<br />
extend the <code>Tapir::StdError</code> and create an exception with additional<br />
fields.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir

  <i><font color="#9A1900"># Create a specific exception to add more information for the client</font></i>
  <b><font color="#0000FF">class</font></b> TooOldError <font color="#990000">&lt;</font> StdError
    attr_reader <font color="#990000">:</font>age<font color="#990000">,</font> <font color="#990000">:</font>max_age

    <b><font color="#0000FF">def</font></b> initialize<font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">=</font>$<font color="#990000">!,</font> age<font color="#990000">,</font> max_age<font color="#990000">)</font>
      <b><font color="#0000FF">super</font></b><font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">)</font>
      <font color="#009900">@age</font><font color="#990000">,</font> <font color="#009900">@max_age</font> <font color="#990000">=</font> age<font color="#990000">,</font> max_age
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client usage</font></i>
<b><font color="#0000FF">begin</font></b>
  tapir<font color="#990000">.</font>mate<font color="#990000">(</font>other_tapir<font color="#990000">)</font>
<b><font color="#0000FF">rescue</font></b> TooOldError <font color="#990000">=&gt;</font> error
  <i><font color="#9A1900"># Use the specific error properties</font></i>
  puts <font color="#FF0000">"Hey, your are #{error.age}, that is too damn old!"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<h2>Throw &ndash; Catch</h2>
<p>Ruby also has an alternative to <code>raise</code> and <code>rescue</code> called <code>throw</code> and<br />
<code>catch</code>.</p>
<p>They should not be used as an alternative to exceptions, instead they are<br />
<a href="http://en.wikipedia.org/wiki/Continuation#Kinds_of_continuations">escape continuations</a><br />
that should be used to escape from nested control structures across<br />
method calls. Powerful! Here is an example from <a href="http://www.sinatrarb.com/">Sinatra</a></p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Here is the throw</font></i> 

   <i><font color="#9A1900"># Pass control to the next matching route.</font></i>
    <i><font color="#9A1900"># If there are no more matching routes, Sinatra will</font></i>
    <i><font color="#9A1900"># return a 404 response.</font></i>
    <b><font color="#0000FF">def</font></b> pass<font color="#990000">(&amp;</font>block<font color="#990000">)</font>
      <b><font color="#0000FF">throw</font></b> <font color="#990000">:</font>pass<font color="#990000">,</font> block
    <b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># and here is where it is caught</font></i> 

    <b><font color="#0000FF">def</font></b> process_route<font color="#990000">(</font>pattern<font color="#990000">,</font> keys<font color="#990000">,</font> conditions<font color="#990000">)</font>
      <font color="#990000">...</font>
      <b><font color="#0000FF">catch</font></b><font color="#990000">(:</font>pass<font color="#990000">)</font> <b><font color="#0000FF">do</font></b>
        conditions<font color="#990000">.</font>each <font color="#FF0000">{</font> <font color="#990000">|</font>cond<font color="#990000">|</font>
          <b><font color="#0000FF">throw</font></b> <font color="#990000">:</font>pass <b><font color="#0000FF">if</font></b> instance_eval<font color="#990000">(&amp;</font>cond<font color="#990000">)</font> <font color="#990000">==</font> <b><font color="#0000FF">false</font></b> <font color="#FF0000">}</font>
        <b><font color="#0000FF">yield</font></b>
      <b><font color="#0000FF">end</font></b>
    <b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Allowing usage such as</font></i> 

  get <font color="#FF0000">'/guess/:who'</font> <b><font color="#0000FF">do</font></b>
    pass <b><font color="#0000FF">unless</font></b> params<font color="#990000">[:</font>who<font color="#990000">]</font> <font color="#990000">==</font> <font color="#FF0000">'Frank'</font>
    <font color="#FF0000">'You got me!'</font>
  <b><font color="#0000FF">end</font></b> 

  get <font color="#FF0000">'/guess/*'</font> <b><font color="#0000FF">do</font></b>
    <font color="#FF0000">'You missed!'</font>
  <b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>Lovely!</p>
<h2>Wrap up</h2>
<p>This is how I use exceptions in Ruby now, thanks to ideas from the book.<br />
Other good ideas from the book are the three guarantees:</p>
<ul>
<li><em>The weak guarantee</em>, if an exception is raised, the object will be in<br />
a consistent state.</li>
<li><em>The strong guarantee</em>, if an exception is raised, the object will be<br />
left in its initial state.</li>
<li><em>The nothrow guarantee</em>, no exceptions will be raised by this method.</li>
</ul>
<p>And a nice way of categorizing exceptions based on three different<br />
usages by the client. (My categories are not exactly the same as Avdis)</p>
<ul>
<li><em>User Error</em>, the client has used the library wrong.</li>
<li><em>Internal Error</em>, something is wrong with the library. We are looking<br />
into the problem&hellip;</li>
<li><em>Transient Error</em>, something is now working right now, but the same<br />
call may succeed in a while. It is a good idea to provide a period<br />
after whick the call will probably succeed.<br />
the client to try again.</li>
</ul>
<p>It is a great book which contains a lot more information than I covered<br />
here. Get it, it is well worth the money.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Not Very Short Introduction To Node.js</title>
		<link>http://blog.jayway.com/2011/05/15/a-not-very-short-introduction-to-node-js/</link>
		<comments>http://blog.jayway.com/2011/05/15/a-not-very-short-introduction-to-node-js/#comments</comments>
		<pubDate>Sun, 15 May 2011 08:04:15 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8393</guid>
		<description><![CDATA[Node.js is a set of asynchronous libraries, built on top of the Google V8 Javascript engine. Node is used for server side development in Javascript. Do you feel the rush of the 90's coming through your head. It is not the revival of LiveWire, Node is a different beast. Node is a single threaded process, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nodejs.org/">Node.js</a> is a set of asynchronous libraries, built on top of the <a href="http://code.google.com/p/v8/"> Google V8 Javascript engine</a>. Node is used for server side development in Javascript. Do you feel the rush of the 90's coming through your head. It is not the revival of <a href="http://docsrv.sco.com/INT_LiveWire/CONTENTS.html">LiveWire</a>, Node is a different beast. Node is a single threaded process, focused on doing networking right. Right, in this case, means without blocking I/O. </p>
<p>All the libraries built for Node use non-blocking I/O. This is a really cool feature, which allows the single thread in Node to serve thousands of request per second. It even lets you run multiple servers in the same thread. Check out the performance characteristics of Nginx and Apache that utilize the same technique.</p>
<p><img src="http://www.webfaction.com/blog/nginx-apache-reqs-sec.png" title="Concurrency x Requests" alt="Concurrency x Requests" /></p>
<p>The graph for memory usage is even better.</p>
<p><img src="http://www.webfaction.com/blog/nginx-apache-memory.png" title="Concurrency x Memory" alt="Concurrency x Memory" /></p>
<p>Read more about it at the <a href="http://blog.webfaction.com/a-little-holiday-present">Web Faction Blog</a></p>
<p>OK, so what's the catch? The catch is that all code that does I/O, or<br />
anything slow at all, has to be called in an asynchronous style.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Synchronous</font></i>
<b><font color="#0000FF">var</font></b> result <font color="#990000">=</font> db<font color="#990000">.</font><b><font color="#000000">query</font></b><font color="#990000">(</font><font color="#FF0000">"select * from T"</font><font color="#990000">);</font>
<i><font color="#9A1900">// Use result</font></i> 

<i><font color="#9A1900">// Asynchronous</font></i>
db<font color="#990000">.</font><b><font color="#000000">query</font></b><font color="#990000">(</font><font color="#FF0000">"select * from T"</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b> <font color="#990000">(</font>result<font color="#990000">)</font> <font color="#FF0000">{</font>
    <i><font color="#9A1900">// Use result</font></i>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<p>So, all libraries that deal with IO has to be re-implemented with this<br />
style of programming. The good news is that even though Node has only<br />
been around for a couple of years, there are more than 1800 libraries<br />
available. The libraries are of varying quality but the popularity of<br />
Node shows good promise to deliver high-quality libraries for anything<br />
that you can imagine.</p>
<h2>History</h2>
<p>Node is definitely not the first of its kind. The non-blocking<br />
<code>select()</code> loop, that is at the heart of Node, dates back to 1983.</p>
<p><a href="http://twistedmatrix.com/trac/">Twisted</a> appeared in Python (2002) and <a href="http://rubyeventmachine.com/">EventMachine</a> in Ruby (2003).</p>
<p>This year a couple of newcomers appeared.</p>
<p><a href="http://postrank-labs.github.com/goliath/">Goliath</a>, which builds on EventMachine, and uses fibers to allow us to program in an synchronous style even though it is asynchronous under the hood.</p>
<p>And, the <a href="http://msdn.microsoft.com/en-us/vstudio/gg316360">Async Framework in<br />
.Net</a>, which enhances the compiler with the keywords <code>async</code> and <code>await</code> to allow for very elegant asynchronous programming.</p>
<h2>Get Started</h2>
<p>This example uses OSX as an example platform, if you use something else<br />
you will have to google for instructions.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Install Node using Homebrew</font></i>
$ brew install node
<font color="#990000">==&gt;</font> Downloading http<font color="#990000">:</font>//nodejs<font color="#990000">.</font>org/dist/node-v<font color="#993399">0.4</font><font color="#990000">.</font><font color="#993399">7</font><font color="#990000">.</font>tar<font color="#990000">.</font>gz
<i><font color="#9A1900">######################################################################## 100.0%</font></i>
<font color="#990000">==&gt;</font> <font color="#990000">.</font>/configure --prefix<font color="#990000">=</font>/usr/local/Cellar/node<font color="#990000">/</font><font color="#993399">0.4</font><font color="#990000">.</font><font color="#993399">7</font>
<font color="#990000">==&gt;</font> make install
<font color="#990000">==&gt;</font> Caveats
Please add /usr/local/lib/node to your NODE_PATH environment variable to have node libraries picked up<font color="#990000">.</font>
<font color="#990000">==&gt;</font> Summary
/usr/local/Cellar/node<font color="#990000">/</font><font color="#993399">0.4</font><font color="#990000">.</font><font color="#993399">7</font><font color="#990000">:</font> <font color="#993399">72</font> files<font color="#990000">,</font> <font color="#993399">7</font><font color="#990000">.</font>5M<font color="#990000">,</font> built <b><font color="#0000FF">in</font></b> <font color="#993399">1.2</font> minutes
</tt></pre>
<p>When installed you have access to the <code>node</code> command-line command. When<br />
invoked without arguments, it start a REPL.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ node
<font color="#990000">&gt;</font> <b><font color="#000000">function hello</font></b><font color="#990000">(</font>name<font color="#990000">)</font> {
<font color="#990000">...</font> <b><font color="#0000FF">return</font></b> <font color="#FF0000">'hello '</font> <font color="#990000">+</font> name<font color="#990000">;</font>
<font color="#990000">...</font> }
<font color="#990000">&gt;</font> hello<font color="#990000">(</font><font color="#FF0000">'tapir'</font><font color="#990000">)</font>
<font color="#FF0000">'hello tapir'</font>
<font color="#990000">&gt;</font>
</tt></pre>
<p>When invoked with a script it runs the script.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// hello.js</font></i>
<b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font><font color="#FF0000">'Tapir'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">2000</font><font color="#990000">);</font>
console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font><font color="#FF0000">'Hello'</font><font color="#990000">);</font> 

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ node hello<font color="#990000">.</font>js
Hello
<font color="#990000">...</font>
Tapir
</tt></pre>
<h2>Networking</h2>
<p>As I mentioned above, Node is focused on networking. That means it<br />
should be easy to write networking code. Here is a simple echo server.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Echo Server</font></i>
<b><font color="#0000FF">var</font></b> net <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'net'</font><font color="#990000">);</font> 

<b><font color="#0000FF">var</font></b> server <font color="#990000">=</font> net<font color="#990000">.</font><b><font color="#000000">createServer</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>socket<font color="#990000">)</font> <font color="#FF0000">{</font>
    socket<font color="#990000">.</font><b><font color="#000000">on</font></b><font color="#990000">(</font><font color="#FF0000">'data'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>data<font color="#990000">)</font> <font color="#FF0000">{</font>
        console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font>data<font color="#990000">.</font><b><font color="#000000">toString</font></b><font color="#990000">());</font>
        socket<font color="#990000">.</font><b><font color="#000000">write</font></b><font color="#990000">(</font>data<font color="#990000">);</font>
    <font color="#FF0000">}</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
server<font color="#990000">.</font><b><font color="#000000">listen</font></b><font color="#990000">(</font><font color="#993399">4000</font><font color="#990000">);</font> 

</tt></pre>
<p>And here is a simple HTTP server.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// HTTP Server</font></i>
<b><font color="#0000FF">var</font></b> http <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'http'</font><font color="#990000">);</font>
<b><font color="#0000FF">var</font></b> web <font color="#990000">=</font> http<font color="#990000">.</font><b><font color="#000000">createServer</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>request<font color="#990000">,</font> response<font color="#990000">)</font> <font color="#FF0000">{</font>
  response<font color="#990000">.</font><b><font color="#000000">writeHead</font></b><font color="#990000">(</font><font color="#993399">200</font><font color="#990000">,</font> <font color="#FF0000">{</font>
    <font color="#FF0000">'Content-Type'</font><font color="#990000">:</font> <font color="#FF0000">'text/plain'</font>
  <font color="#FF0000">}</font><font color="#990000">);</font>
  response<font color="#990000">.</font><b><font color="#000000">end</font></b><font color="#990000">(</font><font color="#FF0000">'Tapirs are beautiful!</font><font color="#CC33CC">\n</font><font color="#FF0000">'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
web<font color="#990000">.</font><b><font color="#000000">listen</font></b><font color="#990000">(</font><font color="#993399">4001</font><font color="#990000">);</font> 

</tt></pre>
<p>Quite similar. A cool thing is that the servers can be started from the<br />
same file and node will, happily, serve both HTTP and echo requests from<br />
the same thread without any problems. Let's try them out!</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># curl the http service</font></i>
$ curl localhost<font color="#990000">:</font><font color="#993399">4001</font>
Tapirs are beautiful<font color="#990000">!</font> 

<i><font color="#9A1900"># use netcat to send the string to the echo server</font></i>
$ echo <font color="#FF0000">'Hello beautiful tapir'</font> <font color="#990000">|</font> nc localhost <font color="#993399">4000</font>
Hello beautiful tapir

</tt></pre>
<h2>Modules</h2>
<p>Node comes with a selection of built in modules. Ryan Dahl says that<br />
they try to keep the core small, but even so the built-in modules cover<br />
a lot of useful functionality.</p>
<ul>
<li>net - contains tcp/ip related networking functionality.</li>
<li>http - contains functionality for dealing with the HTTP protocol.</li>
<li>util - holds common utility functions, such as log, inherits, pump,<br />
...</li>
<li>fs - contains filesystem related functionality, remember that<br />
everything should be asynchronous.</li>
<li>events - contains the EventEmitter that is used for dealing with<br />
events in a consistent way. It is used internally but it can be used<br />
externally too.</li>
</ul>
<h3>An example</h3>
<p>Here is an example of a simple module.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// module tapir.js</font></i> 

<i><font color="#9A1900">// require another module</font></i>
<b><font color="#0000FF">var</font></b> util <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'util'</font><font color="#990000">);</font> 

<b><font color="#0000FF">function</font></b> <b><font color="#000000">eat</font></b><font color="#990000">(</font>food<font color="#990000">)</font> <font color="#FF0000">{</font>
  util<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font><font color="#FF0000">'eating '</font><font color="#990000">+</font> food<font color="#990000">);</font>
<font color="#FF0000">}</font> 

<i><font color="#9A1900">// export a function</font></i>
exports<font color="#990000">.</font>eat <font color="#990000">=</font> eat<font color="#990000">;</font> 

</tt></pre>
<p>As you can see it looks like a normal Javascript file and it even looks<br />
like it has global variables. It doesn't. When a module is loaded it is<br />
wrapped in code, similar to this.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">var</font></b> module <font color="#990000">=</font> <font color="#FF0000">{</font> exports<font color="#990000">:</font> <font color="#FF0000">{}}</font><font color="#990000">;</font>
<font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>module<font color="#990000">,</font> exports<font color="#990000">)</font><font color="#FF0000">{</font>
  <i><font color="#9A1900">// module code from file</font></i>
  <font color="#990000">...</font>
<font color="#FF0000">}</font><font color="#990000">)(</font>module<font color="#990000">,</font> module<font color="#990000">.</font>exports<font color="#990000">);</font> 

</tt></pre>
<p>As you can see the code is wrapped in a function and an empty object<br />
with an <code>export</code> property is sent into it. This is used by the file to<br />
export only the functions that it want to publish.</p>
<p>The <code>require</code> function works in symphony with the module and it returns<br />
the exported functions to the caller.</p>
<h3>Node Package Manager, npm</h3>
<p>To allow simple handling of third-party packages, Node uses <code>npm</code>. It<br />
can be installed like this:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ curl http<font color="#990000">:</font>//npmjs<font color="#990000">.</font>org/install<font color="#990000">.</font>sh <font color="#990000">|</font> sh
<font color="#990000">...</font> 

</tt></pre>
<p>And used like this:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ npm install -g express
mime@<font color="#993399">1.2</font><font color="#990000">.</font><font color="#993399">1</font> /usr/local/lib/node_modules/express/node_modules/mime
connect@<font color="#993399">1.4</font><font color="#990000">.</font><font color="#993399">0</font> /usr/local/lib/node_modules/express/node_modules/connect
qs@<font color="#993399">0.1</font><font color="#990000">.</font><font color="#993399">0</font> /usr/local/lib/node_modules/express/node_modules/qs
/usr/local/bin/express -<font color="#990000">&gt;</font> /usr/local/lib/node_modules/express/bin/express
express@<font color="#993399">2.3</font><font color="#990000">.</font><font color="#993399">2</font> /usr/local/lib/node_modules/express

</tt></pre>
<p>As you can see, installing a module also installs its dependencies. This<br />
works because a module can be package with meta-data, like so:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// express/package.json</font></i>
<font color="#FF0000">{</font>
  <font color="#FF0000">"name"</font><font color="#990000">:</font> <font color="#FF0000">"express"</font><font color="#990000">,</font>
  <font color="#FF0000">"description"</font><font color="#990000">:</font> <font color="#FF0000">"Sinatra inspired web development framework"</font><font color="#990000">,</font>
  <font color="#FF0000">"version"</font><font color="#990000">:</font> <font color="#FF0000">"2.3.2"</font><font color="#990000">,</font>
  <font color="#FF0000">"author"</font><font color="#990000">:</font> <font color="#FF0000">"TJ Holowaychuk &lt;tj@vision-media.ca&gt;"</font><font color="#990000">,</font>
  <font color="#FF0000">"contributors"</font><font color="#990000">:</font> <font color="#990000">[</font>
    <font color="#FF0000">{</font> <font color="#FF0000">"name"</font><font color="#990000">:</font> <font color="#FF0000">"TJ Holowaychuk"</font><font color="#990000">,</font> <font color="#FF0000">"email"</font><font color="#990000">:</font> <font color="#FF0000">"tj@vision-media.ca"</font> <font color="#FF0000">}</font><font color="#990000">,</font>
    <font color="#FF0000">{</font> <font color="#FF0000">"name"</font><font color="#990000">:</font> <font color="#FF0000">"Guillermo Rauch"</font><font color="#990000">,</font> <font color="#FF0000">"email"</font><font color="#990000">:</font> <font color="#FF0000">"rauchg@gmail.com"</font> <font color="#FF0000">}</font>
  <font color="#990000">],</font>
  <font color="#FF0000">"dependencies"</font><font color="#990000">:</font> <font color="#FF0000">{</font>
    <font color="#FF0000">"connect"</font><font color="#990000">:</font> <font color="#FF0000">"&gt;= 1.4.0 &lt; 2.0.0"</font><font color="#990000">,</font>
    <font color="#FF0000">"mime"</font><font color="#990000">:</font> <font color="#FF0000">"&gt;= 0.0.1"</font><font color="#990000">,</font>
    <font color="#FF0000">"qs"</font><font color="#990000">:</font> <font color="#FF0000">"&gt;= 0.0.6"</font>
  <font color="#FF0000">}</font><font color="#990000">,</font>
  <font color="#FF0000">"keywords"</font><font color="#990000">:</font> <font color="#990000">[</font><font color="#FF0000">"framework"</font><font color="#990000">,</font> <font color="#FF0000">"sinatra"</font><font color="#990000">,</font> <font color="#FF0000">"web"</font><font color="#990000">,</font> <font color="#FF0000">"rest"</font><font color="#990000">,</font> <font color="#FF0000">"restful"</font><font color="#990000">],</font>
  <font color="#FF0000">"repository"</font><font color="#990000">:</font> <font color="#FF0000">"git://github.com/visionmedia/express"</font><font color="#990000">,</font>
  <font color="#FF0000">"main"</font><font color="#990000">:</font> <font color="#FF0000">"index"</font><font color="#990000">,</font>
  <font color="#FF0000">"bin"</font><font color="#990000">:</font> <font color="#FF0000">{</font> <font color="#FF0000">"express"</font><font color="#990000">:</font> <font color="#FF0000">"./bin/express"</font> <font color="#FF0000">}</font><font color="#990000">,</font>
  <font color="#FF0000">"engines"</font><font color="#990000">:</font> <font color="#FF0000">{</font> <font color="#FF0000">"node"</font><font color="#990000">:</font> <font color="#FF0000">"&gt;= 0.4.1 &lt; 0.5.0"</font> <font color="#FF0000">}</font>
<font color="#FF0000">}</font> 

</tt></pre>
<p>The <code>package.json</code> contains information about who made the module, its<br />
dependencies, along with some additional information to enable better<br />
searching facilities.</p>
<p>Npm installs the modules from <a href="http://npm.mape.me/">a common<br />
respository</a>, which contains more than 1800<br />
modules.</p>
<h3>Noteworthy Modules</h3>
<p><em>Express</em> is probably the most used of all third-party modules. It is<br />
a Sinatra clone and it is very good, just like Sinatra.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Create a server</font></i>
<b><font color="#0000FF">var</font></b> app <font color="#990000">=</font> express<font color="#990000">.</font><b><font color="#000000">createServer</font></b><font color="#990000">();</font>
app<font color="#990000">.</font><b><font color="#000000">listen</font></b><font color="#990000">(</font><font color="#993399">4000</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Mount the root (/) and redirect to index</font></i>
app<font color="#990000">.</font><b><font color="#000000">get</font></b><font color="#990000">(</font><font color="#FF0000">'/'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>req<font color="#990000">,</font> res<font color="#990000">)</font> <font color="#FF0000">{</font>
  res<font color="#990000">.</font><b><font color="#000000">redirect</font></b><font color="#990000">(</font><font color="#FF0000">'/index.html'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Handle a post to /quiz</font></i>
app<font color="#990000">.</font><b><font color="#000000">post</font></b><font color="#990000">(</font><font color="#FF0000">'/quiz'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>req<font color="#990000">,</font> res<font color="#990000">)</font> <font color="#FF0000">{</font>
  res<font color="#990000">.</font><b><font color="#000000">send</font></b><font color="#990000">(</font>quiz<font color="#990000">.</font><b><font color="#000000">create</font></b><font color="#990000">().</font>id<font color="#990000">.</font><b><font color="#000000">toString</font></b><font color="#990000">());</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<p>Express uses <em>Connect</em> to handle middleware. Middleware is like Rack<br />
but for Node (No wonder that Node is nice to work with when it borrows<br />
its ideas from Ruby <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#000000">connect</font></b><font color="#990000">(</font>
      <i><font color="#9A1900">// Add a logger</font></i>
      connect<font color="#990000">.</font><b><font color="#000000">logger</font></b><font color="#990000">()</font>
      <i><font color="#9A1900">// Serve static file from the current directory</font></i>
    <font color="#990000">,</font> connect<font color="#990000">.</font><b><font color="#0000FF">static</font></b><font color="#990000">(</font>__dirname<font color="#990000">)</font>
      <i><font color="#9A1900">// Compile Sass and Coffescript files, on the fly</font></i>
    <font color="#990000">,</font> connect<font color="#990000">.</font><b><font color="#000000">compiler</font></b><font color="#990000">(</font><font color="#FF0000">{</font>enable<font color="#990000">:</font> <font color="#990000">[</font><font color="#FF0000">'sass'</font><font color="#990000">,</font> <font color="#FF0000">'coffeescript'</font><font color="#990000">]</font><font color="#FF0000">}</font><font color="#990000">)</font>
      <i><font color="#9A1900">// Profile all requests</font></i>
    <font color="#990000">,</font> connect<font color="#990000">.</font><b><font color="#000000">profiler</font></b><font color="#990000">()</font>
  <font color="#990000">).</font><b><font color="#000000">listen</font></b><font color="#990000">(</font><font color="#993399">3000</font><font color="#990000">);</font>
</tt></pre>
<p>Another popular library is <em>Socket.IO</em>. It handles the usual socket<br />
variants, such as WebSocket, Comet, Flash Sockets, etc...</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">var</font></b> http <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'http'</font><font color="#990000">);</font>
<b><font color="#0000FF">var</font></b> io <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'socket.io'</font><font color="#990000">);</font> 

server <font color="#990000">=</font> http<font color="#990000">.</font><b><font color="#000000">createServer</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>req<font color="#990000">,</font> res<font color="#990000">)</font><font color="#FF0000">{</font><font color="#990000">...</font><font color="#FF0000">}</font><font color="#990000">);</font>
server<font color="#990000">.</font><b><font color="#000000">listen</font></b><font color="#990000">(</font><font color="#993399">80</font><font color="#990000">);</font> 

<i><font color="#9A1900">// socket.io attaches to an existing server</font></i>
<b><font color="#0000FF">var</font></b> socket <font color="#990000">=</font> io<font color="#990000">.</font><b><font color="#000000">listen</font></b><font color="#990000">(</font>server<font color="#990000">);</font>
socket<font color="#990000">.</font><b><font color="#000000">on</font></b><font color="#990000">(</font><font color="#FF0000">'connection'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>client<font color="#990000">)</font><font color="#FF0000">{</font>
  <i><font color="#9A1900">// new client is here!</font></i>
  client<font color="#990000">.</font><b><font color="#000000">on</font></b><font color="#990000">(</font><font color="#FF0000">'message'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> ... <font color="#FF0000">}</font><font color="#990000">)</font>
  client<font color="#990000">.</font><b><font color="#000000">on</font></b><font color="#990000">(</font><font color="#FF0000">'disconnect'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> ... <font color="#FF0000">}</font><font color="#990000">)</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<p><em>MySql</em> has a library for Node.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>client<font color="#990000">.</font><b><font color="#000000">query</font></b><font color="#990000">(</font>
  <font color="#FF0000">'SELECT * FROM '</font> <font color="#990000">+</font> TEST_TABLE<font color="#990000">,</font>
  <i><font color="#9A1900">// Note the callback style</font></i>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>err<font color="#990000">,</font> results<font color="#990000">,</font> fields<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>err<font color="#990000">)</font> <font color="#FF0000">{</font> <b><font color="#0000FF">throw</font></b> err<font color="#990000">;</font> <font color="#FF0000">}</font> 

    console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font>results<font color="#990000">);</font>
    console<font color="#990000">.</font><b><font color="#000000">log</font></b><font color="#990000">(</font>fields<font color="#990000">);</font>
    client<font color="#990000">.</font><b><font color="#000000">end</font></b><font color="#990000">();</font>
  <font color="#FF0000">}</font>
<font color="#990000">);</font>
</tt></pre>
<p>And <em>Mongoose</em> can be used for accessing MongoDB.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Declare the schema</font></i>
<b><font color="#0000FF">var</font></b> Schema <font color="#990000">=</font> mongoose<font color="#990000">.</font>Schema
  <font color="#990000">,</font> ObjectId <font color="#990000">=</font> Schema<font color="#990000">.</font>ObjectId<font color="#990000">;</font> 

<b><font color="#0000FF">var</font></b> BlogPost <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">Schema</font></b><font color="#990000">(</font><font color="#FF0000">{</font>
    author    <font color="#990000">:</font> ObjectId
  <font color="#990000">,</font> title     <font color="#990000">:</font> String
  <font color="#990000">,</font> body      <font color="#990000">:</font> String
  <font color="#990000">,</font> date      <font color="#990000">:</font> Date
<font color="#FF0000">}</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Use it</font></i>
<b><font color="#0000FF">var</font></b> BlogPost <font color="#990000">=</font> mongoose<font color="#990000">.</font><b><font color="#000000">model</font></b><font color="#990000">(</font><font color="#FF0000">'BlogPost'</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Save</font></i>
<b><font color="#0000FF">var</font></b> post <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">BlogPost</font></b><font color="#990000">();</font>
post<font color="#990000">.</font>author <font color="#990000">=</font> <font color="#FF0000">'Stravinsky'</font><font color="#990000">;</font>
instance<font color="#990000">.</font><b><font color="#000000">save</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b> <font color="#990000">(</font>err<font color="#990000">)</font> <font color="#FF0000">{</font>
  <i><font color="#9A1900">//</font></i>
<font color="#FF0000">}</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Find</font></i>
BlogPost<font color="#990000">.</font><b><font color="#000000">find</font></b><font color="#990000">(</font><font color="#FF0000">{}</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b> <font color="#990000">(</font>err<font color="#990000">,</font> docs<font color="#990000">)</font> <font color="#FF0000">{</font>
  <i><font color="#9A1900">// docs.forEach</font></i>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<h3>Templating Engines</h3>
<p>Everytime a new platform makes its presence, it brings along a couple of<br />
new templating languages and Node is no different. Along with the<br />
popular ones from the Ruby world, like Haml and Erb (EJS in Node),<br />
comes some new ones like Jade and some browser templating languages like<br />
Mustache and jQuery templates. I'll show examples of Jade and Mu<br />
(Mustache for Node).</p>
<p>I like <em>Jade</em>, because it is a Javascript dialect of Haml and it seems<br />
appropriate to use if I'm using Javascript on the server side.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><font color="#990000">!!!</font> <font color="#993399">5</font>
<b><font color="#000000">html</font></b><font color="#990000">(</font>lang<font color="#990000">=</font><font color="#FF0000">"en"</font><font color="#990000">)</font>
  head
    title<font color="#990000">=</font> pageTitle
    <b><font color="#000000">script</font></b><font color="#990000">(</font>type<font color="#990000">=</font><font color="#FF0000">'text/javascript'</font><font color="#990000">)</font>
      <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>foo<font color="#990000">)</font> <font color="#990000">{</font>
        <b><font color="#000000">bar</font></b><font color="#990000">()</font>
      <font color="#990000">}</font>
  body
    h1 Jade <font color="#990000">-</font> node template engine
    <i><font color="#9A1900">#container</font></i>
      <font color="#990000">-</font> <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>youAreUsingJade<font color="#990000">)</font>
        p You are amazing
      <font color="#990000">-</font> <b><font color="#0000FF">else</font></b>
        p Get on it<font color="#990000">!</font>
</tt></pre>
<p>I'm not really sure if I like Mustache or not, but I can surely see the<br />
value of having a templating language which works both on the server side<br />
and in the browser.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">&lt;h1&gt;</font></b>{{header}}<b><font color="#0000FF">&lt;/h1&gt;</font></b>
{{#bug}}
{{/bug}}

{{#items}}
  {{#first}}
    <b><font color="#0000FF">&lt;li&gt;&lt;strong&gt;</font></b>{{name}}<b><font color="#0000FF">&lt;/strong&gt;&lt;/li&gt;</font></b>
  {{/first}}
  {{#link}}
    <b><font color="#0000FF">&lt;li&gt;&lt;a</font></b> <font color="#009900">href</font><font color="#990000">=</font><font color="#FF0000">"{{url}}"</font><b><font color="#0000FF">&gt;</font></b>{{name}}<b><font color="#0000FF">&lt;/a&gt;&lt;/li&gt;</font></b>
  {{/link}}
{{/items}}

{{#empty}}
  <b><font color="#0000FF">&lt;p&gt;</font></b>The list is empty.<b><font color="#0000FF">&lt;/p&gt;</font></b>
{{/empty}}
</tt></pre>
<h2>Testing</h2>
<p>Node comes with assertions built in, and all testing frameworks build on<br />
the Assert module, so it is good to know.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>assert<font color="#990000">.</font><b><font color="#000000">ok</font></b><font color="#990000">(</font>value<font color="#990000">,</font> <font color="#990000">[</font>message<font color="#990000">]);</font>
assert<font color="#990000">.</font><b><font color="#000000">equal</font></b><font color="#990000">(</font>actual<font color="#990000">,</font> expected<font color="#990000">,</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#000000">notEqual</font></b><font color="#990000">(</font>actual<font color="#990000">,</font> expected<font color="#990000">,</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#000000">deepEqual</font></b><font color="#990000">(</font>actual<font color="#990000">,</font> expected<font color="#990000">,</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#000000">strictEqual</font></b><font color="#990000">(</font>actual<font color="#990000">,</font> expected<font color="#990000">,</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#0000FF">throws</font></b><font color="#990000">(</font>block<font color="#990000">,</font> <font color="#990000">[</font>error<font color="#990000">],</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#000000">doesNotThrow</font></b><font color="#990000">(</font>block<font color="#990000">,</font> <font color="#990000">[</font>error<font color="#990000">],</font> <font color="#990000">[</font>message<font color="#990000">])</font>
assert<font color="#990000">.</font><b><font color="#000000">ifError</font></b><font color="#990000">(</font>value<font color="#990000">)</font>
assert<font color="#990000">.</font><b><font color="#000000">fail</font></b><font color="#990000">(</font>actual<font color="#990000">,</font> expected<font color="#990000">,</font> message<font color="#990000">,</font> operator<font color="#990000">)</font> 

<i><font color="#9A1900">// Example</font></i>
<i><font color="#9A1900">// assert.throws(function, regexp)</font></i>
assert<font color="#990000">.</font><b><font color="#0000FF">throws</font></b><font color="#990000">(</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font> <b><font color="#0000FF">throw</font></b> <b><font color="#0000FF">new</font></b> <b><font color="#000000">Error</font></b><font color="#990000">(</font><font color="#FF0000">"Wrong value"</font><font color="#990000">);</font> <font color="#FF0000">}</font><font color="#990000">,</font>
  <font color="#FF6600">/value/</font>
<font color="#990000">);</font>
</tt></pre>
<p>Apart from that there are at least 30 different testing frameworks to<br />
use. I have chosen to use NodeUnit since I find that it handles<br />
asynchronous testing well, and it has a nice UTF-8 output that looks<br />
good in the terminal,</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// ./test/test-doubled.js</font></i>
<b><font color="#0000FF">var</font></b> doubled <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'../lib/doubled'</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Exported functions are run by the test runner</font></i>
exports<font color="#990000">[</font><font color="#FF0000">'calculate'</font><font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#0000FF">function</font></b> <font color="#990000">(</font>test<font color="#990000">)</font> <font color="#FF0000">{</font>
    test<font color="#990000">.</font><b><font color="#000000">equal</font></b><font color="#990000">(</font>doubled<font color="#990000">.</font><b><font color="#000000">calculate</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">),</font> <font color="#993399">4</font><font color="#990000">);</font>
    test<font color="#990000">.</font><b><font color="#000000">done</font></b><font color="#990000">();</font>
<font color="#FF0000">}</font><font color="#990000">;</font> 

<i><font color="#9A1900">// An asynchronous test</font></i>
exports<font color="#990000">[</font><font color="#FF0000">'read a number'</font><font color="#990000">]</font> <font color="#990000">=</font> <b><font color="#0000FF">function</font></b> <font color="#990000">(</font>test<font color="#990000">)</font> <font color="#FF0000">{</font>
    test<font color="#990000">.</font><b><font color="#000000">expect</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font> <i><font color="#9A1900">// Make sure the assertion is run</font></i> 

    <b><font color="#0000FF">var</font></b> ev <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> events<font color="#990000">.</font><b><font color="#000000">EventEmitter</font></b><font color="#990000">();</font>
    process<font color="#990000">.</font>openStdin <font color="#990000">=</font> <b><font color="#0000FF">function</font></b> <font color="#990000">()</font> <font color="#FF0000">{</font> <b><font color="#0000FF">return</font></b> ev<font color="#990000">;</font> <font color="#FF0000">}</font><font color="#990000">;</font>
    process<font color="#990000">.</font>exit <font color="#990000">=</font> test<font color="#990000">.</font>done<font color="#990000">;</font> 

    console<font color="#990000">.</font>log <font color="#990000">=</font> <b><font color="#0000FF">function</font></b> <font color="#990000">(</font>str<font color="#990000">)</font> <font color="#FF0000">{</font>
        test<font color="#990000">.</font><b><font color="#000000">equal</font></b><font color="#990000">(</font>str<font color="#990000">,</font> <font color="#FF0000">'Doubled: 24'</font><font color="#990000">);</font>
    <font color="#FF0000">}</font><font color="#990000">;</font> 

    doubled<font color="#990000">.</font><b><font color="#000000">read</font></b><font color="#990000">();</font>
    ev<font color="#990000">.</font><b><font color="#000000">emit</font></b><font color="#990000">(</font><font color="#FF0000">'data'</font><font color="#990000">,</font> <font color="#FF0000">'12'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">;</font> 

</tt></pre>
<p><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/nodeunit.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/nodeunit.png" alt="" title="nodeunit" width="245" height="156" class="aligncenter size-full wp-image-8395" /></a></p>
<h2>Deployment</h2>
<p>There are already a lot of platforms providing Node as a service (PaaS<br />
, Platform as a Service). Most of them are using<br />
<a href="http://heroku.com">Heroku</a> style deployment by pushing to a Git remote.<br />
I'll show three alternatives that all provide free Node hosting.</p>
<h3>Joyent (no.de)</h3>
<p>Joyent, the employers of Ryan Dahl, give you <code>ssh</code> access so that you<br />
can install the modules you need. Deployment is done by pushing to<br />
a Git remote.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ ssh node@my-machine<font color="#990000">.</font>no<font color="#990000">.</font>de
$ nmp install express
$ git remote add node node@andersjanmyr<font color="#990000">.</font>no<font color="#990000">.</font>de<font color="#990000">:</font>repo
$ git push node master
Counting objects<font color="#990000">:</font> <font color="#993399">5</font><font color="#990000">,</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Delta compression using up to <font color="#993399">2</font> threads<font color="#990000">.</font>
Compressing objects<font color="#990000">:</font> <font color="#993399">100</font><font color="#990000">%</font> <font color="#990000">(</font><font color="#993399">3</font><font color="#990000">/</font><font color="#993399">3</font><font color="#990000">),</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Writing objects<font color="#990000">:</font> <font color="#993399">100</font><font color="#990000">%</font> <font color="#990000">(</font><font color="#993399">3</font><font color="#990000">/</font><font color="#993399">3</font><font color="#990000">),</font> <font color="#993399">321</font> bytes<font color="#990000">,</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Total <font color="#993399">3</font> <font color="#990000">(</font>delta <font color="#993399">2</font><font color="#990000">),</font> reused <font color="#993399">0</font> <font color="#990000">(</font>delta <font color="#993399">0</font><font color="#990000">)</font>
remote<font color="#990000">:</font> Starting node v0<font color="#990000">.</font><font color="#993399">4.7</font><font color="#990000">...</font>
remote<font color="#990000">:</font> Successful
To node@andersjanmyr<font color="#990000">.</font>no<font color="#990000">.</font>de<font color="#990000">:</font>repo
  8f59169<font color="#990000">..</font>c1177b0  master -<font color="#990000">&gt;</font> master
</tt></pre>
<h3>Nodester</h3>
<p>Nodester, gives you a command line tool, <code>nodester</code>, that you use to<br />
install modules. Deployment by pushing to a Git remote.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ nodester npm install express
$ git push nodester master
Counting objects<font color="#990000">:</font> <font color="#993399">5</font><font color="#990000">,</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Delta compression using up to <font color="#993399">2</font> threads<font color="#990000">.</font>
Compressing objects<font color="#990000">:</font> <font color="#993399">100</font><font color="#990000">%</font> <font color="#990000">(</font><font color="#993399">3</font><font color="#990000">/</font><font color="#993399">3</font><font color="#990000">),</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Writing objects<font color="#990000">:</font> <font color="#993399">100</font><font color="#990000">%</font> <font color="#990000">(</font><font color="#993399">3</font><font color="#990000">/</font><font color="#993399">3</font><font color="#990000">),</font> <font color="#993399">341</font> bytes<font color="#990000">,</font> <b><font color="#0000FF">done</font></b><font color="#990000">.</font>
Total <font color="#993399">3</font> <font color="#990000">(</font>delta <font color="#993399">2</font><font color="#990000">),</font> reused <font color="#993399">0</font> <font color="#990000">(</font>delta <font color="#993399">0</font><font color="#990000">)</font>
remote<font color="#990000">:</font> Syncing repo with chroot
remote<font color="#990000">:</font> From /node/hosted_apps/andersjanmyr<font color="#990000">/</font><font color="#993399">1346</font>-7856c14e6a5d92a6b5374ec4772a6da0<font color="#990000">.</font>git<font color="#990000">/.</font>
remote<font color="#990000">:</font>    38f4e6e<font color="#990000">..</font>8f59169  master     -<font color="#990000">&gt;</font> origin/master
remote<font color="#990000">:</font> Updating 38f4e6e<font color="#990000">..</font>8f59169
remote<font color="#990000">:</font> Fast-forward
remote<font color="#990000">:</font>  Gemfile<font color="#990000">.</font>lock <font color="#990000">|</font>   <font color="#993399">10</font> <font color="#990000">++++</font>------
remote<font color="#990000">:</font>  <font color="#993399">1</font> files changed<font color="#990000">,</font> <font color="#993399">4</font> insertions<font color="#990000">(+),</font> <font color="#993399">6</font> deletions<font color="#990000">(</font>-<font color="#990000">)</font>
remote<font color="#990000">:</font> Checking <font color="#990000">./.</font>git/hooks/post-receive
remote<font color="#990000">:</font> Attempting to restart your app<font color="#990000">:</font> <font color="#993399">1346</font>-7856c14e6a5d92a6b5374ec4772a6da0
remote<font color="#990000">:</font> App restarted<font color="#990000">..</font>
remote<font color="#990000">:</font>
remote<font color="#990000">:</font>
remote<font color="#990000">:</font>     <font color="#990000">\</font>m<font color="#990000">/</font> Nodester out <font color="#990000">\</font>m<font color="#990000">/</font>
remote<font color="#990000">:</font>
remote<font color="#990000">:</font>
To ec2-user@nodester<font color="#990000">.</font>com<font color="#990000">:</font>/node/hosted_apps/andersjanmyr<font color="#990000">/</font><font color="#993399">1346</font>-7856c14e6a5d92a6b5374ec4772a6da0<font color="#990000">.</font>git
   38f4e6e<font color="#990000">..</font>8f59169  master -<font color="#990000">&gt;</font> master
</tt></pre>
<h3>Cloud Foundry</h3>
<p>Cloud Foundry is one of the most interesting platforms in the cloud.  It<br />
was genius by VM Ware to open source the platform, allowing anyone to<br />
set up their own cloud if they wish. If you don't want to setup your own<br />
Cloud Foundry Cloud, you can use the service hosted at<br />
cloundfoundry.com.</p>
<p>With Cloud Foundry, you install the modules locally and then they are<br />
automatically deployed as part of the <code>vmc push</code>. Push in this case does<br />
not mean <code>git push</code>, but instead, copy all the files from my local machine<br />
to the server.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ npm install express  <i><font color="#9A1900"># Install locally</font></i>
mime@<font color="#993399">1.2</font><font color="#990000">.</font><font color="#993399">1</font> <font color="#990000">.</font>/node_modules/express/node_modules/mime
connect@<font color="#993399">1.4</font><font color="#990000">.</font><font color="#993399">0</font> <font color="#990000">.</font>/node_modules/express/node_modules/connect
qs@<font color="#993399">0.1</font><font color="#990000">.</font><font color="#993399">0</font> <font color="#990000">.</font>/node_modules/express/node_modules/qs
express@<font color="#993399">2.3</font><font color="#990000">.</font><font color="#993399">0</font> <font color="#990000">.</font>/node_modules/express

$ vmc push
Would you like to deploy from the current directory<font color="#990000">?</font> <font color="#990000">[</font>Yn<font color="#990000">]:</font> Y
Application Name<font color="#990000">:</font> snake
Application Deployed URL<font color="#990000">:</font> <font color="#FF0000">'snake.cloudfoundry.com'</font><font color="#990000">?</font>
Detected a Node<font color="#990000">.</font>js Application<font color="#990000">,</font> is this correct<font color="#990000">?</font> <font color="#990000">[</font>Yn<font color="#990000">]:</font>
Memory Reservation <font color="#990000">[</font>Default<font color="#990000">:</font>64M<font color="#990000">]</font> <font color="#990000">(</font>64M<font color="#990000">,</font> 128M<font color="#990000">,</font> 256M<font color="#990000">,</font> 512M<font color="#990000">,</font> 1G or 2G<font color="#990000">)</font>
Creating Application<font color="#990000">:</font> OK
Would you like to <b><font color="#0000FF">bind</font></b> any services to <font color="#FF0000">'snake'</font><font color="#990000">?</font> <font color="#990000">[</font>yN<font color="#990000">]:</font>
Uploading Application<font color="#990000">:</font>
  Checking <b><font color="#0000FF">for</font></b> available resources<font color="#990000">:</font> OK
  Packing application<font color="#990000">:</font> OK
  Uploading <font color="#990000">(</font>1K<font color="#990000">):</font> OK
Push Status<font color="#990000">:</font> OK
Staging Application<font color="#990000">:</font> OK
Starting Application<font color="#990000">:</font> <font color="#990000">........</font>OK
</tt></pre>
<h2>Tools</h2>
<p>There are of course a bunch of tools that come with a new platform,<br />
Jake, is a Javascript version of Rake, but I am happy with Rake and<br />
I don't see the need to switch. But, there are some tools that I cannot<br />
live without when using Node.</p>
<h3>Reloaders</h3>
<p>If you use the vanilla <code>node</code> command then you have to restart it<br />
every time you make a change to a file. That is awfully annoying and<br />
there are already a number of solutions to the problem.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Nodemon watches the files in your directory and reloads them if necessary</font></i>
$ npm install nodemon
nodemon@<font color="#993399">0.3</font><font color="#990000">.</font><font color="#993399">2</font> <font color="#990000">..</font>/node_modules/nodemon

$ nodemon server<font color="#990000">.</font>js
<font color="#993399">30</font> Apr <font color="#993399">08</font><font color="#990000">:</font><font color="#993399">21</font><font color="#990000">:</font><font color="#993399">23</font> - <font color="#990000">[</font>nodemon<font color="#990000">]</font> running server<font color="#990000">.</font>js
<font color="#990000">...</font>
<i><font color="#9A1900"># Saving the file</font></i>
<font color="#993399">30</font> Apr <font color="#993399">08</font><font color="#990000">:</font><font color="#993399">22</font><font color="#990000">:</font><font color="#993399">01</font> - <font color="#990000">[</font>nodemon<font color="#990000">]</font> restarting due to changes<font color="#990000">...</font> 

<i><font color="#9A1900"># Alternative</font></i>
$ npm install supervisor
$ supervisor server<font color="#990000">.</font>js
DEBUG<font color="#990000">:</font> Watching directory <font color="#FF0000">'/evented-programming-with-nodejs/. </font>
</tt></pre>
<h3>Debuggers</h3>
<p>Another tool that it is hard to live without is a debugger. Node comes<br />
with one built in. It has a <code>gdb</code> flavor to it and it is kind of rough.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ node debug server<font color="#990000">.</font>js
debug<font color="#990000">&gt;</font> run
debugger listening on port <font color="#993399">5858</font>
connecting<font color="#990000">...</font>ok
<b><font color="#0000FF">break</font></b> <b><font color="#0000FF">in</font></b> <i><font color="#9A1900">#&lt;Socket&gt; ./server.js:9</font></i>
    debugger<font color="#990000">;</font> 

debug<font color="#990000">&gt;</font> p data<font color="#990000">.</font><b><font color="#000000">toString()</font></b><font color="#990000">;</font>
tapir

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Javascript</font></i>
<b><font color="#0000FF">var</font></b> echo <font color="#990000">=</font> net<font color="#990000">.</font><b><font color="#000000">createServer</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>socket<font color="#990000">)</font> <font color="#FF0000">{</font>
  socket<font color="#990000">.</font><b><font color="#000000">on</font></b><font color="#990000">(</font><font color="#FF0000">'data'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>data<font color="#990000">)</font> <font color="#FF0000">{</font>
      <b><font color="#0000FF">debugger</font></b><font color="#990000">;</font> <i><font color="#9A1900">// &lt;= break into debugger</font></i>
      socket<font color="#990000">.</font><b><font color="#000000">write</font></b><font color="#990000">(</font>data<font color="#990000">);</font>
  <font color="#FF0000">}</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<p>If you want a GUI debugger, it is possible to use the one that comes with<br />
Chrome by installing the <code>node-inspector</code>. It is started similarly to<br />
the built in debugger, but the <code>--debug</code> is an option instead of<br />
a subcommand.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ node-inspector <font color="#990000">&amp;</font>
visit http<font color="#990000">://</font><font color="#993399">0.0</font><font color="#990000">.</font><font color="#993399">0.0</font><font color="#990000">:</font><font color="#993399">8080</font>/debug<font color="#990000">?</font><font color="#009900">port</font><font color="#990000">=</font><font color="#993399">5858</font> to start debugging

$ node --debug server<font color="#990000">.</font>js debugger listening on port <font color="#993399">5858</font>
</tt></pre>
<p>After that you can just fire up Chrome on the URL,<br />
<a href="http://0.0.0.0:8080/debug?port=5858"> http://0.0.0.0:8080/debug?port=5858</a> and you can debug the node process<br />
just as if it was running in the browser.</p>
<p><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/nodeJS-Inspector.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/nodeJS-Inspector-300x207.png" alt="" title="nodeJS-Inspector" width="300" height="207" class="aligncenter size-medium wp-image-8397" /></a></p>
<h2>Idioms</h2>
<p>Idioms, patterns, techniques, call it what you like. Javascript code is<br />
littered with callbacks, and event more so with Node. Here are some tips<br />
on how to write good asynchronous code with Node.</p>
<h3>Return on Callbacks</h3>
<p>It is easy to forget to escape from the function after a callback has<br />
been called. An easy way to remedy this problem is to call return before<br />
every call to a callback. Even though the value is never used by the<br />
caller, it is an easy pattern to recognize and it prevents bugs.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>
<b><font color="#0000FF">function</font></b> <b><font color="#000000">doSomething</font></b><font color="#990000">(</font>response<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#000000">doAsyncCall</font></b><font color="#990000">(</font><font color="#FF0000">'tapir'</font><font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>err<font color="#990000">,</font> result<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>err<font color="#990000">)</font> <font color="#FF0000">{</font>
      <i><font color="#9A1900">// return on the callback</font></i>
      <b><font color="#0000FF">return</font></b> <b><font color="#000000">callback</font></b><font color="#990000">(</font>err<font color="#990000">);</font>
    <font color="#FF0000">}</font>
    <i><font color="#9A1900">// return on the callback</font></i>
    <b><font color="#0000FF">return</font></b> <b><font color="#000000">callback</font></b><font color="#990000">(</font><b><font color="#0000FF">null</font></b><font color="#990000">,</font> result<font color="#990000">);</font>
  <font color="#FF0000">}</font><font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>
<h3>Exceptions in Callbacks</h3>
<p>Exceptions that occur in callbacks cannot be handled the way we are used<br />
to, since the context is different. The solution to this is to pass<br />
along the exception as a parameter to the callback. In Node the<br />
convetion is to pass the error as the first parameter into the callback.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">function</font></b> <b><font color="#000000">insertIntoTable</font></b><font color="#990000">(</font>row<font color="#990000">,</font> <b><font color="#000000">function</font></b><font color="#990000">(</font>err<font color="#990000">,</font> data<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>err<font color="#990000">)</font> <b><font color="#0000FF">return</font></b> <b><font color="#000000">callback</font></b><font color="#990000">(</font>err<font color="#990000">);</font>
  <font color="#990000">...</font> 

  <i><font color="#9A1900">// Everything is OK</font></i>
  <b><font color="#0000FF">return</font></b> <b><font color="#000000">callback</font></b><font color="#990000">(</font><b><font color="#0000FF">null</font></b><font color="#990000">,</font> <font color="#FF0000">'row inserted'</font><font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>
<h3>Parallel Execution</h3>
<p>If you have multiple tasks that need to be finished before you take some<br />
new action, this can be handled with a simple counter. Here is an<br />
example of a simple function that starts up a bunch of functions in<br />
parallel and waits for all of them to finish before calling the<br />
callback.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Do all in parallel</font></i>
<b><font color="#0000FF">function</font></b> <b><font color="#000000">doAll</font></b><font color="#990000">(</font>collection<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#0000FF">var</font></b> left <font color="#990000">=</font> collection<font color="#990000">.</font>length<font color="#990000">;</font>
  collection<font color="#990000">.</font><b><font color="#000000">forEach</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>fun<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">fun</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
      <b><font color="#0000FF">if</font></b> <font color="#990000">(--</font>left <font color="#990000">==</font> <font color="#993399">0</font><font color="#990000">)</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font>
    <font color="#FF0000">}</font><font color="#990000">);</font>
  <font color="#FF0000">}</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">;</font> 

<i><font color="#9A1900">// Use it</font></i>
<b><font color="#0000FF">var</font></b> result <font color="#990000">=</font> <font color="#990000">[];</font>
<b><font color="#000000">doAll</font></b><font color="#990000">([</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">2000</font> <font color="#990000">)</font><font color="#FF0000">}</font><font color="#990000">,</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">3000</font> <font color="#990000">)</font><font color="#FF0000">}</font><font color="#990000">,</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">3</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">1000</font> <font color="#990000">)</font><font color="#FF0000">}</font>
  <font color="#990000">],</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font> <b><font color="#0000FF">return</font></b> result<font color="#990000">;</font> <font color="#FF0000">}</font> 

<i><font color="#9A1900">// returns [3, 1, 2]</font></i>
</tt></pre>
<h3>Sequential Execution</h3>
<p>Sometimes the ordering is important. Here is a simple function that<br />
makes sure that the calls are executed in sequence. It uses recursion to<br />
to make sure that the calls are handled in the correct order. It also<br />
uses the Node function <code>process.nextTick()</code> to prevent the stack from<br />
getting to large for large collections. Similar results can be obtained<br />
with <code>setTimeout()</code> in browser Javascript. It can be seen as a simple<br />
trick to achieve <em>tail recursion</em>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">function</font></b> <b><font color="#000000">doInSequence</font></b><font color="#990000">(</font>collection<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">var</font></b> queue <font color="#990000">=</font> collection<font color="#990000">.</font><b><font color="#000000">slice</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font> <i><font color="#9A1900">// Duplicate</font></i> 

    <b><font color="#0000FF">function</font></b> <b><font color="#000000">iterate</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
      <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>queue<font color="#990000">.</font>length <font color="#990000">===</font> <font color="#993399">0</font><font color="#990000">)</font> <b><font color="#0000FF">return</font></b> <b><font color="#000000">callback</font></b><font color="#990000">();</font>
      <i><font color="#9A1900">// Take the first element</font></i>
      <b><font color="#0000FF">var</font></b> fun <font color="#990000">=</font> queue<font color="#990000">.</font><b><font color="#000000">splice</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">)[</font><font color="#993399">0</font><font color="#990000">];</font>
      <b><font color="#000000">fun</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">(</font>err<font color="#990000">)</font> <font color="#FF0000">{</font>
        <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>err<font color="#990000">)</font> <b><font color="#0000FF">throw</font></b> err<font color="#990000">;</font>
        <i><font color="#9A1900">// Call it without building up the stack</font></i>
        process<font color="#990000">.</font><b><font color="#000000">nextTick</font></b><font color="#990000">(</font><b><font color="#000000">iterate</font></b><font color="#990000">);</font>
      <font color="#FF0000">}</font><font color="#990000">);</font>
    <font color="#FF0000">}</font>
    <b><font color="#000000">iterate</font></b><font color="#990000">();</font>
<font color="#FF0000">}</font> 

<b><font color="#0000FF">var</font></b> result <font color="#990000">=</font> <font color="#990000">[];</font>
<b><font color="#000000">doInSequence</font></b><font color="#990000">([</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">2000</font> <font color="#990000">)</font><font color="#FF0000">}</font><font color="#990000">,</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">3000</font> <font color="#990000">)</font><font color="#FF0000">}</font><font color="#990000">,</font>
  <b><font color="#0000FF">function</font></b><font color="#990000">(</font>callback<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>result<font color="#990000">.</font><b><font color="#000000">push</font></b><font color="#990000">(</font><font color="#993399">3</font><font color="#990000">);</font> <b><font color="#000000">callback</font></b><font color="#990000">();</font><font color="#FF0000">}</font><font color="#990000">,</font> <font color="#993399">1000</font> <font color="#990000">)</font><font color="#FF0000">}</font>
  <font color="#990000">],</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font> <b><font color="#0000FF">return</font></b> result<font color="#990000">;</font> <font color="#FF0000">}</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Returns [1, 2, 3]</font></i>
</tt></pre>
<h2>Library Support for Asynchronous Programming</h2>
<p>If you don't want to write these functions yourself, there are a few<br />
libraries that can help you out. I'll show two version that I like.</p>
<h3>Fibers</h3>
<p>Fibers are also called co-routines. Fibers provide two functions,<br />
<em>suspend</em> and <em>resume</em>, which allows us to write code in a synchronous<br />
looking style. In the Node version of fibers,<br />
<a href="https://github.com/laverdet/node-fibers">node-fibers</a>, suspend and<br />
resume are called <code>yield()</code> and <code>run()</code> instead.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'fibers'</font><font color="#990000">);</font>
<b><font color="#0000FF">var</font></b> print <font color="#990000">=</font> <b><font color="#000000">require</font></b><font color="#990000">(</font><font color="#FF0000">'util'</font><font color="#990000">).</font>print<font color="#990000">;</font> 

<b><font color="#0000FF">function</font></b> <b><font color="#000000">sleep</font></b><font color="#990000">(</font>ms<font color="#990000">)</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">var</font></b> fiber <font color="#990000">=</font> Fiber<font color="#990000">.</font>current<font color="#990000">;</font>
    <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font> fiber<font color="#990000">.</font><b><font color="#000000">run</font></b><font color="#990000">();</font> <font color="#FF0000">}</font><font color="#990000">,</font> ms<font color="#990000">);</font>
    <b><font color="#000000">yield</font></b><font color="#990000">();</font>
<font color="#FF0000">}</font> 

<b><font color="#000000">Fiber</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    <b><font color="#000000">print</font></b><font color="#990000">(</font><font color="#FF0000">'wait... '</font> <font color="#990000">+</font> <b><font color="#0000FF">new</font></b> Date <font color="#990000">+</font> <font color="#FF0000">'</font><font color="#CC33CC">\n</font><font color="#FF0000">'</font><font color="#990000">);</font>
    <b><font color="#000000">sleep</font></b><font color="#990000">(</font><font color="#993399">1000</font><font color="#990000">);</font>
    <b><font color="#000000">print</font></b><font color="#990000">(</font><font color="#FF0000">'ok... '</font> <font color="#990000">+</font> <b><font color="#0000FF">new</font></b> Date <font color="#990000">+</font> <font color="#FF0000">'</font><font color="#CC33CC">\n</font><font color="#FF0000">'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">).</font><b><font color="#000000">run</font></b><font color="#990000">();</font>
<b><font color="#000000">print</font></b><font color="#990000">(</font><font color="#FF0000">'back in main</font><font color="#CC33CC">\n</font><font color="#FF0000">'</font><font color="#990000">);</font>
</tt></pre>
<p>Fibers are a very nice way of writing asynchronous code but, in Node,<br />
they have one drawback. They are not supported without patching the V8<br />
virtual machine. The patching is done when you install <code>node-fibers</code> and<br />
you have to run the command <code>node-fibers</code> instead of <code>node</code> to use it.</p>
<h3>The <code>async</code> Library</h3>
<p>If you don't want to use the patched version of V8, I can recommend the<br />
<a href="https://github.com/caolan/async">async</a> library.  Async provides<br />
around 20 functions that include the usual 'functional' suspects (map,<br />
reduce, filter, forEach...) as well as some common patterns for<br />
asynchronous flow control (parallel, series, waterfall...). All these<br />
functions assume you follow the Node convention of providing a single<br />
callback as the last argument of your async function.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>async<font color="#990000">.</font><b><font color="#000000">map</font></b><font color="#990000">([</font><font color="#FF0000">'file1'</font><font color="#990000">,</font><font color="#FF0000">'file2'</font><font color="#990000">,</font><font color="#FF0000">'file3'</font><font color="#990000">],</font> fs<font color="#990000">.</font>stat<font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>err<font color="#990000">,</font> results<font color="#990000">)</font><font color="#FF0000">{</font>
    <i><font color="#9A1900">// results is now an array of stats for each file</font></i>
<font color="#FF0000">}</font><font color="#990000">);</font> 

async<font color="#990000">.</font><b><font color="#000000">filter</font></b><font color="#990000">([</font><font color="#FF0000">'file1'</font><font color="#990000">,</font><font color="#FF0000">'file2'</font><font color="#990000">,</font><font color="#FF0000">'file3'</font><font color="#990000">],</font> path<font color="#990000">.</font>exists<font color="#990000">,</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>results<font color="#990000">)</font><font color="#FF0000">{</font>
    <i><font color="#9A1900">// results now equals an array of the existing files</font></i>
<font color="#FF0000">}</font><font color="#990000">);</font> 

async<font color="#990000">.</font><b><font color="#000000">parallel</font></b><font color="#990000">([</font>
    <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> <font color="#990000">...</font> <font color="#FF0000">}</font><font color="#990000">,</font>
    <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> <font color="#990000">...</font> <font color="#FF0000">}</font>
<font color="#990000">],</font> callback<font color="#990000">);</font> 

async<font color="#990000">.</font><b><font color="#000000">series</font></b><font color="#990000">([</font>
    <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> <font color="#990000">...</font> <font color="#FF0000">}</font><font color="#990000">,</font>
    <b><font color="#0000FF">function</font></b><font color="#990000">()</font><font color="#FF0000">{</font> <font color="#990000">...</font> <font color="#FF0000">}</font>
<font color="#990000">],</font> callback<font color="#990000">);</font>
</tt></pre>
<h2>Conclusion</h2>
<p>Node is definitely an interesting platform. The possibility to have<br />
Javascript running through the whole stack, from the browser all the way<br />
down into the database (if you use something like CouchDB or MongoDB)<br />
really appeals to me. The easy way to deploy code to multiple, different<br />
cloud providers is also a good argument for Node.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/05/15/a-not-very-short-introduction-to-node-js/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mastery</title>
		<link>http://blog.jayway.com/2011/05/08/mastery/</link>
		<comments>http://blog.jayway.com/2011/05/08/mastery/#comments</comments>
		<pubDate>Sun, 08 May 2011 09:06:38 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8302</guid>
		<description><![CDATA[Mastery Here is a wonderful story, told by Ken Robinson in his book The Element, about an 8 year old girl, Gillian Lynne, who everyone sees as a problem child. Gillian and her mother went to a psychiatrist and the mother describes her daughter's difficulties with concentrating, sitting, and doing her homework. The doctor listened [...]]]></description>
			<content:encoded><![CDATA[<h1>Mastery</h1>
<p>Here is a wonderful story, told by Ken Robinson in his book <a href="http://www.amazon.com/Element-Finding-Passion-Changes-Everything/dp/0670020478?tag=thtasta-20">The Element</a>,<br />
about an 8 year old girl, Gillian Lynne, who everyone sees as a problem child.</p>
<blockquote><p>Gillian and her mother went to a psychiatrist and the mother describes<br />
her daughter's difficulties with concentrating, sitting, and doing her<br />
homework.</p>
<p>The doctor listened to the mother and then he told Gillian that he<br />
wanted to talk to her mother alone.  When they left the room, the<br />
doctor turned on some music on the radio. Outside the room, the doctor<br />
said to the mother "Just stand her and watch her!" As they watched,<br />
the girl started moving to the music with a grace that anyone would<br />
have been impressed by.</p>
<p>After watching for a few minutes, the doctor turned to the mother and<br />
said: "There is nothing wrong with your daughter, she's a dancer, take<br />
her to a dance school!"</p>
<p>Gillan grew up to become one of the most accomplished choreographers<br />
of our time.</p>
</blockquote>
<p>This is a story about how people shine when they are allowed to do what<br />
they are meant to do, when they are in their element. But, what it does<br />
not take into account is work, hard work!</p>
<p>Thomas Jefferson put it nicely:</p>
<blockquote><p>I find that the harder I work, the more luck I seem to have!</p>
</blockquote>
<p>This post is not about talent, it is about mastery, the hard work of mastery!</p>
<p>Talent is not a requirement for Mastery, in fact talent can get in your<br />
way on the path of mastery, if it is too easy in the beginning we may<br />
not be able to put in the hours needed for true mastery when it gets hard<br />
or boring.</p>
<p>Many of the ideas for this post come from the book called<br />
<a href="http://www.amazon.com/Mastery-Keys-Success-Long-Term-Fulfillment/dp/0452267560?tag=thtasta-20">Mastery</a><br />
by George Leonard and he describes it well.</p>
<blockquote><p>We fail to realize that mastery is not about perfection. It's about<br />
a process, a journey. The master is the one who stays on the path day<br />
after day, year after year. The master is the one who is willing to<br />
try, and fail, and try again, for as long as he or she lives.</p>
</blockquote>
<p>Leonard identifies five keys to mastery. They are instruction, practice,<br />
surrender, intentionality, and the Edge.</p>
<h2>Instruction</h2>
<p>Instruction is vital. How do we know that what we are doing is the right<br />
thing. As a programmer I am always confronted with new things. How do<br />
I tell the good from the bad? After you reach a certain level of<br />
experience it gets easier to make an educated guess, but it is still<br />
just a guess. And before we have a lot of experience, we are liable to<br />
fall into traps all the time, (EJB anyone?).</p>
<p>My way of dealing with new things is to read books, papers, and blogs,<br />
listen to podcasts, watch screencasts, and if I can find the time, attend<br />
workshops at conferences. But, it is important to listen to critique as<br />
well as praise of a new technique. If it seems reasonable to you, by all<br />
means, give it a shot. And then, not until you have actually used<br />
a system for quite a while, can you tell if it is good or not for you.<br />
Because that is another aspect of mastery, we are all on our own path,<br />
what is right for me may not be right for someone else!</p>
<h2>Practice</h2>
<p>Practice is the true essence of Mastery. Without it, everything else<br />
falls to pieces. Practice is the path that build the foundation for<br />
mastery. Another quote from Leonard:</p>
<blockquote><p>How do you best move toward mastery? To put it simply, you practice<br />
diligently, but you practice primarily for the sake of the practice<br />
itself.</p>
</blockquote>
<p>An interesting fact about practice is how the normal learning curve looks.</p>
<p><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/practice_curve.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2011/05/practice_curve-300x32.png" alt="" title="practice_curve" width="300" height="32" class="alignnone size-medium wp-image-8303" /></a></p>
<p>Do you recognize it? Long periods where you practice and practice but<br />
you don't seem to get any better. Then suddenly there is a small<br />
improvement and you feel like you finally get it, and then after<br />
a little while you are back to normal again.</p>
<blockquote><p>The more I know, the more I know that I don't know. --Socrates<br />
(misquoted:)</p>
</blockquote>
<h2>Surrender</h2>
<p>What Leonard calls surrender I would call humility. A Zen Master would<br />
probably call it <em>Beginners Mind</em>.</p>
<blockquote><p>Beginner's Mind. It refers to having an attitude of openness, eagerness,<br />
and lack of preconceptions when studying a subject, even when studying<br />
at an advanced level, just as a beginner in that subject would.</p>
</blockquote>
<p>It can be very hard to give up your hard earned skills in order to grasp<br />
a new concept. But, sometimes, the only way to improve is to start over<br />
with a completely different approach.</p>
<p>How do we expect to learn something new if we are not willing to look<br />
like an idiot for awhile?</p>
<h2>Intentionality</h2>
<blockquote><p>You gotta be careful if you don't know where you're going, because you<br />
might not get there. -- Yogi Berra</p>
</blockquote>
<p>Intentionality means a lot of things. It means character, discipline,<br />
stamina or willpower. But it also means knowing what you want.</p>
<p>Do you know what you want? Unless you do, it is very hard to stay on the<br />
path. In my mind intentionality is not necessarily goals, it is more<br />
a direction. If I stop for a while and look around, I can tell if I am<br />
on the path, because if I am, I will, slowly, be moving in the direction<br />
I intend to go.</p>
<blockquote><p>When you get to the top of the mountain, keep climbing. -- Chinese<br />
proverb</p>
</blockquote>
<h2>The Edge</h2>
<p>Here's another quote that I like, from the book,<br />
<a href="http://www.amazon.com/Zorba-Greek-Nikos-Kazantzakis/dp/0684825546?tag=thtasta-20">Zorba</a>:</p>
<blockquote><p>No you're not free! The string that you are tied to is perhaps longer<br />
than others'. You're on a long piece of string boss, but you never cut<br />
the string in two, you need a touch of folly to do that...<br />
A man's head is like a grocer, it keeps accounts. It never risks all<br />
it has, always keeps something in reserve. It never breaks the string.<br />
But if a man never breaks the string, tell me what flavor is left in<br />
life? Nothing, but the flavor of weak camomile tea. Nothing like rum<br />
that makes you see life inside out!</p>
</blockquote>
<p>In order to make the really big leaps like, for example, Einstein's<br />
Relativity Theory, we need to be a bit crazy. It is impossible to<br />
analytically reach certain conclusions. They can only be reached by<br />
thinking so far outside the box, that most people would consider us<br />
insane.</p>
<h2>Conclusion</h2>
<p>I will conclude with another quote from Leonard:</p>
<blockquote><p>It is easy to get on the path of mastery. The real challenge lies in<br />
staying on it.</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/05/08/mastery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What Is New in EcmaScript 5?</title>
		<link>http://blog.jayway.com/2011/04/05/what-is-new-in-ecmascript-5/</link>
		<comments>http://blog.jayway.com/2011/04/05/what-is-new-in-ecmascript-5/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 08:55:04 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7894</guid>
		<description><![CDATA[More and more browsers have implemented EcmaScript 5. That means that it is time to get up to speed on what it means for us as developers Here is a summary of Douglas Crockfords talk on Ecmascript 5 at Scandev New Syntax It is now possible to use reserved words as property names and in [...]]]></description>
			<content:encoded><![CDATA[
<p>More and more <a href="http://kangax.github.com/es5-compat-table/">browsers have implemented EcmaScript 5</a>. That means that it is time to get up to speed on what it means for us as developers</p>
<p>Here is a summary of Douglas Crockfords talk on Ecmascript 5 at <a href="http://www.scandevconf.se/2011/conference/speakers/douglas-crockford/">Scandev</a></p>
<h2>New Syntax</h2>
<ul>
<li>It is now possible to use reserved words as property names and in<br />
unquoted literal keys.</li>
<li>A comma is allowed after the last pair in object literals and after<br />
the last element in array literals.</li>
<li>It is possible to break strings by inserting a backslash at the end of<br />
the line (not recommended by Doug).</li>
</ul>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>object<font color="#990000">.</font><b><font color="#0000FF">return</font></b> <font color="#990000">=</font> <font color="#FF0000">'please'</font><font color="#990000">;</font>
object <font color="#990000">=</font> <font color="#FF0000">{</font> <b><font color="#0000FF">throw</font></b><font color="#990000">:</font> <font color="#FF0000">'down'</font><font color="#990000">,</font> <font color="#FF0000">}</font><font color="#990000">;</font>
array <font color="#990000">=</font> <font color="#990000">[</font><font color="#FF0000">'a'</font><font color="#990000">,</font> <font color="#FF0000">'tapir'</font><font color="#990000">,</font> <font color="#FF0000">'is'</font><font color="#990000">,</font> <font color="#FF0000">'lovely'</font><font color="#990000">,</font> <font color="#990000">];</font>
string <font color="#990000">=</font> <font color="#FF0000">"a tapir is \</font>
<font color="#FF0000">beautiful"</font>
</tt></pre>
<h2>Library Additions</h2>
<h3>JSON</h3>
<p><code>JSON.parse</code> and <code>JSON.stringify</code> are now part of the standard.</p>
<h3>Date</h3>
<p><code>Date</code> has new methods, <code>toISOString</code> and <code>toJSON</code> It is also able to<br />
parse ISO strings.</p>
<h3>Object</h3>
<p><code>Object</code> gets a bunch of new methods. But they are added on <code>Object</code> and not<br />
on <code>Object.prototype</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>
<i><font color="#9A1900">// Creates an object with parent as prototype and properties from donor</font></i>
Object<font color="#990000">.</font><b><font color="#000000">create</font></b><font color="#990000">(</font>parent<font color="#990000">,</font> donor<font color="#990000">);</font> 

<i><font color="#9A1900">// Meta properties of an object</font></i>
<b><font color="#0000FF">var</font></b> descriptor <font color="#990000">=</font> <font color="#FF0000">{</font>
  value<font color="#990000">:</font> <font color="#FF0000">"test"</font><font color="#990000">,</font>
  writable<font color="#990000">:</font> <b><font color="#0000FF">true</font></b><font color="#990000">,</font>    <i><font color="#9A1900">// Can the value be changed?</font></i>
  enumerable<font color="#990000">:</font> <b><font color="#0000FF">true</font></b><font color="#990000">,</font>  <i><font color="#9A1900">// Will it appear in for-in and Object.keys(object)?</font></i>
  configurable<font color="#990000">:</font> <b><font color="#0000FF">true</font></b><font color="#990000">,</font> <i><font color="#9A1900">// Can the property be removed?</font></i>
  set<font color="#990000">:</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>value<font color="#990000">)</font> <font color="#FF0000">{</font> test <font color="#990000">=</font> value<font color="#FF0000">}</font><font color="#990000">,</font> <i><font color="#9A1900">// Getter</font></i>
  get<font color="#990000">:</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font> <b><font color="#0000FF">return</font></b> test <font color="#FF0000">}</font>       <i><font color="#9A1900">// Setter</font></i>
<font color="#FF0000">}</font> 

<i><font color="#9A1900">// Methods for manipulation the descriptors</font></i>
Object<font color="#990000">.</font><b><font color="#000000">defineProperty</font></b><font color="#990000">(</font>object<font color="#990000">,</font> property<font color="#990000">,</font> descriptor<font color="#990000">)</font>
Object<font color="#990000">.</font><b><font color="#000000">defineProperties</font></b><font color="#990000">(</font>object<font color="#990000">,</font> descriptors<font color="#990000">)</font>
Object<font color="#990000">.</font><b><font color="#000000">getOwnPropertyDescriptor</font></b><font color="#990000">(</font>object<font color="#990000">,</font> property<font color="#990000">)</font>
Object<font color="#990000">.</font><b><font color="#000000">getPrototypeOf</font></b><font color="#990000">(</font>object<font color="#990000">)</font> 

<i><font color="#9A1900">// Returns an array of enumerable properties</font></i>
Object<font color="#990000">.</font><b><font color="#000000">keys</font></b><font color="#990000">(</font>object<font color="#990000">)</font>
<i><font color="#9A1900">// Returns an array of all properties</font></i>
Object<font color="#990000">.</font><b><font color="#000000">getOwnPropertyNames</font></b><font color="#990000">(</font>object<font color="#990000">)</font> 

<i><font color="#9A1900">// Prevents anyone from adding properties to the object, cannot be undone.</font></i>
Object<font color="#990000">.</font><b><font color="#000000">preventExtensions</font></b><font color="#990000">(</font>object<font color="#990000">)</font>
Object<font color="#990000">.</font><b><font color="#000000">isExtensible</font></b><font color="#990000">(</font>object<font color="#990000">)</font> 

<i><font color="#9A1900">// Prevents anyone from changing, properties or descriptors of the object.</font></i>
<i><font color="#9A1900">// The values can still be changed</font></i>
Object<font color="#990000">.</font><b><font color="#000000">seal</font></b><font color="#990000">(</font>object<font color="#990000">)</font>
Objcect<font color="#990000">.</font><b><font color="#000000">isSealed</font></b><font color="#990000">(</font>object<font color="#990000">)</font> 

<i><font color="#9A1900">// Prevents any changes to the object.</font></i>
Object<font color="#990000">.</font><b><font color="#000000">freeze</font></b><font color="#990000">(</font>object<font color="#990000">)</font>
Object<font color="#990000">.</font><b><font color="#000000">isFrozen</font></b><font color="#990000">(</font>object<font color="#990000">)</font> 

</tt></pre>
<h3>Array</h3>
<p>Arrays are extended with a bunch of new, useful methods, leading to less<br />
need for third-party libraries.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>
<i><font color="#9A1900">// Do all elements satisfy predicate?</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">every</font></b><font color="#990000">(</font>predicate<font color="#990000">)</font> 

<i><font color="#9A1900">// Return a new array with the elements that satisfy predicate?</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">filter</font></b><font color="#990000">(</font>predicate<font color="#990000">)</font> 

<i><font color="#9A1900">// Call action(element) for each element.</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">forEach</font></b><font color="#990000">(</font>action<font color="#990000">)</font> 

<i><font color="#9A1900">// What is the index of the first element that equals value?</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">indexOf</font></b><font color="#990000">(</font>value<font color="#990000">,</font> fromIndex<font color="#990000">)</font> 

<i><font color="#9A1900">// What is the index of the last element that equal value?</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">lastIndexOf</font></b><font color="#990000">(</font>value<font color="#990000">,</font> fromIndex<font color="#990000">)</font> 

<i><font color="#9A1900">// Create a new array by applying unaryFunc to each element</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">map</font></b><font color="#990000">(</font>unaryFunc<font color="#990000">)</font> 

<i><font color="#9A1900">// Reduces the elements of the array, by applying binaryFunc</font></i>
<i><font color="#9A1900">// between the elements</font></i>
<i><font color="#9A1900">// [a0, a1].reduce(+ , seed)  == seed + a0 + a1</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">reduce</font></b><font color="#990000">(</font>binaryFunc<font color="#990000">,</font> seed<font color="#990000">)</font> 

<i><font color="#9A1900">// Is at least one element satisfied by the predicate?</font></i>
Array<font color="#990000">.</font><b><font color="#0000FF">prototype</font></b><font color="#990000">.</font><b><font color="#000000">some</font></b><font color="#990000">(</font>predicate<font color="#990000">)</font> 

</tt></pre>
<h3>String</h3>
<p><code>String.prototype.trim</code> is added to string. It trims whiespace from the<br />
beginning and the end of a string.</p>
<h3>Function</h3>
<p><code>Function.prototype.bind(thisArg, arg, ...)</code> is added to function. It<br />
creates a new function by binding the value of this to a specified object.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>
<b><font color="#0000FF">var</font></b> tapir <font color="#990000">=</font> <font color="#FF0000">{</font>
  method<font color="#990000">:</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>name<font color="#990000">)</font><font color="#FF0000">{</font>
    <b><font color="#0000FF">this</font></b><font color="#990000">.</font>name <font color="#990000">=</font> name<font color="#990000">;</font>
  <font color="#FF0000">}</font>
<font color="#FF0000">}</font><font color="#990000">;</font>
<b><font color="#000000">setTimeout</font></b><font color="#990000">(</font> tapir<font color="#990000">.</font>method<font color="#990000">.</font><b><font color="#000000">bind</font></b><font color="#990000">(</font>tapir<font color="#990000">,</font> <font color="#FF0000">"Malayan"</font><font color="#990000">),</font> <font color="#993399">100</font> <font color="#990000">);</font>
</tt></pre>
<h2>Strict Mode</h2>
<p>A new mode called strict can be turned on by writing</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
  <font color="#FF0000">"use strict"</font><font color="#990000">;</font>
<font color="#FF0000">}</font>
</tt></pre>
<p>It must be the first line in the function. It could also be the first<br />
line of the file, but that does not play well with the optimizing<br />
practices used in Javascript, like concatenation and minification.</p>
<p>In strict mode:</p>
<ul>
<li>Variables must be declared before usage.</li>
<li>The <code>with</code> statement cannot be used.</li>
<li><code>eval</code> is a reserved word.</li>
<li><code>arguments.caller</code> and <code>arguments.callee</code> cannot be used.</li>
<li>etc.</li>
</ul>
<p>Since IE6-IE9 does not implement strict model, Crockford thinks that<br />
they "must die" since they, "hinder the creation of useful mashups".</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/04/05/what-is-new-in-ecmascript-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High Performance Javascript</title>
		<link>http://blog.jayway.com/2011/03/28/high-performance-javascript/</link>
		<comments>http://blog.jayway.com/2011/03/28/high-performance-javascript/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 18:53:40 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7666</guid>
		<description><![CDATA[Here are some tips on high perfomance Javascript I have picked up. Most of it comes from the books High Performance Javascript by Nicholas C. Zakas and High Performance Web Sites by Steve Souders. Loading Load files at the end of the HTML page Load the Javascript files right before the body, this will allow [...]]]></description>
			<content:encoded><![CDATA[
<p>Here are some tips on high perfomance Javascript I have picked up. Most<br />
of it comes from the books <a href="http://www.amazon.com/Performance-JavaScript-Faster-Application-Interfaces/dp/059680279X?tag=thtasta-20">High Performance Javascript</a><br />
by Nicholas C. Zakas and <a href="http://www.amazon.com/High-Performance-Web-Sites-Essential/dp/0596529309?tag=thtasta-20">High Performance Web Sites</a><br />
by Steve Souders.</p>
<h2>Loading</h2>
<h3>Load files at the end of the HTML page</h3>
<p>Load the Javascript files right before the body, this will allow the<br />
page to render without having to wait for all the Javascript files.</p>
<h3>Group files together</h3>
<p>With normal loading, the files are loaded sequentially. Each file will<br />
be loaded and parsed before the next file starts to load. Merge them<br />
together into one large file. While you are at it, you should also<br />
minimize it. Tools to help you with this are:</p>
<ul>
<li><a href="http://www.crockford.com/javascript/jsmin.html">JSMin</a></li>
<li><a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a></li>
</ul>
<h3>Load files asynchronously</h3>
<p>If normal loading with grouped files is not good enough, it is also<br />
possible to load the files asynchronously. This will also allow you to<br />
load files on demand. Tools for this are:</p>
<ul>
<li><a href="http://github.com/rgrove/lazyload">LazyLoad</a></li>
<li><a href="http://labjs.com/">LABjs</a></li>
<li><a href="http://headjs.com/">HeadJS</a></li>
</ul>
<h2>Variable Access</h2>
<p>Literal values and local variables can be accessed very quickly.  Array<br />
access and member access take longer. If the <em>prototype chain</em> or <em>scope<br />
chain</em> needs to be traversed, it will take longer the further up the chain<br />
the access is. Global variables are always the slowest to access because<br />
they are always last in the scope chain.</p>
<p>You can improve the performance of JavaScript code by storing frequently<br />
used, object members, array items, and out-of-scope variables, in local<br />
variables.</p>
<h2>DOM</h2>
<p><em>All DOM manipulation is slow.</em></p>
<ul>
<li>Minimize DOM access</li>
<li>Use local variables to store DOM references you'll access repeatedly.</li>
<li>HTML collections represent the live, underlying document, so:
<ul>
<li>Cache the collection length into a variable and use it when iterating</li>
<li>Make a copy of the collection into an array for heavy work on collections</li>
</ul>
</li>
</ul>
<h3>Reflow and rendering</h3>
<p>The browser contains two trees, the <em>DOM tree</em> and a <em>render tree</em>.<br />
Whenever the DOM layout or geometry is changed the view will have to be<br />
re-rendered. This is known as reflow.</p>
<p>Reflow happens when:</p>
<ul>
<li>Visible DOM elements are added or removed</li>
<li>Elements change position</li>
<li>Elements change size (margin, padding, border thickness, width, height, etc.)</li>
<li>Content is changed, (text changes or an image is replaced with one of a different size)</li>
<li>The page renders initially</li>
<li>The browser window is resized</li>
</ul>
<p>Combine multiple DOM and style changes into a batch and apply them once.<br />
This can be done with documentFragments or by cloning the node.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Create a document fragment</font></i>
<b><font color="#0000FF">var</font></b> fragment <font color="#990000">=</font> document<font color="#990000">.</font><b><font color="#000000">createDocumentFragment</font></b><font color="#990000">();</font> 

<i><font color="#9A1900">// Do something with framgment</font></i> 

<i><font color="#9A1900">// Append the fragments children to the DOM</font></i>
document<font color="#990000">.</font><b><font color="#000000">getElementById</font></b><font color="#990000">(</font><font color="#FF0000">'mylist'</font><font color="#990000">).</font><b><font color="#000000">appendChild</font></b><font color="#990000">(</font>fragment<font color="#990000">);</font> 

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Clone Node</font></i>
<b><font color="#0000FF">var</font></b> old <font color="#990000">=</font> document<font color="#990000">.</font><b><font color="#000000">getElementById</font></b><font color="#990000">(</font><font color="#FF0000">'mylist'</font><font color="#990000">);</font>
<b><font color="#0000FF">var</font></b> clone <font color="#990000">=</font> old<font color="#990000">.</font><b><font color="#000000">cloneNode</font></b><font color="#990000">(</font><b><font color="#0000FF">true</font></b><font color="#990000">);</font> 

<i><font color="#9A1900">// Do something with clone</font></i> 

<i><font color="#9A1900">// Replace node with clone</font></i>
old<font color="#990000">.</font>parentNode<font color="#990000">.</font><b><font color="#000000">replaceChild</font></b><font color="#990000">(</font>clone<font color="#990000">,</font> old<font color="#990000">);</font> 

</tt></pre>
<h2>Algorithms and Flow</h2>
<p>Use algortithms with better complexity performance for large collections.</p>
<ul>
<li><em>for-in</em> loops are slower than for, while and do-while loops. Avoid<br />
for-in unless you need to iterate over a number of unknown object<br />
properties.</li>
<li>Lookup-tables are faster than multiple conditionals.</li>
<li>Recursion can be re-written with iteration if you get stackoverflow<br />
errors.</li>
</ul>
<h2>Strings and Regexes</h2>
<p>Strings concatenation is quite fast in most browsers. In IE, you may<br />
need to use <em>Array.join</em>.</p>
<p>Regular expression can be improved by:</p>
<ul>
<li>Focus on failing faster.</li>
<li>Start regexes with simple, required tokens.</li>
<li>Make quantified patterns and their following pattern mutually exclusive /"[&#94;"]*"/.</li>
<li>Use noncapturing groups. (?:) instead of ().</li>
<li>Capture text to reduce postprocessing.</li>
<li>Expose required tokens /&#94;(ab|cd)/ instead of /(&#94;ab|&#94;cd)</li>
<li>Resue regexes by assigning them to variables.</li>
<li>Split complex regexes into simpler pieces.</li>
</ul>
<h2>Responsiveness</h2>
<p>The total amount of time that a single JavaScript operation should take<br />
is 100. If it takes longer it needs to be split up, this can be done<br />
using timers.</p>
<p>Two determining factors for whether a loop can be done asynchronously<br />
using timers:</p>
<ul>
<li>Does the processing have to be done synchronously?</li>
<li>Does the data have to be processed sequentially?</li>
</ul>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Function for processing an array in parallel</font></i>
<b><font color="#0000FF">function</font></b> <b><font color="#000000">processArray</font></b><font color="#990000">(</font>items<font color="#990000">,</font> process<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#0000FF">var</font></b> minTimeToStart <font color="#990000">=</font> <font color="#993399">25</font><font color="#990000">;</font>
  <b><font color="#0000FF">var</font></b> copyOfItems <font color="#990000">=</font> items<font color="#990000">.</font><b><font color="#000000">concat</font></b><font color="#990000">();</font>
  <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    <b><font color="#000000">process</font></b><font color="#990000">(</font>copyOfItems<font color="#990000">.</font><b><font color="#000000">shift</font></b><font color="#990000">());</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>copyOfItems<font color="#990000">.</font>lengthÂ <font color="#990000">&gt;</font> <font color="#993399">0</font><font color="#990000">)</font>
      <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font>arguments<font color="#990000">.</font>callee<font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
    <b><font color="#0000FF">else</font></b>
      <b><font color="#000000">callback</font></b><font color="#990000">(</font>items<font color="#990000">);</font>
  <font color="#FF0000">}</font><font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
<font color="#FF0000">}</font> 

<i><font color="#9A1900">// Function for processing multiple tasks in parallel</font></i>
<b><font color="#0000FF">function</font></b> <b><font color="#000000">processTasks</font></b><font color="#990000">(</font>tasks<font color="#990000">,</font> args<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#0000FF">var</font></b> minTimeToStart <font color="#990000">=</font> <font color="#993399">25</font><font color="#990000">;</font>
  <b><font color="#0000FF">var</font></b> copyOfTasks <font color="#990000">=</font> steps<font color="#990000">.</font><b><font color="#000000">concat</font></b><font color="#990000">();</font>
  <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    <b><font color="#0000FF">var</font></b> task <font color="#990000">=</font> copyOfTasks<font color="#990000">.</font><b><font color="#000000">shift</font></b><font color="#990000">();</font>
    task<font color="#990000">.</font><b><font color="#000000">apply</font></b><font color="#990000">(</font><b><font color="#0000FF">null</font></b><font color="#990000">,</font> args <font color="#990000">||</font> <font color="#990000">[]);</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>copyOfTasks<font color="#990000">.</font>length <font color="#990000">&gt;</font> <font color="#993399">0</font><font color="#990000">)</font>
      <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font>arguments<font color="#990000">.</font>callee<font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
    <b><font color="#0000FF">else</font></b>
      <b><font color="#000000">callback</font></b><font color="#990000">();</font>
  <font color="#FF0000">}</font><font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
<font color="#FF0000">}</font> 

</tt></pre>
<p>You should limit the number of high-frequency repeating timers in your<br />
web application. It is better to create a single repeating timer that<br />
performs multiple operations with each execution.</p>
<p>It is not recommended to have <em>minTimeToStart</em> less than 25<br />
milliseconds, because there is a risk that the timers will fill up the<br />
queue.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Timed version of process array, where each version is able to</font></i>
<i><font color="#9A1900">// process items from the array for up to 50 milliseconds.</font></i>
<b><font color="#0000FF">function</font></b> <b><font color="#000000">timedProcessArray</font></b><font color="#990000">(</font>items<font color="#990000">,</font> process<font color="#990000">,</font> callback<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#0000FF">var</font></b> minTimeToStart <font color="#990000">=</font> <font color="#993399">25</font><font color="#990000">;</font>
  <b><font color="#0000FF">var</font></b> copyOfItems <font color="#990000">=</font> items<font color="#990000">.</font><b><font color="#000000">concat</font></b><font color="#990000">();</font>
  <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    <i><font color="#9A1900">// (+) converts the Date object into a numeric representation</font></i>
    <b><font color="#0000FF">var</font></b> start <font color="#990000">=</font> <font color="#990000">+</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">Date</font></b><font color="#990000">();</font>
    <b><font color="#0000FF">do</font></b> <font color="#FF0000">{</font>
      <b><font color="#000000">process</font></b><font color="#990000">(</font>copyOfItems<font color="#990000">.</font><b><font color="#000000">shift</font></b><font color="#990000">());</font>
    <font color="#FF0000">}</font> <b><font color="#0000FF">while</font></b> <font color="#990000">(</font>copyOfItems<font color="#990000">.</font>length <font color="#990000">&gt;</font> <font color="#993399">0</font> <font color="#990000">&amp;&amp;</font> <font color="#990000">(+</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">Date</font></b><font color="#990000">()</font> <font color="#990000">-</font> start <font color="#990000">&lt;</font> <font color="#993399">50</font><font color="#990000">));</font>
    <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>copyOfItems<font color="#990000">.</font>length <font color="#990000">&gt;</font> <font color="#993399">0</font><font color="#990000">)</font>
      <b><font color="#000000">setTimeout</font></b><font color="#990000">(</font>arguments<font color="#990000">.</font>callee<font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
    <b><font color="#0000FF">else</font></b>
      <b><font color="#000000">callback</font></b><font color="#990000">(</font>items<font color="#990000">)</font>
  <font color="#FF0000">}</font><font color="#990000">,</font> minTimeToStart<font color="#990000">);</font>
<font color="#FF0000">}</font> 

</tt></pre>
<p>Newer browsers support web workers. Web workers does not run in the<br />
UI-thread and does not affect responsiveness at all. Their environment<br />
is limited to allow this to work. It is limited to:</p>
<ul>
<li>A navigator object, which contains only four properties: appName,<br />
appVersion, user Agent, and platform.</li>
<li>A location object (same as on window, except all properties are read-only)</li>
<li>A self object that points to the global worker object</li>
<li>An importScripts() method that is used to load external JavaScript for<br />
use in the worker</li>
<li>All ECMAScript objects, such as Object, Array, Date, etc.</li>
<li>The XMLHttpRequest constructor</li>
<li>The setTimeout() and setInterval() methods</li>
<li>A close() method that stops the worker immediately</li>
</ul>
<p>It is not possible to create a <em>WebWorker</em> from code. It needs to be<br />
started with its own javascript file. You can however communicate with<br />
it through events.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">// Application code</font></i>
<b><font color="#0000FF">var</font></b> worker <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">Worker</font></b><font color="#990000">(</font><font color="#FF0000">"code.js"</font><font color="#990000">);</font>
worker<font color="#990000">.</font>onmessage <font color="#990000">=</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>event<font color="#990000">)</font> <font color="#FF0000">{</font>
  <b><font color="#000000">alert</font></b><font color="#990000">(</font>event<font color="#990000">.</font>data<font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">;</font>
worker<font color="#990000">.</font><b><font color="#000000">postMessage</font></b><font color="#990000">(</font><font color="#FF0000">"Tapir"</font><font color="#990000">);</font> 

<i><font color="#9A1900">// Worker code (code.js)</font></i>
<i><font color="#9A1900">//inside code.js</font></i>
<b><font color="#000000">importScripts</font></b><font color="#990000">(</font><font color="#FF0000">"file1.js"</font><font color="#990000">,</font> <font color="#FF0000">"file2.js"</font><font color="#990000">);</font> <i><font color="#9A1900">// importing some files</font></i> 

self<font color="#990000">.</font>onmessage <font color="#990000">=</font> <b><font color="#0000FF">function</font></b><font color="#990000">(</font>event<font color="#990000">)</font> <font color="#FF0000">{</font>
  self<font color="#990000">.</font><b><font color="#000000">postMessage</font></b><font color="#990000">(</font><font color="#FF0000">"Hello, "</font> <font color="#990000">+</font> event<font color="#990000">.</font>data <font color="#990000">+</font> <font color="#FF0000">"!"</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">;</font> 

</tt></pre>
<p>Any code that takes longer than 100 milliseconds to run should be<br />
refactored to use webworkers to decrease the load on the UI-thread.</p>
<h2>Ajax</h2>
<p>Favor lightweight formats in general; the best is JSON and<br />
a character-delimited custom format. If the data set is large and parse<br />
time becomes an issue, use one of these two techniques:</p>
<p>JSON-P data, fetched using dynamic script tag insertion. This treats the<br />
data as executable JavaScript, not a string, and allows for extremely<br />
fast parsing. This can be used across domains, but shouldn't be used<br />
with sensitive data.</p>
<p>A character-delimited custom format, fetched using either XHR or dynamic<br />
script tag insertion and parsed using <code>split()</code>. This technique parses<br />
extremely large datasets slightly faster than the JSON-P technique, and<br />
generally has a smaller file size.</p>
<p><em>XML has no place in high-performance Ajax.</em></p>
<p>Cache data! The fastest Ajax request is one that you don't have to make.<br />
There are two main ways of preventing an unnecessary request:</p>
<ul>
<li>On the server side, set HTTP headers that ensure your response will be<br />
cached in the browser.</li>
<li>On the client side, store fetched data locally so that it doesn't have<br />
be requested again.</li>
</ul>
<p>Multipart XHR can be used to reduce the number of requests, and can<br />
handle different file types in a single response, though it does not<br />
cache the resources received.</p>
<p>Some more guidelines that will help your Ajax appear to be faster:</p>
<ul>
<li>Reduce the number of requests you make, either by concatenating<br />
JavaScript and CSS files, or by using MXHR.</li>
<li>Improve the perceived loading time of your page by using Ajax to fetch<br />
less important files after the rest of the page has loaded.</li>
<li>Ensure your code fails gracefully and can handle problems on the<br />
server side.</li>
<li>Know when to use a robust Ajax library and when to write your own<br />
low-level Ajax code.</li>
</ul>
<h2>Programming Practices</h2>
<ul>
<li>Avoid the use of eval() and the Function() constructor.</li>
<li>Pass functions into setTimeout() and setInterval() instead of strings.</li>
<li>Use object and array literals when creating new objects and arrays.</li>
<li>Avoid doing the same work repeatedly.</li>
<li>Use lazy loading or conditional advance loading when browser-detection<br />
logic is necessary.</li>
<li>When performing mathematical operations, consider using bitwise<br />
operators that work directly on the underlying representation of the<br />
number.</li>
<li>Native methods are always faster than anything you can write in<br />
JavaScript.</li>
</ul>
<h2>Building and Deploying</h2>
<p>The build and deployment process can have a tremendous impact on the<br />
performance of a JavaScript-based application. The most important steps<br />
in this process are:</p>
<ul>
<li>Combining JavaScript files to reduce the number of HTTP requests</li>
<li>Minifying JavaScript files using the YUI Compressor</li>
<li>Serving JavaScript files compressed (gzip encoding)</li>
<li>Making JavaScript files cacheable by setting the appropriate HTTP<br />
response headers</li>
<li>Work around caching issues by appending a timestamp to filenames</li>
<li>Using a Content Delivery Network to serve JavaScript files;</li>
<li>All these steps should be automated using build tools</li>
</ul>
<h2>Tools</h2>
<h3>Minification</h3>
<ul>
<li>The <a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a>.</li>
<li><a href="http://github.com/jlecomte/smasher">Smasher</a></li>
<li><a href="http://www.crockford.com/javascript/jsmin.html">JSMin</a></li>
<li><a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a></li>
</ul>
<h3>Profiling</h3>
<ul>
<li>The <a href="http://developer.yahoo.com/yui/profiler/">YUI Profiler</a>,</li>
<li><a href="http://code.google.com/speed/page-speed/">Page Speed</a></li>
<li><a href="http://developer.yahoo.com/yslow/">YSlow</a></li>
<li><a href="http://ajax.dynatrace.com/pages/">dynaTrace Ajax Edition</a></li>
</ul>
<h3>Development</h3>
<ul>
<li><a href="http://www.getfirebug.com/">Firebug</a></li>
<li>Internet Explorer Developer Tools</li>
<li>Safari Web Inspector</li>
<li>Chrome Developer Tools</li>
</ul>
<h3>Proxies</h3>
<ul>
<li><a href="http://www.fiddler2.com/fiddler2/">Fiddler</a></li>
<li><a href="http://www.charlesproxy.com/">Charles Proxy</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/03/28/high-performance-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Host Specific Routing Via the iPhone on OSX</title>
		<link>http://blog.jayway.com/2011/03/01/host-specific-routing-via-the-iphone-on-osx/</link>
		<comments>http://blog.jayway.com/2011/03/01/host-specific-routing-via-the-iphone-on-osx/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 19:59:39 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[macosx]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[routing]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7243</guid>
		<description><![CDATA[If you are working as a consultant, it is sometimes not easy to get proper network access when you are using your customer's network. The easiest way to solve this problem is to connect via your mobile phone. But, the 3G network is not always the fastest and it would be nice to use the [...]]]></description>
			<content:encoded><![CDATA[
<p>If you are working as a consultant, it is sometimes not easy to get<br />
proper network access when you are using your customer's network. The<br />
easiest way to solve this problem is to connect via your mobile phone.<br />
But, the 3G network is not always the fastest and it would be nice to<br />
use the normal network for certain tasks and the 3G network for others.</p>
<p>The problem in my case was that I wanted to connect to Github but, SSH<br />
traffic was blocked on the network where I was working. So what to do?<br />
I quick mail to the Jayway tech mailing list, lets me know that in Linux<br />
there is a <code>route</code> command, that can solve this problem. (Un)Fortunately<br />
I am not using Linux on my development machine, but it turns out there<br />
is a <code>route(8)</code> command in BSD too, with slightly different syntax.</p>
<h3>Find the IP-address of the host I wish to connect to</h3>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Find the IP-address of the host I wish to connect to</font></i>
$ nslookup github<font color="#990000">.</font>com
Server<font color="#990000">:</font>		<font color="#993399">195.58</font><font color="#990000">.</font><font color="#993399">103.21</font>
Address<font color="#990000">:</font>	<font color="#993399">195.58</font><font color="#990000">.</font><font color="#993399">103.21</font><i><font color="#9A1900">#53</font></i> 

Non-authoritative answer<font color="#990000">:</font>
Name<font color="#990000">:</font>	github<font color="#990000">.</font>com
Address<font color="#990000">:</font> <font color="#993399">207.97</font><font color="#990000">.</font><font color="#993399">227.239</font> 

</tt></pre>
<h3>Find out the gateway of the interface of the cell phone</h3>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Find out the gateway of the interface of the cell phone</font></i>
$ ifconfig
lo0<font color="#990000">:</font> <font color="#009900">flags</font><font color="#990000">=</font><font color="#993399">8049</font><font color="#990000">&lt;</font>UP<font color="#990000">,</font>LOOPBACK<font color="#990000">,</font>RUNNING<font color="#990000">,</font>MULTICAST<font color="#990000">&gt;</font> mtu <font color="#993399">16384</font>
	inet <font color="#993399">127.0</font><font color="#990000">.</font><font color="#993399">0.1</font> netmask <font color="#993399">0xff000000</font>
	inet6 <font color="#990000">::</font><font color="#993399">1</font> prefixlen <font color="#993399">128</font>
	inet6 fe80<font color="#990000">::</font><font color="#993399">1</font><font color="#990000">%</font>lo0 prefixlen <font color="#993399">64</font> scopeid <font color="#993399">0x1</font>
<font color="#990000">...</font>
vmnet8<font color="#990000">:</font> <font color="#009900">flags</font><font color="#990000">=</font><font color="#993399">8863</font><font color="#990000">&lt;</font>UP<font color="#990000">,</font>BROADCAST<font color="#990000">,</font>SMART<font color="#990000">,</font>RUNNING<font color="#990000">,</font>SIMPLEX<font color="#990000">,</font>MULTICAST<font color="#990000">&gt;</font> mtu <font color="#993399">1500</font>
	ether <font color="#993399">00</font><font color="#990000">:</font><font color="#993399">50</font><font color="#990000">:</font><font color="#993399">56</font><font color="#990000">:</font>c0<font color="#990000">:</font><font color="#993399">00</font><font color="#990000">:</font><font color="#993399">08</font>
	inet <font color="#993399">192.168</font><font color="#990000">.</font><font color="#993399">105.1</font> netmask <font color="#993399">0xffffff00</font> broadcast <font color="#993399">192.168</font><font color="#990000">.</font><font color="#993399">105.255</font>
en3<font color="#990000">:</font> <font color="#009900">flags</font><font color="#990000">=</font><font color="#993399">8863</font><font color="#990000">&lt;</font>UP<font color="#990000">,</font>BROADCAST<font color="#990000">,</font>SMART<font color="#990000">,</font>RUNNING<font color="#990000">,</font>SIMPLEX<font color="#990000">,</font>MULTICAST<font color="#990000">&gt;</font> mtu <font color="#993399">1500</font>
	ether <font color="#993399">06</font><font color="#990000">:</font>1e<font color="#990000">:</font><font color="#993399">64</font><font color="#990000">:</font><font color="#993399">00</font><font color="#990000">:</font><font color="#993399">34</font><font color="#990000">:</font>1a
	inet6 fe80<font color="#990000">::</font>41e<font color="#990000">:</font>64ff<font color="#990000">:</font>fe00<font color="#990000">:</font>341a<font color="#990000">%</font>en3 prefixlen <font color="#993399">64</font> scopeid <font color="#993399">0xa</font>
	inet <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.3</font> netmask <font color="#993399">0xfffffff0</font> broadcast <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.15</font>
	media<font color="#990000">:</font> 10baseT/UTP
	status<font color="#990000">:</font> active
</tt></pre>
<p>In my case the interface I want to use is the last one, <strong>en3</strong> with <strong>inet<br />
172.20.10.3</strong> but if you are unsure, you can open the network preference<br />
pane and look it up there.</p>
<p>Now I need to find out what the gateway for this interface is. I can<br />
do this with <code>netstat -r</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Show the routing table</font></i>
$ netstat -r
Routing tables

Internet<font color="#990000">:</font>
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            <font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.254</font>       UGSc           <font color="#993399">10</font>        <font color="#993399">0</font>     en1
default            <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font>        UGScI           <font color="#993399">0</font>        <font color="#993399">0</font>     en3
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10</font><font color="#990000">/</font><font color="#993399">24</font>        link<i><font color="#9A1900">#5             UCS             3        0     en1</font></i>
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.88</font>        localhost          UHS             <font color="#993399">1</font>      <font color="#993399">811</font>     lo0
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.254</font>       <font color="#993399">0</font><font color="#990000">:</font><font color="#993399">14</font><font color="#990000">:</font>4f<font color="#990000">:</font><font color="#993399">69</font><font color="#990000">:</font>da<font color="#990000">:</font><font color="#993399">13</font>   UHLWI          <font color="#993399">10</font>       <font color="#993399">41</font>     en1    <font color="#993399">661</font>
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.255</font>       link<i><font color="#9A1900">#5             UHLWbI          1       78     en1</font></i>
<font color="#993399">127</font>                localhost          UCS             <font color="#993399">0</font>        <font color="#993399">0</font>     lo0
<font color="#990000">...</font> 

Internet6<font color="#990000">:</font>
Destination        Gateway            Flags         Netif Expire
localhost          localhost          UH              lo0
fe80<font color="#990000">::%</font>lo0         localhost          Uc              lo0
localhost          link<i><font color="#9A1900">#1             UHL             lo0</font></i>
<font color="#990000">...</font> 

</tt></pre>
<p>Close to the top we find the entry for our interface <strong>en3</strong>.</p>
<pre><code>default            172.20.10.1        UGScI           0        0     en3
</code></pre>
<p>Now we have the ip to the gateway that we wanted. So now the last step<br />
is to make sure that access to github.com is routed via this gateway.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900">#route command -host destination gateway</font></i>
$ sudo route add -host <font color="#993399">207.97</font><font color="#990000">.</font><font color="#993399">227.239</font> <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font>
add host <font color="#993399">207.97</font><font color="#990000">.</font><font color="#993399">227.239</font><font color="#990000">:</font> gateway <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font> 

</tt></pre>
<p>That's it, I'm done. To make sure the routing is changed I check the<br />
routing table with <code>netstat -r</code> again, to make sure the new entry is in it.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>$ netstat -r
Routing tables

Internet<font color="#990000">:</font>
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            <font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.254</font>       UGSc           <font color="#993399">30</font>        <font color="#993399">0</font>     en1
default            <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font>        UGScI           <font color="#993399">0</font>        <font color="#993399">0</font>     en3
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10</font><font color="#990000">/</font><font color="#993399">24</font>        link<i><font color="#9A1900">#5             UCS             4        0     en1</font></i>
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.88</font>        localhost          UHS             <font color="#993399">0</font>      <font color="#993399">973</font>     lo0
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.254</font>       <font color="#993399">0</font><font color="#990000">:</font><font color="#993399">14</font><font color="#990000">:</font>4f<font color="#990000">:</font><font color="#993399">69</font><font color="#990000">:</font>da<font color="#990000">:</font><font color="#993399">13</font>   UHLWI          <font color="#993399">27</font>       <font color="#993399">11</font>     en1    <font color="#993399">708</font>
<font color="#993399">10.67</font><font color="#990000">.</font><font color="#993399">10.255</font>       ff<font color="#990000">:</font>ff<font color="#990000">:</font>ff<font color="#990000">:</font>ff<font color="#990000">:</font>ff<font color="#990000">:</font>ff  UHLWbI          <font color="#993399">1</font>       <font color="#993399">14</font>     en1
<font color="#993399">127</font>                localhost          UCS             <font color="#993399">0</font>        <font color="#993399">0</font>     lo0
<font color="#990000">...</font>
github<font color="#990000">.</font>com         <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font>        UGHS            <font color="#993399">0</font>       <font color="#993399">43</font>     en3

Internet6<font color="#990000">:</font>
Destination        Gateway            Flags         Netif Expire
localhost          localhost          UH              lo0
fe80<font color="#990000">::%</font>lo0         localhost          Uc              lo0
localhost          link<i><font color="#9A1900">#1             UHL             lo0</font></i>
fe80<font color="#990000">::%</font>en0         link<i><font color="#9A1900">#4             UC              en0</font></i> 

</tt></pre>
<p>Sure enough, there it is, <code>github.com</code>. Finally I can now <code>git pull</code><br />
and everything works <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>If I want to remove the entry, when I'm working elsewhere, I do that with</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt>
$ sudo route delete -host <font color="#993399">207.97</font><font color="#990000">.</font><font color="#993399">227.239</font> <font color="#993399">172.20</font><font color="#990000">.</font><font color="#993399">10.1</font> 

</tt></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/03/01/host-specific-routing-via-the-iphone-on-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Continuous Delivery</title>
		<link>http://blog.jayway.com/2011/02/26/continuous-delivery/</link>
		<comments>http://blog.jayway.com/2011/02/26/continuous-delivery/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 14:59:32 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[Deployment]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7234</guid>
		<description><![CDATA[I just finished the book, Continuous Delivery by Jez Humble and David Farley. It's a great book, but it is a bit wordy (512 pages), so here is the gist of it. Continuous delivery builds on a simple foundation, continuous integration, automation, and version control. But in order to get it to work you must [...]]]></description>
			<content:encoded><![CDATA[
<p>I just finished the book, <a href="http://www.amazon.com/gp/product/0321601912?tag=thtasta-20">Continuous Delivery</a> by Jez Humble and David Farley. It's a great book, but it is a bit wordy (512 pages), so here is the gist of it.</p>
<p>Continuous delivery builds on a simple foundation, continuous integration,<br />
automation, and version control. But in order to get it to work you must<br />
version control and automate everything. It's that simple! <em>Version control and<br />
automate everything!</em> Everything means different things to different people,<br />
but to Jez and David it means <em>a lot</em>.</p>
<p>You should version control:</p>
<ul>
<li>Source code and test code</li>
<li>Assets, images, media files, base data, etc.</li>
<li>Build scripts</li>
<li>Deployment scripts</li>
<li>Secret configuration settings, like keys and passwords</li>
<li>Environments, development, production, test, staging, ...</li>
</ul>
<p>The first three you are hopefully doing already, but the fourth is unusual<br />
and the fifth and sixth, I rarely see anywhere. Secret settings should be kept<br />
in version control too, just don't put them into the public repository. Here<br />
are some question to ask yourself:</p>
<ul>
<li>Can you set up a complete development machine with one command?</li>
<li>Can you set up the deployment pipeline machine with one command?</li>
<li>Can you set up a fresh test environment with one command?</li>
<li>Can you set up a complete production environment with one command?</li>
<li>Can you upgrade the application, including data migrations, with one command?</li>
<li>Can you roll it back, in case it fails, with one command?</li>
</ul>
<h2>The Deployment Pipeline</h2>
<p>Continuous Delivery uses a Continuous Itegration server to drive the<br />
<em>Deployment Pipeline</em>. In the simplest form of application, a deployment<br />
pipeline may have only one step. This step, runs all the tests and if all goes<br />
well it will deploy the application. But as soon as the application grows this<br />
will take too long, the single step needs to become a pipeline.</p>
<p>A deployment pipeline may have arbitrary many steps and they should be as<br />
automated as possible. A common setup of a deployment pipeline contains the<br />
following steps.</p>
<ul>
<li>The Commit Stage (automatic)</li>
<li>The Integration Test Stage (automatic)</li>
<li>The Acceptance Test Stage (automatic)</li>
<li>Exploratory Testing Stage (manual)</li>
<li>Perfomance Testing Stage (automatic)</li>
<li>Deployment Stage (triggered manually)</li>
</ul>
<p>The main reason for the deployment pipeline is to provide feedback as<br />
fast as possible. The commit stage typically only contains unit tests<br />
that run very fast. The steps after that only run if the step before<br />
succeeds.</p>
<p>The steps above may contain everything you need, too much, or in the<br />
wrong order.  Feel free to remove and re-order as you please. If<br />
performance testing is cheaper than exploratory testing, in your case,<br />
switch them.  There is no one-solution-fits-all when it comes to<br />
pipelines. Customize it, to fit your needs.</p>
<p>One important aspect of the deployment pipeline that I rarely see is that the<br />
final product should be created by the first commit stage and then resused by<br />
the other stages. Very often I see that the separate stages check out and<br />
build a new version from scratch, albeit a tagged version but still. The reuse<br />
of the same artifact through all the steps not only ensures that the same<br />
artifact has passed all the stages, it also speeds up the deployment pipeline,<br />
removing redundant work.</p>
<p>Another thing worth noting is that the manual steps described above should be<br />
as automatic as possible. If a tester want to test the latest build that has<br />
passed the acceptance test stage, he should be able to do it with one click.<br />
That one click may involve:</p>
<ul>
<li>Create a fresh production like environment.</li>
<li>Install the selected artifact into the new envrionment.</li>
<li>Apply all the relevant configuration.</li>
</ul>
<p>If the tester decides that the build should fail or succeed, he should be able<br />
to do that with one click. And, the feedback with the result should be fed back<br />
into the deploynment cycle so that the developers know that it failed, and why,<br />
or the operation guys knows that there is a new version ready to deploy.</p>
<h2>Conclusion</h2>
<p>The book is a good read and I highly recommend it. In the process of describing<br />
continuous delivery, it provides tons of useful tips on keeping your<br />
code and data under control. Among other things it describes.</p>
<ul>
<li>Release strategies, such as canary relasing and blue-green deployment.</li>
<li>Test strategies for acceptance tests, capacity test, etc.</li>
<li>Why not to use long-lived feature branches and how to avoid it.</li>
<li>Dependency management</li>
<li>...</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/02/26/continuous-delivery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic For The Win</title>
		<link>http://blog.jayway.com/2011/01/09/dynamic-for-the-win/</link>
		<comments>http://blog.jayway.com/2011/01/09/dynamic-for-the-win/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 20:55:00 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7023</guid>
		<description><![CDATA[Everything passes, everything. -- Anthony de Mello, Awareness Take a look at what is happening around you. Relational databases are replaced by NoSQL databases. SOAP is replaced by REST, Javascript is dominating web development. The only place where dynamic is not winning is in the world of programming languages. But even if you take a [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Everything passes, everything. -- Anthony de Mello, Awareness</p>
</blockquote>
<p>Take a look at what is happening around you. Relational databases are replaced by NoSQL databases. SOAP is replaced by REST, Javascript is dominating web development. The only place where dynamic is not winning is in the world of programming languages. But even if you take a look at programming languages, you can see that many of the largest most vital sites, such as Facebook and Wikipedia, are using dynamic languages. Why?</p>
<h2>Dynamic is Better</h2>
<blockquote><p> It's not the strongest of the species that survives, nor the most intelligent, but those who are most responsive to change. -- Charles Darwin</p>
</blockquote>
<p>What I mean when I say that dynamic is better is that dynamic systems are more responsive to change. And change, as they say, is the only thing that we can be sure of!</p>
<ul>
<li>If I need to add a new field to a NoSQL database, I just do it.</li>
<li>If I need to add new field to my RESTful web service, I just do it.</li>
<li>If I need to alter the behavior of my Javascript UI, I change it, reload the page, done!</li>
</ul>
<h3>Soap vs. REST</h3>
<p>REST is beating the hell out of SOAP nowadays, but you may have noticed that most "RESTful" web services aren't really RESTful. They don't follow the HATEOAS rule, they are just "Accessible over HTTP". So why the big fuss about "REST" if no one is using it as intended?  Because there are no contracts for REST! You are not constrained by WSDL and all the other bloated specifications. You can just publish your services without contracts, write some documentation and be done with it.</p>
<h3>Relational vs. NoSQL Databases</h3>
<p>NoSQL is the new rave, and most people think that it is because they allow us to handle large amounts of data, fast. I don't think that is the main reason. I think the main reason is that they are schemaless. It is easy to add or remove fields without changing the data of all the other items of similar type. The problem with relational databases is not, in most cases, that they are slow, it is that they have a rigid contract that make them painful to work with. This effect is also, often, amplified by having O/R-mappers wrap the tables in static types.</p>
<h3>Javascript vs. RIA Plugins</h3>
<p>Javascript is the clear winner as the programming language of the web. Silverlight finally threw in the towel and Java is only living in ancient bank applets. Javascript is one of the most dynamic languages that you can imagine and it has won the battle of the web! Cool!</p>
<h3>Programming Languages</h3>
<p>In the area of programming languages, both client (not web) and server, dynamic is not winning, yet. Even though languages like C# and Objective-C are becoming more and more dynamic, they are still nowhere near Ruby and Python.<br />
Java has stagnated and Scala is even more typeful than Java, although type inference relieves you from having to declare most of the types. Clojure is an interesting dynamic JVM language but it suffers since you still have to compile the code.</p>
<p>But dynamic will win when it comes to programming languages too. Why? For the same reasons that dynamic is winning everywhere else. They are more suited for solving ever changing problems. The problems I try to solve everyday, always has to do with integrating with something else, and it is much easier to do with dynamic programming languages.</p>
<p>So why is dynamic not winning in the area of client and server programming? I think there are a few reasons.</p>
<ul>
<li>
<p>Education, most of us have been taught programming at a university that propagates for statically typed, compiled languages. We have been taught that dynamic languages are bad and that they may be suitable for scripting but nothing else. It has been hammered into us, so hard that we have a hard time letting go of this.</p>
</li>
<li>
<p>Tradition, the folklore says that dynamic programming languages are too slow. The fact that they are not, has not changed this. You may remember that the argument was the same against Java fifteen years ago.</p>
</li>
<li>
<p>And then there is the main reason, dynamic is harder!</p>
</li>
</ul>
<h2>Dynamic is Harder</h2>
<p>So why isn't everyone using dynamic programming langugages? Because, it is harder. It is easier to write really bad code in dynamic languages. But, on the other hand, it is also easier to write really good code in dynamic languages.</p>
<p>It is harder to develop large systems, because when you are using convention over configuration, it is really important to know the conventions and most people don't take the time to learn what they use properly, they rely on the IDE for guiding them down the right path.<br />
If you are suffering from IDEitis, the transition to dynamic languages is going to be tough until you realize that the help you get from the IDE is also what is holding you back.</p>
<p>The problem is moved from the programming language to me, the programmer, architect or whatever title I feel entitled to. If I am using a dynamic language and I screw up, it is my fault. If I don't organize the code in a way that can be understood by others, it is my fault. If I make changes to core classes, and the system stops working, it is my fault. I am responsible! I am in control!</p>
<h3>An example, third party libraries in Ruby</h3>
<p>I remember a comment from one of my colleagues, when he found out that I had a problem with a library (a Ruby Gem) that didn't behave the way I wanted it too.</p>
<blockquote><p>Ah, you're using Ruby, then you can just fix it. -- Mattias Arthursson</p>
</blockquote>
<p>Right on Mattias, I can just fix it!</p>
<p>Almost every Ruby Gem lives at Github. The flexibility this brings is tremendous. If a library is not working the way I think it should, I can just fix it. The process goes something like this:</p>
<ul>
<li>Fork it</li>
<li>Make the appropriate changes</li>
<li>Push it back up to Github (to my fork)</li>
<li>Change the Gemfile, that holds my project dependencies, to use my fork instead.</li>
<li>Optionally, I may also send a pull requests of my patch to the maintainer of the library.</li>
<li>The maintainer takes a look at it and pulls it in, relieving me of the burden of keeping my fork up to date.</li>
</ul>
<p>The process is so simple, it is almost seamless.</p>
<p>I'll finish with another quote.</p>
<blockquote><p>It is vain to do with more, what can be done with less. -- William of Occam</p>
</blockquote>
<p>It's a new year, it is time to prepare for the future, a dynamic future! Here are some books that I can recommend, they are about Ruby and Javascript, since they are my current favorites. But if you like other dynamic languages better, knock yourself out!</p>
<h2>Books</h2>
<p>The books are ordered, by increasing level of complexity.</p>
<h3>Ruby</h3>
<ul>
<li><a href="http://www.amazon.com/Well-Grounded-Rubyist-David-Black/dp/1933988657/ref=sr_1_1?tag=thtasta-20">The Well Grounded Rubyist</a></li>
<li><a href="http://www.amazon.com/Ruby-Way-Second-Techniques-Programming/dp/0672328844/ref=sr_1_1?tag=thtasta-20">The Ruby Way</a></li>
<li><a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009/ref=sr_1_1?tag=thtasta-20">Ruby Best Practices</a></li>
<li><a href="http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros/dp/1934356476/ref=sr_1_1?tag=thtasta-20">Metaprogramming Ruby</a></li>
</ul>
<h3>Rails</h3>
<ul>
<li><a href="http://www.amazon.com/Beginning-Rails-Experts-Voice-Development/dp/1430224339/ref=sr_1_1?tag=thtasta-20">Beginning Rails</a></li>
<li><a href="http://www.amazon.com/Rails-Way-Addison-Wesley-Professional-Ruby/dp/0321601661/ref=sr_1_1?tag=thtasta-20">The Rails 3 Way</a></li>
<li><a href="http://pragprog.com/titles/jvrails/crafting-rails-applications">Crafting Rails Applications</a></li>
</ul>
<h3>Javascript</h3>
<ul>
<li><a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=sr_1_1?tag=thtasta-20">Javascript, the Good Parts</a></li>
<li><a href="http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752/ref=sr_1_1?tag=thtasta-20">Javascript Patterns</a></li>
<li><a href="http://www.amazon.com/Performance-JavaScript-Faster-Application-Interfaces/dp/059680279X/ref=sr_1_1?tag=thtasta-20">High Performance Javascript</a></li>
</ul>
<h3>jQuery</h3>
<ul>
<li><a href="http://www.amazon.com/jQuery-Action-Second-Bear-Bibeault/dp/1935182323/ref=sr_1_1?tag=thtasta-20">jQuery in Action</a></li>
<li><a href="http://www.amazon.com/jQuery-Cookbook-Solutions-Examples-Developers/dp/0596159773/ref=sr_1_1?tag=thtasta-20">jQuery Cookbook</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/01/09/dynamic-for-the-win/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Review of Crafting Rails Applications, by Jos&#233; Valim</title>
		<link>http://blog.jayway.com/2010/12/30/review-of-crafting-rails-applications-by-jos-valim/</link>
		<comments>http://blog.jayway.com/2010/12/30/review-of-crafting-rails-applications-by-jos-valim/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 11:16:37 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6934</guid>
		<description><![CDATA[Jos&#233; Valim is one of the newest members of the Rails core team. Apart from this he has also developed some good gems, Devise, Responders, and SimpleForm, that I use for almost every project. And now he has written a book, a really good book, about advanced Rails programming techniques, called Crafting Rails Applications. The [...]]]></description>
			<content:encoded><![CDATA[<p>Jos&eacute; Valim is one of the newest members of the Rails core team. Apart from this he has also developed some good gems, <a href="https://github.com/plataformatec/devise">Devise</a>, <a href="https://github.com/plataformatec/responders">Responders</a>, and <a href="https://github.com/plataformatec/simple_form">SimpleForm</a>, that I use for almost every project.</p>
<p>And now he has written a book, a <strong>really</strong> good book, about advanced Rails programming techniques, called <a href="http://pragprog.com/titles/jvrails/crafting-rails-applications">Crafting Rails Applications</a>.</p>
<p>The book is only 180 pages long, and contains 7 chapters. I wish more books was this thin. The books is written in a clear concise way. Jos&eacute;'s lightweight test-driven approach clearly introduces every problem it intends to solve, and then solves the problem by introducing a new concept in the Rails framework. Most of the solutions are only a few lines of code, showing the power of Rails and also showing the power of having intimate knowledge of the framework. By hooking into the framework at appropriate places, Jos&eacute; is able to provide elegant solutions to a range of problems.</p>
<p>I highly recommend the book to anyone interested in Rails.</p>
<p>If this was an ordinary book, this review could have ended here, but since the book is full of things I really wanted to know but didn't take the time to learn I decided to internalize them by describing parts of the book in my own words.</p>
<h2>Chapter 1, Creating our own renderer</h2>
<p>In this chapter we learn how to add a new renderer Rails. We add a renderer that handles generating PDF files with <a href="http://prawn.majesticseacreature.com/">Prawn</a>, allowing us to write code that looks like this.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> HomeController <font color="#990000">&lt;</font> ApplicationController
  <b><font color="#0000FF">def</font></b> index
    respond_to <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>format<font color="#990000">|</font>
      format<font color="#990000">.</font>html
      <i><font color="#9A1900"># This is the new capability, pdf rendering.</font></i>
      format<font color="#990000">.</font>pdf <font color="#FF0000">{</font> render <font color="#990000">:</font>pdf <font color="#990000">=&gt;</font> <font color="#FF0000">"contents"</font> <font color="#FF0000">}</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<p>To allow the following we first have to register pdf as a mime type. We can do this with:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#000080">require</font></b> <font color="#FF0000">"action_controller"</font>
<i><font color="#9A1900"># Register a new mime type</font></i>
Mime<font color="#990000">::</font>Type<font color="#990000">.</font>register <font color="#FF0000">"application/pdf"</font><font color="#990000">,</font> <font color="#990000">:</font>pdf

<i><font color="#9A1900"># Registering the type allows us to write</font></i>
respond_to <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>format<font color="#990000">|</font>
  format<font color="#990000">.</font>pdf
<b><font color="#0000FF">end</font></b>
</tt></pre>
<p>With that out of the way all we have to do is to register a renderer, that will handle the actual rendering of the registered mime type. This is done with:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#000080">require</font></b> <font color="#FF0000">"prawn"</font> 

<i><font color="#9A1900"># Adds a new renderer to ActionController.Renderers</font></i>
ActionController<font color="#990000">::</font>Renderers<font color="#990000">.</font>add <font color="#990000">:</font>pdf <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>filename<font color="#990000">,</font> options<font color="#990000">|</font>
  pdf <font color="#990000">=</font> Prawn<font color="#990000">::</font>Document<font color="#990000">.</font>new pdf<font color="#990000">.</font>text render_to_string<font color="#990000">(</font>options<font color="#990000">)</font>
  send_data<font color="#990000">(</font>pdf<font color="#990000">.</font>render<font color="#990000">,</font> <font color="#990000">:</font>filename <font color="#990000">=&gt;</font> <font color="#FF0000">"#{filename}.pdf"</font><font color="#990000">,</font>
      <font color="#990000">:</font>type <font color="#990000">=&gt;</font> <font color="#FF0000">"application/pdf"</font><font color="#990000">,</font> <font color="#990000">:</font>disposition <font color="#990000">=&gt;</font> <font color="#FF0000">"attachment"</font><font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<p>This is all that is needed to add a rendering of a new type to Rails. Jos&eacute; also goes into details about other points of interest where the rendering stack can be customized.</p>
<h2>Chapter 2, Easy models with Active Model</h2>
<p>Active Model is a Rails module that abstracts common behavior needed by different kinds om models such as Active Record, Active Resource and third party modules like Mongoid. Apart from giving developers of new models utility functionality. Active Model also provides tests for being Active Model compliant. To be compliant means to play well with the Rails view and controllers.</p>
<p>Jos&eacute; implements a model for creating sending mail. In doing so he introduces us to a bunch of Active Model modules that will do our work for us.</p>
<p>Here are the modules in alphabetical order:</p>
<h3>ActiveModel::AttributeMethods</h3>
<p>When you include <code>ActiveModel::AttributeMethods</code> in your model, you get access to helper methods that will help you define additional methods for dealing with the attributes of your model. An example</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Tapir
  <b><font color="#0000FF">include</font></b> ActiveModel<font color="#990000">::</font>AttributeMethods

  <i><font color="#9A1900"># Define the attributes </font></i>
  attr_accessor <font color="#FF0000">'name'</font><font color="#990000">,</font> <font color="#FF0000">'color'</font>

  <i><font color="#9A1900"># We want to create clear methods for every attribute</font></i>
  attribute_method_prefix <font color="#FF0000">'clear_'</font>

  <i><font color="#9A1900"># We also want to have query methods for every attribute</font></i>
  attribute_method_suffix <font color="#FF0000">'?'</font>

  <i><font color="#9A1900"># Define the attribute methods that will call the protected methods below</font></i>
  define_attribute_methods <font color="#990000">[</font><font color="#FF0000">'name'</font><font color="#990000">,</font> <font color="#FF0000">'color'</font><font color="#990000">]</font>

protected
  <i><font color="#9A1900"># The name of this method corresponds to the prefix above</font></i>
  <b><font color="#0000FF">def</font></b> clear_attribute<font color="#990000">(</font>attribute<font color="#990000">)</font>
    send<font color="#990000">(</font><font color="#FF0000">"#{attribute}="</font><font color="#990000">,</font> <b><font color="#0000FF">nil</font></b><font color="#990000">)</font>
  <b><font color="#0000FF">end</font></b>

  <i><font color="#9A1900"># The name of this method corresponds to the suffix above</font></i>
  <b><font color="#0000FF">def</font></b> attribute?<font color="#990000">(</font>attribute<font color="#990000">)</font>
    send<font color="#990000">(</font>attribute<font color="#990000">).</font>present?
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>   t <font color="#990000">=</font> Tapir<font color="#990000">.</font>new
 <font color="#990000">=&gt;</font> <i><font color="#9A1900">#&lt;Tapir:0x00000102769b90&gt; </font></i>
<font color="#990000">&gt;</font> t<font color="#990000">.</font>name<font color="#990000">?</font>
 <font color="#990000">=&gt;</font> <b><font color="#0000FF">false</font></b>
<font color="#990000">&gt;</font> t<font color="#990000">.</font><font color="#009900">name</font><font color="#990000">=</font> <font color="#FF0000">'kalle'</font>
 <font color="#990000">=&gt;</font> <font color="#FF0000">"kalle"</font>
<font color="#990000">&gt;</font> t<font color="#990000">.</font>name<font color="#990000">?</font>
 <font color="#990000">=&gt;</font> <b><font color="#0000FF">true</font></b>
<font color="#990000">&gt;</font> t<font color="#990000">.</font>clear_name
 <font color="#990000">=&gt;</font> nil
<font color="#990000">&gt;</font> t<font color="#990000">.</font>name<font color="#990000">?</font>
 <font color="#990000">=&gt;</font> <b><font color="#0000FF">false</font></b>
<font color="#990000">&gt;</font> t<font color="#990000">.</font>name
</tt></pre>
<p>This is the gist of attribute methods, there is also an affix method that will allow you to add both a suffix and a prefix to your attributes. It is also worth taking a look inside the implmentation of this module, since the methods are defined on first usage through method_missing.</p>
<h3>ActiveModel::Callbacks</h3>
<p><code>ActiveModel::Callbacks</code> provides methods for creating <code>before_</code>, <code>after_</code>, and `around_' callbacks to your model methods. An example:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Tapir
  extend ActiveModel<font color="#990000">::</font>Callbacks

  define_model_callbacks <font color="#990000">:</font>snort

  <b><font color="#0000FF">def</font></b> snort
    _run_snort_callbacks <b><font color="#0000FF">do</font></b>
      snort_snort_snort
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">end</font></b>

</tt></pre>
<p>Then in inheriting classes I can use the callbacks as such:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> MountainTapir <font color="#990000">&lt;</font> Tapir
  before_snort <font color="#990000">:</font>do_before_snort

  <b><font color="#0000FF">def</font></b> before_snort
    i_am_so_cold
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<h3>ActiveModel::Conversion</h3>
<p>This module assists with converting to, and representing a specific model instance. It contains three methods.</p>
<ul>
<li><code>to_model</code>, used for converting to an active model compliant model, in the default case it just returns <code>self</code>.</li>
<li><code>to_param</code>, used for representing a model in routing.</li>
<li><code>to_key</code>, used for representing a model from in a dom page.</li>
</ul>
<h3>ActiveModel::Naming and ActiveModel::Translation</h3>
<p><code>ActiveModel::Naming</code> contains one method <code>model_name</code>, that returns a subclass of String, that handles pluralization, etc. It has methods such as <code>plural</code>, <code>human</code>.</p>
<p><code>ActiveModel::Translation</code> deals with I18N by adding functionality to <code>human</code> that handles the name lookup by keys using the preferred <code>I18n.backend</code>.</p>
<h3>ActiveModel::Validations</h3>
<p>The last, but not least of the models, <code>ActiveModel::Validations</code>, deals with validations. Jos&eacute; demostrates how naming convetions and Rails constant lookup allow us to add new validators to Rails with ease. An example:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">module</font></b> Validators
  <b><font color="#0000FF">class</font></b> TapirKindValidator <font color="#990000">&lt;</font> ActiveModel<font color="#990000">::</font>EachValidator
    <b><font color="#0000FF">def</font></b> validate_each<font color="#990000">(</font>record<font color="#990000">,</font> attribute<font color="#990000">,</font> value<font color="#990000">)</font>
      <b><font color="#0000FF">unless</font></b> <font color="#990000">[:</font>baird<font color="#990000">,</font> <font color="#990000">:</font>mountain<font color="#990000">,</font> <font color="#990000">:</font>malayan<font color="#990000">,</font> <font color="#990000">:</font>lowland<font color="#990000">].</font><b><font color="#0000FF">include</font></b><font color="#990000">?</font> value
        record<font color="#990000">.</font>errors<font color="#990000">.</font>add<font color="#990000">(</font>attribute<font color="#990000">,</font> <font color="#990000">:</font>invalid<font color="#990000">,</font> options<font color="#990000">)</font>
      <b><font color="#0000FF">end</font></b>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<p>If I now include my new validator wherever I want to use it. I can write code like:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Tapir
  <b><font color="#0000FF">include</font></b> Validators

  attr_accessor <font color="#990000">:</font>kind
  validates <font color="#990000">:</font>kind<font color="#990000">,</font> <font color="#990000">:</font>tapir_kind <font color="#990000">=&gt;</font> <b><font color="#0000FF">true</font></b>
<b><font color="#0000FF">end</font></b>

t <font color="#990000">=</font> Tapir<font color="#990000">.</font>new <font color="#990000">:</font>kind <font color="#990000">=&gt;</font> <font color="#990000">:</font>mexican
t<font color="#990000">.</font>valid?
<i><font color="#9A1900"># =&gt; false</font></i>
t<font color="#990000">.</font>errors<font color="#990000">[:</font>kind<font color="#990000">]</font>
<i><font color="#9A1900"># ["is invalid"]</font></i>

</tt></pre>
<p>This example is unnecessarily complicated, since the validation could be fixed with:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Tapir
    attr_accessor <font color="#990000">:</font>kind
    validates_inclusion_of <font color="#990000">:</font>kind<font color="#990000">,</font> <font color="#990000">:</font><b><font color="#0000FF">in</font></b> <font color="#990000">=&gt;</font>  <font color="#990000">[:</font>baird<font color="#990000">,</font> <font color="#990000">:</font>mountain<font color="#990000">,</font> <font color="#990000">:</font>malayan<font color="#990000">,</font> <font color="#990000">:</font>lowland<font color="#990000">]</font>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<h3>append_view_path</h3>
<p>There is one more interesting thing in this chapter, <code>append_view_path</code>. It can be used to add a directory inside a gem to the view lookup path. This is essential to be able to deliver custom Rails components, Railties, with default views.</p>
<h2>Chapter 3, Building a template management system</h2>
<p>In the third chapter of the book,Jos&eacute; takes on view resolvers. A resolver is responsible for looking up the appropriate template for a given request. The traditional resolver looks up the templates by path in the file system, but Jos&eacute; guides us through creating a resolver that looks up the template in a database. Perfect for the core of a CMS system. I'm going to alter Jos&eacute;'s resolver to do the lookup in MongoDB instead.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Most of this code is borrowed directly from Jos&amp;eacute; Valim</font></i>
<i><font color="#9A1900"># Only the Mongoid Part is written by me <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </font></i>
<b><font color="#000080">require</font></b> <font color="#FF0000">'mongoid'</font>
<b><font color="#000080">require</font></b> <font color="#FF0000">'singleton'</font>

<b><font color="#0000FF">class</font></b> MongoTemplate
  <b><font color="#0000FF">include</font></b> Mongoid<font color="#990000">::</font>Document
  field <font color="#990000">:</font>body
  field <font color="#990000">:</font>path
  field <font color="#990000">:</font>locale
  field <font color="#990000">:</font>format
  field <font color="#990000">:</font>handler
  field <font color="#990000">:</font>partial<font color="#990000">,</font> <font color="#990000">:</font>type <font color="#990000">=&gt;</font> Boolean
  field <font color="#990000">:</font>updated_at<font color="#990000">,</font> <font color="#990000">:</font>type <font color="#990000">=&gt;</font> Date

  validates <font color="#990000">:</font>body<font color="#990000">,</font> <font color="#990000">:</font>path<font color="#990000">,</font> <font color="#990000">:</font>presence <font color="#990000">=&gt;</font> <b><font color="#0000FF">true</font></b>
  validates <font color="#990000">:</font>format<font color="#990000">,</font> <font color="#990000">:</font>inclusion <font color="#990000">=&gt;</font> Mime<font color="#990000">::</font>SET<font color="#990000">.</font>symbols<font color="#990000">.</font>map<font color="#990000">(&amp;:</font>to_s<font color="#990000">)</font>
  validates <font color="#990000">:</font>locale<font color="#990000">,</font> <font color="#990000">:</font>inclusion <font color="#990000">=&gt;</font> I18n<font color="#990000">.</font>available_locales<font color="#990000">.</font>map<font color="#990000">(&amp;:</font>to_s<font color="#990000">)</font>
  validates <font color="#990000">:</font>handler<font color="#990000">,</font> <font color="#990000">:</font>inclusion <font color="#990000">=&gt;</font>
    ActionView<font color="#990000">::</font>Template<font color="#990000">::</font>Handlers<font color="#990000">.</font>extensions<font color="#990000">.</font>map<font color="#990000">(&amp;:</font>to_s<font color="#990000">)</font>

  <b><font color="#0000FF">class</font></b> Resolver <font color="#990000">&lt;</font> ActionView<font color="#990000">::</font>Resolver
    <b><font color="#0000FF">include</font></b> Singleton
  protected
    <b><font color="#0000FF">def</font></b> find_templates<font color="#990000">(</font>name<font color="#990000">,</font> prefix<font color="#990000">,</font> partial<font color="#990000">,</font> details<font color="#990000">)</font>
      conditions <font color="#990000">=</font> <font color="#FF0000">{</font>
        <font color="#990000">:</font>path <font color="#990000">=&gt;</font> normalize_path<font color="#990000">(</font>name<font color="#990000">,</font> prefix<font color="#990000">),</font>
        <font color="#990000">:</font>locale <font color="#990000">=&gt;</font> normalize_array<font color="#990000">(</font>details<font color="#990000">[:</font>locale<font color="#990000">]).</font>first<font color="#990000">,</font>
        <font color="#990000">:</font>format <font color="#990000">=&gt;</font> normalize_array<font color="#990000">(</font>details<font color="#990000">[:</font>formats<font color="#990000">]).</font>first<font color="#990000">,</font>
        <font color="#990000">:</font>handler<font color="#990000">.</font><b><font color="#0000FF">in</font></b> <font color="#990000">=&gt;</font> normalize_array<font color="#990000">(</font>details<font color="#990000">[:</font>handlers<font color="#990000">]),</font>
        <font color="#990000">:</font>partial <font color="#990000">=&gt;</font> partial <font color="#990000">||</font> <b><font color="#0000FF">false</font></b>
      <font color="#FF0000">}</font>

      MongoTemplate<font color="#990000">.</font>where<font color="#990000">(</font>conditions<font color="#990000">).</font>map <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>record<font color="#990000">|</font>
        initialize_template<font color="#990000">(</font>record<font color="#990000">)</font>
      <b><font color="#0000FF">end</font></b>
    <b><font color="#0000FF">end</font></b>

    <i><font color="#9A1900"># Normalize name and prefix, so the tuple ["index", "users"] becomes </font></i>
    <i><font color="#9A1900"># "users/index" and the tuple ["template", nil] becomes "template". </font></i>
    <b><font color="#0000FF">def</font></b> normalize_path<font color="#990000">(</font>name<font color="#990000">,</font> prefix<font color="#990000">)</font>
      prefix<font color="#990000">.</font>present? <font color="#990000">?</font> <font color="#FF0000">"#{prefix}/#{name}"</font> <font color="#990000">:</font> name
    <b><font color="#0000FF">end</font></b>

    <i><font color="#9A1900"># Normalize arrays by converting all symbols to strings.</font></i>
    <b><font color="#0000FF">def</font></b> normalize_array<font color="#990000">(</font>array<font color="#990000">)</font>
      array<font color="#990000">.</font>map<font color="#990000">(&amp;:</font>to_s<font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>

    <i><font color="#9A1900"># Initialize an ActionView::Template object based on the record found.</font></i>
    <b><font color="#0000FF">def</font></b> initialize_template<font color="#990000">(</font>record<font color="#990000">)</font>
      source <font color="#990000">=</font> record<font color="#990000">.</font>body
      identifier <font color="#990000">=</font> <font color="#FF0000">"MongoTemplate - #{record.id} - #{record.path.inspect}"</font>
      handler <font color="#990000">=</font> ActionView<font color="#990000">::</font>Template<font color="#990000">.</font>registered_template_handler<font color="#990000">(</font>record<font color="#990000">.</font>handler<font color="#990000">)</font>
      details <font color="#990000">=</font> <font color="#FF0000">{</font>
        <font color="#990000">:</font>format <font color="#990000">=&gt;</font> Mime<font color="#990000">[</font>record<font color="#990000">.</font>format<font color="#990000">],</font>
        <font color="#990000">:</font>updated_at <font color="#990000">=&gt;</font> record<font color="#990000">.</font>updated_at<font color="#990000">,</font>
        <font color="#990000">:</font>virtual_path <font color="#990000">=&gt;</font> virtual_path<font color="#990000">(</font>record<font color="#990000">.</font>path<font color="#990000">,</font> record<font color="#990000">.</font>partial<font color="#990000">)</font>
      <font color="#FF0000">}</font>
      ActionView<font color="#990000">::</font>Template<font color="#990000">.</font>new<font color="#990000">(</font>source<font color="#990000">,</font> identifier<font color="#990000">,</font> handler<font color="#990000">,</font> details<font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>

    <i><font color="#9A1900"># Make paths as "users/user" become "users/_user" for partials.</font></i>
    <b><font color="#0000FF">def</font></b> virtual_path<font color="#990000">(</font>path<font color="#990000">,</font> partial<font color="#990000">)</font>
      <b><font color="#0000FF">return</font></b> path <b><font color="#0000FF">unless</font></b> partial
      <b><font color="#0000FF">if</font></b> index <font color="#990000">=</font> path<font color="#990000">.</font>rindex<font color="#990000">(</font><font color="#FF0000">"/"</font><font color="#990000">)</font>
        path<font color="#990000">.</font>insert<font color="#990000">(</font>index <font color="#990000">+</font> <font color="#993399">1</font><font color="#990000">,</font> <font color="#FF0000">"_"</font><font color="#990000">)</font>
      <b><font color="#0000FF">else</font></b>
        <font color="#FF0000">"_#{path}"</font>
      <b><font color="#0000FF">end</font></b>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<p>A Resolver is implemented by extending <code>ActionView::Resolver</code> and implementing the method <code>find_templates(name, prefix, partial, details)</code> There are a lot of extra information in the book, about caching etc. Obviously Rails caches the templates since it would be too slow to create a template every time. That is why we have the template as a singleton and that is why we clear the cache in the <code>after_save</code> hook above. The hook in Mongoid works exactly the same as in Active Record. Thank you Active Model!</p>
<h2>Chapter 4, Multipart Emails with Markdown and Erb</h2>
<p>In this chapter Jos&eacute; walks us through three topics. The template handler API, Rails generators and Railties. I'm going to skip over the last two in this chapter. Generators are a large topic and they deserve a blog post of their own. Jos&eacute; does a great job desribing how they work. The same goes for Railties and Rails Engines, they are definitely worthy of a blog post of their own.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>
<i><font color="#9A1900"># </font></i>
<i><font color="#9A1900"># Again most of the code is stolen from Jos&amp;eacute; <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </font></i>
<i><font color="#9A1900">#</font></i>
<b><font color="#0000FF">module</font></b> ScrambleHandler

  <i><font color="#9A1900"># Lookup the ERb handler</font></i>
  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>erb_handler
    <font color="#009900">@@erb_handler</font> <font color="#990000">||=</font> ActionView<font color="#990000">::</font>Template<font color="#990000">.</font>registered_template_handler<font color="#990000">(:</font>erb<font color="#990000">)</font>
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>call<font color="#990000">(</font>template<font color="#990000">)</font>
    <i><font color="#9A1900"># Call the erb handler, then call the new scrambler method.</font></i>
    source <font color="#990000">=</font> erb_handler<font color="#990000">.</font>call<font color="#990000">(</font>template<font color="#990000">)</font>
    <b><font color="#0000FF">if</font></b> template<font color="#990000">.</font>formats<font color="#990000">.</font><b><font color="#0000FF">include</font></b><font color="#990000">?(:</font>html<font color="#990000">)</font>
      <font color="#FF0000">"Scrambler.from_html(begin;#{source};end).to_s"</font>
    <b><font color="#0000FF">else</font></b>
      <font color="#FF0000">"Scrambler.from_text(begin;#{source};end).to_s"</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># Register our new handler, it handles index.html.scramble, show.html.scramble, ...</font></i>
ActionView<font color="#990000">::</font>Template<font color="#990000">.</font>register_template_handler <font color="#990000">:</font>scramble<font color="#990000">,</font> ScrambleHandler

<i><font color="#9A1900"># This module scrambles text.</font></i>
<i><font color="#9A1900"># Scrambler.from_text('Once upon a time a beautiful tapir came.')</font></i>
<i><font color="#9A1900"># -&gt; Ocne upon a time a baeuuitfl tpiar came.</font></i>
<b><font color="#0000FF">module</font></b> Scrambler
  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>from_html html
    doc <font color="#990000">=</font> Nokogiri<font color="#990000">::</font>HTML<font color="#990000">(</font>html<font color="#990000">)</font>
    doc<font color="#990000">.</font>xpath<font color="#990000">(</font><font color="#FF0000">'//text()'</font><font color="#990000">).</font>each <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>node<font color="#990000">|</font>
      node<font color="#990000">.</font>content <font color="#990000">=</font> from_text<font color="#990000">(</font>node<font color="#990000">.</font>content<font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>
    doc<font color="#990000">.</font>to_html
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>from_text text
    tokenize<font color="#990000">(</font>text<font color="#990000">).</font>map<font color="#FF0000">{</font><font color="#990000">|</font>token<font color="#990000">|</font> scramble<font color="#990000">(</font>token<font color="#990000">)</font><font color="#FF0000">}</font><font color="#990000">.</font>join<font color="#990000">(</font><font color="#FF0000">''</font><font color="#990000">)</font>
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>tokenize text
    <b><font color="#0000FF">return</font></b> <b><font color="#0000FF">nil</font></b> <b><font color="#0000FF">unless</font></b> text
    text<font color="#990000">.</font>scan<font color="#990000">(</font><font color="#FF6600">/(\p{Word}+|\W+|)/</font>um<font color="#990000">).</font>flatten
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>scramble word
    <b><font color="#0000FF">return</font></b> word <b><font color="#0000FF">if</font></b> word <font color="#990000">=~</font> <font color="#FF6600">/\W/</font>
    <b><font color="#0000FF">return</font></b> word <b><font color="#0000FF">if</font></b> word<font color="#990000">.</font>size <font color="#990000">&lt;</font> <font color="#993399">4</font>
    arr <font color="#990000">=</font> word<font color="#990000">.</font>split<font color="#990000">(</font><font color="#FF6600">//</font><font color="#990000">)</font>
    arr<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">]</font> <font color="#990000">+</font> arr<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">...-</font><font color="#993399">1</font><font color="#990000">].</font>shuffle<font color="#990000">.</font>join<font color="#990000">(</font><font color="#FF0000">''</font><font color="#990000">)</font> <font color="#990000">+</font> arr<font color="#990000">[-</font><font color="#993399">1</font><font color="#990000">]</font>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<h2>Chapter 5, Publishing and subscribing to your applications events</h2>
<p>In this chapter Jos&eacute; uses the Notifications API to store notifcations in a MongoDB. He also shows how to use Rack middleware to configure what request to log. In order to configure the middleware he uses a Rails Engine. I'm just going to show how the Notifications API works here, and I'll let you read the rest in the book.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Subscribe to all events and print them to the console</font></i>
ActiveSupport<font color="#990000">::</font>Notifications<font color="#990000">.</font>subscribe <b><font color="#0000FF">do</font></b> <font color="#990000">|*</font>args<font color="#990000">|</font>
  p ActiveSupport<font color="#990000">::</font>Notifications<font color="#990000">::</font>Event<font color="#990000">.</font>new<font color="#990000">(*</font>args<font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># Instrument the rendering of Foo</font></i>
ActiveSupport<font color="#990000">::</font>Notifications<font color="#990000">.</font>instrument<font color="#990000">(:</font>render<font color="#990000">,</font> <font color="#990000">:</font>extra <font color="#990000">=&gt;</font> <font color="#990000">:</font>information<font color="#990000">)</font> <b><font color="#0000FF">do</font></b>
  render <font color="#990000">:</font>text <font color="#990000">=&gt;</font> <font color="#FF0000">"Foo"</font>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<h2>Chapter 6, DRY controllers with Responders</h2>
<p>In this chapter Jos&eacute; goes through the implementation of responders. This implementation allows you to DRY up your controllers by factoring out common behavior.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">def</font></b> index
  <font color="#009900">@users</font> <font color="#990000">=</font> User<font color="#990000">.</font>all
  respond_to <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>format<font color="#990000">|</font>
    format<font color="#990000">.</font>html <i><font color="#9A1900"># index.html.erb </font></i>
    format<font color="#990000">.</font>xml <font color="#FF0000">{</font> render <font color="#990000">:</font>xml <font color="#990000">=&gt;</font> <font color="#009900">@users</font> <font color="#FF0000">}</font>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># becomes</font></i>

<b><font color="#0000FF">def</font></b> index
  <font color="#009900">@users</font> <font color="#990000">=</font> User<font color="#990000">.</font>all
  respond_with<font color="#990000">(</font><font color="#009900">@users</font><font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># and</font></i>
<b><font color="#0000FF">def</font></b> create
  <font color="#009900">@user</font> <font color="#990000">=</font> User<font color="#990000">.</font>new<font color="#990000">(</font>params<font color="#990000">[:</font>user<font color="#990000">])</font>
  respond_to <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>format<font color="#990000">|</font>
  <b><font color="#0000FF">if</font></b> <font color="#009900">@user</font><font color="#990000">.</font>save
    format<font color="#990000">.</font>html <font color="#FF0000">{</font> redirect_to<font color="#990000">(</font><font color="#009900">@user</font><font color="#990000">,</font> <font color="#990000">:</font>notice <font color="#990000">=&gt;</font> <font color="#FF0000">'User was successfully created.'</font><font color="#990000">)</font> <font color="#FF0000">}</font>
    format<font color="#990000">.</font>xml <font color="#FF0000">{</font> render <font color="#990000">:</font>xml <font color="#990000">=&gt;</font> <font color="#009900">@user</font><font color="#990000">,</font> <font color="#990000">:</font>status <font color="#990000">=&gt;</font> <font color="#990000">:</font>created<font color="#990000">,</font> <font color="#990000">:</font>location <font color="#990000">=&gt;</font> <font color="#009900">@user</font> <font color="#FF0000">}</font>
  <b><font color="#0000FF">else</font></b>
    format<font color="#990000">.</font>html <font color="#FF0000">{</font> render <font color="#990000">:</font>action <font color="#990000">=&gt;</font> <font color="#FF0000">"new"</font> <font color="#FF0000">}</font>
    format<font color="#990000">.</font>xml <font color="#FF0000">{</font> render <font color="#990000">:</font>xml <font color="#990000">=&gt;</font> <font color="#009900">@user</font><font color="#990000">.</font>errors<font color="#990000">,</font> <font color="#990000">:</font>status <font color="#990000">=&gt;</font> <font color="#990000">:</font>unprocessable_entity <font color="#FF0000">}</font>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># becomes</font></i>
<b><font color="#0000FF">def</font></b> create
  <font color="#009900">@user</font> <font color="#990000">=</font> User<font color="#990000">.</font>new<font color="#990000">(</font>params<font color="#990000">[:</font>user<font color="#990000">])</font>
  flash<font color="#990000">[:</font>notice<font color="#990000">]</font> <font color="#990000">=</font> <font color="#FF0000">'User was successfully created.'</font> <b><font color="#0000FF">if</font></b> <font color="#009900">@user</font><font color="#990000">.</font>save
  respond_with<font color="#990000">(</font><font color="#009900">@user</font><font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<p>In this chapter we also learn how to replace the default generators with our own customized generators.</p>
<h2>Chapter 7, Translatable app with I18n and Redis</h2>
<p>In the final chapter Jos&eacute; shows us how the I18N backed system works. He does this by creating a backend the uses Redis instead of the default YAML files. He also developes a simple Sinatra application that he hooks into the Rails routing with the <code>mount</code> method.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Mount the Sinatra Translator::App at the /translator path.</font></i>
mount Translator<font color="#990000">::</font>App<font color="#990000">,</font> <font color="#990000">:</font>at <font color="#990000">=&gt;</font> <font color="#FF0000">"/translator"</font>
</tt></pre>
<p>Rack applications normally get the full path inside <code>ENV['PATH_INFO']</code>, but when Rails mounts an application it removes the prefix before it sends it on <code>/translator/en/pt/</code> becomes <code>/en/pt/</code>. The additional path is sent on in <code>ENV['SCRIPT_NAME'] = '/translator'</code>.</p>
<p>We also get a brief overview of Devise, the state-of-the-art authentication solution written by Jos&eacute; himself.</p>
<h2>Conclusion</h2>
<p>One last thing, the book I have reviewed is a beta book, but the quality of it is higher than most published books I have seen. Congratulations to Jos&eacute; and to the pragmatic bookshelf! There is not much more to say, get the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/12/30/review-of-crafting-rails-applications-by-jos-valim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Choices, Habits and Awareness</title>
		<link>http://blog.jayway.com/2010/10/04/choices-habits-and-awareness/</link>
		<comments>http://blog.jayway.com/2010/10/04/choices-habits-and-awareness/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 05:34:52 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mind]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6363</guid>
		<description><![CDATA[Every second, the human brain is bombarded with information, the conscious part of the brain is only able to handle a tiny percentage of this. The rest is handled by our unconscious. A habit is an automatic reaction to a given situation. Habits allow us to do things automatically, without being consciously aware of what [...]]]></description>
			<content:encoded><![CDATA[<p>Every second, the human brain is bombarded with information, the conscious part of the brain is only able to handle a tiny percentage of this. The rest is handled by our unconscious.</p>
<p>A <em>habit</em> is an automatic reaction to a given situation. Habits allow us to do things automatically, without being consciously <em>aware</em> of what we are doing. But, to form a new habit takes time, and during that time we must <em>choose</em> to do what we want to make a habit of. And, to be able to make this choice we must be aware of the choice in the first place. <em>We must be aware to be able to become unaware!</em></p>
<p>Here is an algorithm to illustrate this:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Sequential</font></i>
<b><font color="#0000FF">if</font></b> <b><font color="#0000FF">not</font></b> habit?<font color="#990000">(</font>choice<font color="#990000">)</font> <b><font color="#0000FF">or</font></b> aware_of?<font color="#990000">(</font>choice<font color="#990000">)</font>
  make_decision<font color="#990000">(</font>choice<font color="#990000">)</font>
<b><font color="#0000FF">else</font></b>
  automatic_response<font color="#990000">(</font>choice<font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<p>Of course, it is not that simple. The brain is not sequential.</p>
<p>The brain is massively parallel and almost everything in it happens unconsciously. Every second our senses are fed with more than 11 million bits of information, while the conscious part of our brain is only able to process less than 50! All other information is filtered out by our unconscious. Scary! But, here comes the cool part, <em>we can consciously change our filters by creating new habits</em>.</p>
<p>Here is parallel version of the code above.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Parallel, unconscious</font></i>
<b><font color="#0000FF">if</font></b> habit?<font color="#990000">(</font>choice<font color="#990000">)</font>
  automatic_response<font color="#990000">(</font>choice<font color="#990000">)</font>
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># Parallel, conscious</font></i>
<b><font color="#0000FF">if</font></b> aware_of<font color="#990000">(</font>choice<font color="#990000">)</font>
  make_decision<font color="#990000">(</font>choice<font color="#990000">)</font> <i><font color="#9A1900"># Practice</font></i>
<b><font color="#0000FF">end</font></b>
</tt></pre>
<h2>Choices</h2>
<p>Every single moment of our life, we are given choices. Some choices are obvious (the choices are, the answers may not be) such as:</p>
<ul>
<li>Should I use Ruby or Python for my next project?</li>
<li>Do you take this woman to be your lawfully wedded wife?</li>
<li>Should I learn a new programming language that I have no hope, whatsoever, to use in a live project?</li>
<li>Are the kids allowed to play with fire?</li>
</ul>
<p>Other choices are not so clear, such as:</p>
<ul>
<li>Do I get up now or press snooze another time?</li>
<li>Should I step on the white lines when crossing the street?</li>
<li>Do I put the right foot in front of the left or jump on the left?</li>
<li>Should I remove the hand from the hot stove or not?</li>
<li>Should I react by being angry or not?</li>
</ul>
<p>What's cool, and scary, with choices is that every time we make one, our brain changes. It literally changes physically every time. It actually changes every time we think a thought.</p>
<p>Every time we make a certain choice, that path through the brain gets wider, and thus the choice gets easier to make. It is easier to make the same decision, over and over, than it is to make new decisions all the time.</p>
<p>Another thing about choices is that it is often painful to make them. The reason for this, I believe, is that we often know, intuitively, what the right choice is, but that choice involves more work. This is why we should form good habits, because habits free us from the agony to choose.</p>
<h2>Habits</h2>
<p>When we have made a decision many times; when a path through the brain has become wide enough, we don't have to make a choice any more! We have formed a habit. Psychologists say that it takes 21 days. We have both good habits and bad ones. And most of the habits we have, we are unaware of.</p>
<p>A great thing about <em>good</em> habits is that they are performed by the unconscious part of the brain. They allow us to do things without consciously thinking about them. And since we are not consciously thinking about them, it is almost as if they do themselves. <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>A sad thing about <em>bad</em> habits is that they are performed by the unconscious part of the brain. We do things without consciously thinking about them. And since we are not consciously thinking about them, it is almost as if they do themselves. <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>That is why we should nurture our habits. We should create habits of things that we want to do, like writing our tests first, and complimenting people when they do good. We should make habits of boring things, that we have to do, like doing the dishes and writing documentation. Once they become habits they are not boring anymore.</p>
<p>This is also why we should be grateful when someone points out our bad habits, because they help us become aware of something that we may want to change.</p>
<h2>Awareness</h2>
<p>The key to forming new good habits and getting rid of bad ones, is <em>awareness</em>. We cannot change what we are not aware of. We cannot train what we are not aware of. But what are we aware of? We are aware of things we are trained to be aware of. Our awareness is learned, it is dependent on our habits! Ah, recursion!</p>
<p>When we are creating habits, we define ourselves.</p>
<p>Are you aware that there is a <a href="http://www.ruby-lang.org">programming language</a> designed for programmer happiness?</p>
<p>Are you aware that touch typing is a habit? That sitting in front of a computer eight hours a day without being able to touch type sucks? If you're not, read <a href="http://steve-yegge.blogspot.com/2008/09/programmings-dirtiest-little-secret.html">this blog post</a> by blogger extraordinaire, Steve Yegge. I wasn't aware two years ago but now I type 50 WPM (which is not great, but I get better every day).</p>
<p>Are you aware of that your habits form your thoughts, that they form you?</p>
<blockquote><p>We are what we repeatably do. --Aristotele</p>
</blockquote>
<h2>Conclusion</h2>
<p>Can you imagine a day without googling for code-related things? When you don't have to think at all about the signatures of methods, the key combinations that moves you to the right position, or the inner workings of the authentication library. Can you imagine how good you would feel, and how fast you would work? Can you imagine eight hours of continuous flow?</p>
<blockquote><p>Linus Torvalds, allegedly, wrote the bare bones of Git in two weeks. How could he do this? Because he has been living and breathing C and Linux, for the last 20 years!</p>
</blockquote>
<p>So, where am I going with this?  I want flow! I want eight hours of continuous flow while I'm working. In order to achieve this, I have to train my brain to know, really know, unconsciously know, the tools of my trade.</p>
<p>I am going to select a small number of tools that resonates with the way I am and want to work.</p>
<p>I have previously been a generalist when it comes to programming. I am proficient in many programming languages, but I have yet to become a true expert in one. With a true expert I mean someone who doesn't have to lookup <em>anything</em>. Everything, the language constructs, the built-in libraries, the major third-party libraries, the tools, should be intuitively known, habitual.</p>
<p><em>I aim to become an expert in the languages Javascript and Ruby.</em></p>
<p>This doesn't mean that I'll give up other programming languages. I will still dabble with interesting programming languages like Clojure, Scala, Mirah, etc., but my main focus will be on Ruby and Javascript.</p>
<p>Why Ruby and Javascript? Because they are languages that resonates with my brain. They are dynamic, interpreted and give lightning-fast feedback. They also allow me to express my concepts clearly and succinctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/10/04/choices-habits-and-awareness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why Ruby?</title>
		<link>http://blog.jayway.com/2010/09/29/why-ruby/</link>
		<comments>http://blog.jayway.com/2010/09/29/why-ruby/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 16:52:27 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6357</guid>
		<description><![CDATA[Emancipate yourself from mental slavery, none but ourselves can free our minds. --Bob Marley, Redemption Song There are a number of reasons to love Ruby and I will share some of them here. Ruby is influenced by some of the greatest languages ever invented: Perl, probably the most pragmatic language in the world, hell, even [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Emancipate yourself from mental slavery, none but ourselves can free our minds. --Bob Marley, Redemption Song</p>
</blockquote>
<p>There are a number of reasons to love Ruby and I will share some of them here. Ruby is influenced by some of the greatest languages ever invented: Perl, probably the most pragmatic language in the world, hell, even God used it <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="http://imgs.xkcd.com/comics/lisp.jpg" alt="Lisp from XKCD" /></p>
<p>Ruby is also influenced by Smalltalk and Lisp. Both these languages are still living, vibrant languages despite their respectable age, 40 and 60 years respectively.</p>
<p>When Matz chose parts from the above programming languages his primary design goal was <strong>Programmer Happiness</strong>. He succeeded.</p>
<p>Here are some features that I love about the language.</p>
<h2>Parallel Assignment</h2>
<p>You can assign many values on the same line by separating them by commas. They are assigned in parallel. No need for temporary variables.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Swap a and b, without a temporary variable</font></i>
a<font color="#990000">,</font> b <font color="#990000">=</font> b<font color="#990000">,</font> a
</tt></pre>
<h2>Multiple Return Values</h2>
<p>If you want to return more than one value in Ruby all you need to do is to wrap it in an array. Then you can get the values by using commas. Also note, that return is not required in Ruby, the last evaluated expression is returned.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">def</font></b> div<font color="#990000">(</font>a<font color="#990000">,</font> b<font color="#990000">)</font>
  <font color="#990000">[</font>a<font color="#990000">/</font>b<font color="#990000">,</font> a<font color="#990000">%</font>b<font color="#990000">]</font>
<b><font color="#0000FF">end</font></b>

a<font color="#990000">,</font> b <font color="#990000">=</font> div<font color="#990000">(</font><font color="#993399">5</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">)</font>
<i><font color="#9A1900"># a = 2, b = 1</font></i>
</tt></pre>
<h2>String Interpolation</h2>
<p>Double-quoted strings can be interpolated.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>kid <font color="#990000">=</font> <font color="#FF0000">'Kid'</font>
<font color="#FF0000">"Hello #{kid} ##{1+3}"</font>
<i><font color="#9A1900"># =&gt; Hello Kid #4</font></i>
</tt></pre>
<h2>Here documents</h2>
<p>You have seen them in other scripting languages and they are as good as they look. No need for String concatenation or embedded newlines (\n).</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>sql <font color="#990000">=</font> <font color="#990000">&lt;&lt;-</font>SQL
SELECT allocations<font color="#990000">.</font>project_id as project_id<font color="#990000">,</font> year<font color="#990000">,</font> week<font color="#990000">,</font> allocations<font color="#990000">.</font>id as allocation_id
  FROM weeks
  LEFT OUTER JOIN allocations ON allocations<font color="#990000">.</font>start_date <font color="#990000">&lt;</font> weeks<font color="#990000">.</font>end_date
    AND allocations<font color="#990000">.</font>end_date <font color="#990000">&gt;</font> weeks<font color="#990000">.</font>start_date
    AND allocations<font color="#990000">.</font>project_id <font color="#990000">=</font> <font color="#990000">?</font>
  WHERE weeks<font color="#990000">.</font>start_date <font color="#990000">&gt;=</font> <font color="#990000">?</font> AND weeks<font color="#990000">.</font>end_date <font color="#990000">&lt;=</font> <font color="#990000">?</font>
SQL
</tt></pre>
<h2>Higher Order Functions</h2>
<p>Higher order functions are easily expressed with Ruby blocks.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Map converts an array of values to a new array of values</font></i>
<font color="#990000">[</font><font color="#FF0000">"a"</font><font color="#990000">,</font> <font color="#FF0000">"b"</font><font color="#990000">,</font> <font color="#FF0000">"c"</font><font color="#990000">].</font>map <font color="#FF0000">{</font> <font color="#990000">|</font>item<font color="#990000">|</font> item<font color="#990000">.</font>upcase <font color="#FF0000">}</font>
<i><font color="#9A1900"># =&gt; ["A", "B", "C"]</font></i>

<i><font color="#9A1900"># Zip zips arrays together</font></i>
<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">,</font> <font color="#993399">3</font><font color="#990000">].</font>zip<font color="#990000">([</font><font color="#993399">10</font><font color="#990000">,</font> <font color="#993399">20</font><font color="#990000">,</font> <font color="#993399">30</font><font color="#990000">])</font>
<i><font color="#9A1900"># =&gt; [[1, 10], [2, 20], [3, 30]]</font></i>

<i><font color="#9A1900"># Zip with a function applies the function to the results</font></i>
<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">,</font> <font color="#993399">3</font><font color="#990000">].</font>zip<font color="#990000">([</font><font color="#993399">10</font><font color="#990000">,</font> <font color="#993399">20</font><font color="#990000">,</font> <font color="#993399">30</font><font color="#990000">])</font> <font color="#FF0000">{</font><font color="#990000">|</font>arr<font color="#990000">|</font> arr<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">]</font> <font color="#990000">+</font> arr<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">]</font><font color="#FF0000">}</font>
<i><font color="#9A1900"># =&gt; [11, 22, 33]</font></i>
</tt></pre>
<h2>Simple Command Line Access</h2>
<p>The command line is always readily available in Ruby</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Backticks invokes the command and returns the result as a string</font></i>
`ls`
<i><font color="#9A1900"># =&gt; "Scripts\nStylesheets\nbin\nget_destinations.scpt\nlib\nnibs\n"</font></i>
`ls`<font color="#990000">.</font>split
<i><font color="#9A1900"># =&gt; ["Scripts", "Stylesheets", "bin", "get_destinations.scpt", "lib", "nibs"]</font></i>

<i><font color="#9A1900"># System invokes the command, prints the result to stdout, and return a boolean</font></i>
system<font color="#990000">(</font><font color="#FF0000">'ls -l'</font><font color="#990000">)</font>
total <font color="#993399">64</font>
drwxr<font color="#990000">-</font>xr<font color="#990000">-</font>x  <font color="#993399">3</font> andersjanmyr  admin    <font color="#993399">102</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> Scripts
drwxr<font color="#990000">-</font>xr<font color="#990000">-</font>x  <font color="#993399">3</font> andersjanmyr  admin    <font color="#993399">102</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> Stylesheets
drwxr<font color="#990000">-</font>xr<font color="#990000">-</font>x  <font color="#993399">5</font> andersjanmyr  admin    <font color="#993399">170</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> bin
<font color="#990000">-</font>rw<font color="#990000">-</font>r<font color="#990000">--</font>r<font color="#990000">--</font>  <font color="#993399">1</font> andersjanmyr  admin  <font color="#993399">12312</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> get_destinations<font color="#990000">.</font>scpt
drwxr<font color="#990000">-</font>xr<font color="#990000">-</font>x  <font color="#993399">3</font> andersjanmyr  admin    <font color="#993399">102</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> lib
drwxr<font color="#990000">-</font>xr<font color="#990000">-</font>x  <font color="#993399">3</font> andersjanmyr  admin    <font color="#993399">102</font> Aug <font color="#993399">29</font>  <font color="#993399">2009</font> nibs
<i><font color="#9A1900"># =&gt; true</font></i>

system<font color="#990000">(</font><font color="#FF0000">'ls tapir'</font><font color="#990000">)</font>
ls<font color="#990000">:</font> tapir<font color="#990000">:</font> No such file <b><font color="#0000FF">or</font></b> directory
<i><font color="#9A1900"># =&gt; false</font></i>
</tt></pre>
<h2>Open Classes</h2>
<p>Open classes mean that it is possible to add new methods to already existing classes. This is very powerful.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>
<b><font color="#0000FF">class</font></b> String
  <b><font color="#0000FF">def</font></b> vowels
    <b><font color="#0000FF">self</font></b><font color="#990000">.</font>scan<font color="#990000">(</font><font color="#FF6600">/[aeiouy]/</font>i<font color="#990000">)</font>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

<font color="#FF0000">"A tapir is beautiful"</font><font color="#990000">.</font>vowels
<i><font color="#9A1900"># =&gt; ["A", "a", "i", "i", "e", "a", "u", "i", "u"]</font></i>
</tt></pre>
<p>There are two common reactions to this.</p>
<ul>
<li>Wow! This is really cool, this will allow me to put everything in the right place.</li>
<li>What! That is too dangerous, our programmers will create an unmaintainable system.</li>
</ul>
<p>I'm in the first camp!</p>
<h2>Class Inheritance</h2>
<p>In Ruby the classes are objects and follow the inheritance rules of objects. No statics are needed.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Mammal
  <i><font color="#9A1900"># This is a class method, that returns all mammals of this kind</font></i>
  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>all
    mammals<font color="#990000">.</font>select <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>m<font color="#990000">|</font>
      m<font color="#990000">.</font>kind_of? <b><font color="#0000FF">self</font></b>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>

  <i><font color="#9A1900"># Another class method that gives access to the class varibable</font></i>
  <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>mammals
    <i><font color="#9A1900"># @@ is a class variable prefix</font></i>
    <font color="#009900">@@all_mammals</font> <font color="#990000">||=</font> <font color="#990000">[]</font>
  <b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">class</font></b> Tapir <font color="#990000">&lt;</font> Mammal
<b><font color="#0000FF">end</font></b>

<i><font color="#9A1900"># Add some mammals and a tapir</font></i>
Mammal<font color="#990000">.</font>mammals <font color="#990000">&lt;&lt;</font> Mammal<font color="#990000">.</font>new <font color="#990000">&lt;&lt;</font> Mammal<font color="#990000">.</font>new <font color="#990000">&lt;&lt;</font> Tapir<font color="#990000">.</font>new

Tapir<font color="#990000">.</font>all
<i><font color="#9A1900"># =&gt; [#&lt;Tapir:0x000001010fc1a0&gt;]</font></i>

Mammal<font color="#990000">.</font>all
<i><font color="#9A1900"># =&gt; [#&lt;Mammal:0x000001010fc218&gt;, #&lt;Mammal:0x000001010fc1c8&gt;, #&lt;Tapir:0x000001010fc1a0&gt;]</font></i>

</tt></pre>
<h2>Meta Programming</h2>
<p>When the classes are parsed, Ruby is already running and since it is possible to create methods and classes on the fly you can do some very powerful meta-programming with Ruby. Here is a simple example.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">class</font></b> Tapir

  <font color="#990000">[:</font>sniff<font color="#990000">,</font> <font color="#990000">:</font>eat<font color="#990000">].</font>each <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>method_name<font color="#990000">|</font>
    send <font color="#990000">:</font>define_method<font color="#990000">,</font> method_name <b><font color="#0000FF">do</font></b> <font color="#990000">|</font>thing<font color="#990000">|</font>
      <font color="#FF0000">"I'm #{method_name}ing #{thing}!"</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>

<b><font color="#0000FF">end</font></b>

t <font color="#990000">=</font> Tapir<font color="#990000">.</font>new
t<font color="#990000">.</font>eat <font color="#FF0000">'bananas'</font>
I'm eating bananas!

t<font color="#990000">.</font>sniff <font color="#FF0000">'glue'</font>
I'm sniffing glue!

</tt></pre>
<p>This type of meta-programming is what enables the simplicity of Rails' <code>has_many</code>, and <code>belongs_to</code> methods for declaring relationships.</p>
<h2>Method Missing</h2>
<p>The last feature of Ruby that I want to share is <code>method_missing</code>. <code>method_missing</code> is a feature that Ruby inherited from Smalltalk. If an object is invoked with a method that is not defined by its class or ancestors, <code>method_missing(method, args)</code> is called instead. By implementing this method you can effectively create all sorts of cool stuff such as proxies, prototypes, etc.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Simple implementation of a Javascript-like structure without inheritance</font></i>
<b><font color="#0000FF">class</font></b> Prototype

  <b><font color="#0000FF">def</font></b> props
    <font color="#009900">@props</font> <font color="#990000">||=</font> <font color="#FF0000">{}</font>
  <b><font color="#0000FF">end</font></b>

  <b><font color="#0000FF">def</font></b> method_missing<font color="#990000">(</font>method<font color="#990000">,</font> <font color="#990000">*</font>args<font color="#990000">)</font>
    m <font color="#990000">=</font> method<font color="#990000">.</font>to_s<font color="#990000">.</font>sub<font color="#990000">(</font><font color="#FF0000">'='</font><font color="#990000">,</font> <font color="#FF0000">''</font><font color="#990000">)</font> <i><font color="#9A1900"># Strip the trailing '=' to allow setters</font></i>
    <b><font color="#0000FF">if</font></b> args<font color="#990000">.</font>empty?
      props<font color="#990000">[</font>m<font color="#990000">]</font>
    <b><font color="#0000FF">else</font></b>
      props<font color="#990000">[</font>m<font color="#990000">]</font> <font color="#990000">=</font> args<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">]</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b>

prot <font color="#990000">=</font> Prototype<font color="#990000">.</font>new
prot<font color="#990000">.</font>age
<i><font color="#9A1900"># =&gt; nil</font></i>
prot<font color="#990000">.</font>age <font color="#993399">14</font>
<i><font color="#9A1900"># =&gt; 14</font></i>
prot<font color="#990000">.</font>age
<i><font color="#9A1900"># =&gt; 14</font></i>
prot<font color="#990000">.</font>age<font color="#990000">=</font> <font color="#993399">16</font>
<i><font color="#9A1900"># =&gt; 16</font></i>
prot<font color="#990000">.</font>age
<i><font color="#9A1900"># =&gt; 16</font></i>

</tt></pre>
<p>Inside <code>method_missing</code> it is of course also possible to define new methods and classes dynamically, but I'll leave that for another day.</p>
<p>Ruby is a supreme dynamic language that lets you do almost anything. Try it!</p>
<blockquote><p>When you can do anything, you have to become more responsible. You own your fate. And, that, I think, is a situation which promotes responsibility. -- Michael Feathers, author of <a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052?tag=thtasta-20">Working Effectively with Legacy Code</a></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/09/29/why-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Webkit CSS Transitions and Transformations</title>
		<link>http://blog.jayway.com/2010/09/18/webkit-css-transitions-and-transformations/</link>
		<comments>http://blog.jayway.com/2010/09/18/webkit-css-transitions-and-transformations/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 06:12:31 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6304</guid>
		<description><![CDATA[This blog is best viewed in Safari or Chrome since it uses Webkit-specific functionality. Transitions transition, the process or a period of changing from one state or condition to another Transitions are usually performed using jQuery or some other Javascript framework to animate the changes to a property. When using Webkit (Safari, Chrome, iPad, iPhone) [...]]]></description>
			<content:encoded><![CDATA[<p>This blog is best viewed in Safari or Chrome since it uses Webkit-specific functionality.</p>
<h2>Transitions</h2>
<blockquote><p><strong>transition</strong>, the process or a period of changing from one state or condition to another</p>
</blockquote>
<p>Transitions are usually performed using jQuery or some other Javascript framework to animate the changes to a property. When using Webkit (Safari, Chrome, iPad, iPhone) Javascript is not necessary anymore.</p>
<p>In Webkit the transitions are described using four different properties. It is worth noting that the properties only define how the transitions will work when the values on the element is changed. <em>They don't actually do anything until the values are changed!</em></p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">/* This sets up a transition property on all divs on opacity.*/</font></i>
div <font color="#FF0000">{</font>
  <font color="#0000FF">-webkit-transition-property:</font> <i><font color="#009900">opacity</font></i>;
  <font color="#0000FF">-webkit-transition-duration:</font> <i><font color="#009900">2s</font></i>;
<font color="#FF0000">}</font>
<i><font color="#9A1900">/* The fade-in class, changes the opacity when set on an element.</font></i>
<i><font color="#9A1900">  And thus triggers the transition defined above if the element is a div. */</font></i>
<font color="#993399">.fade</font><font color="#990000">-</font>in <font color="#FF0000">{</font>
  <font color="#0000FF">opacity:</font> <i><font color="#009900">1</font></i>;
<font color="#FF0000">}</font>
</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// Adds the class 'fade-in' on all divs, via jQuery.</font></i>
$<font color="#990000">(</font><font color="#FF0000">"div"</font><font color="#990000">).</font><b><font color="#000000">each</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
  $<font color="#990000">(</font><b><font color="#0000FF">this</font></b><font color="#990000">).</font><b><font color="#000000">addClass</font></b><font color="#990000">(</font><font color="#FF0000">'fade-in'</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font>
</tt></pre>
<h3><code>-webkit-transition-property</code></h3>
<p>This is the property that will be affected by the transition, this can be any valid property, and it defaults to <strong>all</strong>. What this means is that a change to any attribute on an element will be affected by the transition and occur according to the specification.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">/* only width should be affected */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> width<font color="#990000">;</font>

<i><font color="#9A1900">/* multiple comma-separated properties are supported */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> opacity<font color="#990000">,</font> width<font color="#990000">,</font> height<font color="#990000">;</font>

<i><font color="#9A1900">/* all properties should be affected, this is the default */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> all<font color="#990000">;</font>
</tt></pre>
<h3><code>-webkit-transition-duration</code></h3>
<p>How long the transition should take? Valid units are <code>s</code> and <code>ms</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">/* Set the duration to 400 milliseconds */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>duration<font color="#990000">:</font> 400ms<font color="#990000">;</font>

<i><font color="#9A1900">/* Multiple values are supported here too, in combination with</font></i>
<i><font color="#9A1900">multiple -webkit-transition-property's */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> opacity<font color="#990000">,</font> width<font color="#990000">,</font> height<font color="#990000">;</font>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>duration<font color="#990000">:</font> 400ms 1s 3s<font color="#990000">;</font>
</tt></pre>
<h3><code>-webkit-transition-delay</code></h3>
<p>How long should we wait before the transition starts. Valid units are <code>s</code> and <code>ms</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">/* Set the delay to 1 second */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>delay<font color="#990000">:</font> 1s<font color="#990000">;</font>

<i><font color="#9A1900">/* Multiple values are supported here too, in combination with</font></i>
<i><font color="#9A1900">multiple -webkit-transition-property's */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> width<font color="#990000">,</font> height<font color="#990000">;</font>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>delay<font color="#990000">:</font> 1s 3s<font color="#990000">;</font>
</tt></pre>
<h3><code>-webkit-transition-timing-function</code></h3>
<p>The timing function is a function that changes the speed of the transition over its duration. It is described with a <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">cubic-bezier</a> curve, but it has some predefined values, <code>ease</code> (default), <code>ease-in</code>, <code>ease-out</code>, <code>ease-in-out</code>, and <code>linear</code>. In none of those values are to your liking you can define your own with the <code>cubic-bezier</code> function.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">/* Transition with the default timing*/</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>timing<font color="#990000">-</font>function<font color="#990000">:</font> ease<font color="#990000">;</font>
<i><font color="#9A1900">/* Multiple values are supported here too, in combination with</font></i>
<i><font color="#9A1900">multiple -webkit-transition-property's */</font></i>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>property<font color="#990000">:</font> opacity<font color="#990000">,</font> <font color="#990000">-</font>webkit<font color="#990000">-</font>transform<font color="#990000">;</font>
<font color="#990000">-</font>webkit<font color="#990000">-</font>transition<font color="#990000">-</font>timing<font color="#990000">-</font>function<font color="#990000">:</font> ease<font color="#990000">-</font>out<font color="#990000">,</font> cubic<font color="#990000">-</font>bezier<font color="#990000">(</font>0<font color="#993399">.5</font><font color="#990000">,</font> 0<font color="#993399">.2</font><font color="#990000">,</font> 0<font color="#993399">.3</font><font color="#990000">,</font> 1<font color="#993399">.0</font><font color="#990000">);</font>
</tt></pre>
<p>Thats it for transitions. But wait, what was that <code>-webkit-transform</code> in the end?</p>
<h2>Transformations</h2>
<blockquote><p><strong>transformation</strong>, a thorough or dramatic change in form or appearance</p>
</blockquote>
<p>Webkit supports some really cool transformations, that allow you to animate the pages in remarkable ways.</p>
<h3><code>rotate()</code></h3>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">&lt;div</font></b> <font color="#009900">style</font> <font color="#990000">=</font><font color="#FF0000"> "width: 12em;</font>
<font color="#FF0000">  margin-top: 5em;</font>
<font color="#FF0000">  -webkit-transform: rotate(45deg)"</font><b><font color="#0000FF">&gt;</font></b>
I am rotated!
<b><font color="#0000FF">&lt;/div&gt;</font></b>
</tt></pre>
<div style = "width: 12em;  margin-top: 5em;  -webkit-transform: rotate(45deg)">
I am rotated!
</div>
<p>Rotate accepts the units <code>deg</code>, <code>rad</code>, and <code>grad</code></p>
<p>It also comes in 3D forms, <code>rotateX</code>, <code>rotateY</code>, <code>rotateZ</code>, and <code>rotate3d</code>.</p>
<h3><code>scale()</code></h3>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">&lt;div</font></b> <font color="#009900">style</font> <font color="#990000">=</font><font color="#FF0000"> "width: 12em;</font>
<font color="#FF0000">  margin: 2em 0 2em 18em;</font>
<font color="#FF0000">  -webkit-transform: scale(4)"</font><b><font color="#0000FF">&gt;</font></b>
I am Scaled!
<b><font color="#0000FF">&lt;/div&gt;</font></b>
</tt></pre>
<div style = "width: 12em;  margin: 2em 0 2em 18em;  -webkit-transform: scale(4)">
I am Scaled!
</div>
<p>Scale can take an additional parameter <code>scale(x, y)</code> If you want to scale  un-proportionally. It also comes in 3D versions, <code>scaleX</code>, <code>scaleY</code>, <code>scaleZ</code>, and <code>scale3d</code>.</p>
<h3><code>translate()</code></h3>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">&lt;div</font></b> <font color="#009900">style</font> <font color="#990000">=</font><font color="#FF0000"> "width: 12em;</font>
<font color="#FF0000">  margin: 2em;</font>
<font color="#FF0000">  -webkit-transform: translate(4em, -2em)"</font><b><font color="#0000FF">&gt;</font></b>
I am Translated!
<b><font color="#0000FF">&lt;/div&gt;</font></b>
</tt></pre>
<div style = "width: 12em;  margin: 2em;  -webkit-transform:translate(4em, -2em)">
I am Translated!
</div>
<p>Translate takes one or two parameters and moves the element to the new location. It, too, comes in 3D forms, <code>translateX</code>, <code>translateY</code>, <code>translateZ</code>, and <code>translate3d</code>.</p>
<h3>Additional Transforms</h3>
<p>Apart from the translations above, Webkit also supports <code>skew</code>, <code>perspective</code>, and <code>matrix</code>. Read more in <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariCSSRef/Articles/Functions.html#//apple_ref/doc/uid/TP40007955-SW22">the Safari Reference Library</a></p>
<p>I'll finish up with a composite transform combined with transitions, best viewed with Safari. This transformations are initiated by changing the style from Javascript.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">&lt;div</font></b> <font color="#009900">style</font><font color="#990000">=</font><font color="#FF0000">"width: 4em; height: 4em;</font>
<font color="#FF0000">  margin: 2em; background-color: blue;</font>
<font color="#FF0000">  color: white;</font>
<font color="#FF0000">  -webkit-transition-duration: 5s;</font>
<font color="#FF0000">  -webkit-transition-function: ease-out;"</font>
  <font color="#009900">onclick</font><font color="#990000">=</font><font color="#FF0000">'style.webkitTransform="translate(16em, -16em) scale(6) rotate(720deg)"; style.backgroundColor="red";'</font><b><font color="#0000FF">&gt;</font></b> Click Me!
<b><font color="#0000FF">&lt;/div&gt;</font></b>
</tt></pre>
<div style="width: 4em; height: 4em;margin: 2em; background-color: blue;  color: white;  -webkit-transition-duration: 5s;  -webkit-transition-function: ease-out;"  onclick='style.webkitTransform="translate(16em, -16em) scale(6) rotate(720deg)"; style.backgroundColor="red";'>
Click Me!
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/09/18/webkit-css-transitions-and-transformations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Git with Subversion</title>
		<link>http://blog.jayway.com/2010/09/02/using-git-with-subversion/</link>
		<comments>http://blog.jayway.com/2010/09/02/using-git-with-subversion/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 07:45:26 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6260</guid>
		<description><![CDATA[I had the unfortunate experience of having to use Subversion again after using Git for a long time. It is amazing how fast I can forget. After renaming a directory at the prompt, and the agony that goes with it, I decided to switch back to Git. $ mv requester sampler # svn agony after [...]]]></description>
			<content:encoded><![CDATA[<p>I had the unfortunate experience of having to use Subversion again after using Git for a long time. It is amazing how fast I can forget. After renaming a directory at the prompt, and the agony that goes with it, I decided to switch back to Git.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>$ mv requester sampler
<i><font color="#9A1900"># svn agony after renaming a directory</font></i>
$ svn st
<font color="#990000">?</font>       sampler
<font color="#990000">!</font>       requester
<font color="#990000">!</font>       requester<font color="#990000">.</font>rb
A       sampler<font color="#990000">.</font>rb

</tt></pre>
<p>The tool to use when using Git with Subversion is, of course, <code>git svn</code>.</p>
<p><code>git svn</code> works very well as long as you remember that <em>Subversion is not Git</em>. It does not handle merging well, and it will bite you if you don't respect that. So what does this actually mean? It means:</p>
<h2>Always keep the Git master in sync with Subversion</h2>
<p>To do this you have two commands you can use.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Rebases the commits from the upstream Subversion server with your local master.</font></i>
$ git svn rebase

</tt></pre>
<p>You should only <code>git svn rebase</code> in your Git master, and you should <strong>ALWAYS</strong> do it before you <code>git svn dcommit</code> anything to the subversion repository. <code>git svn rebase</code> keeps the upstream subversion in sync with your local master by pulling down the changes, kind of like <code>svn update</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Commits the changes you have in your local master to the upstream Subversion server.</font></i>
$ git svn dcommit

</tt></pre>
<p>When you have changes ready to commit, you commit them to subversion with <code>git svn dcommit</code>. You should <strong>ALWAYS</strong> <code>git svn rebase</code> before you do the update, or it will fail.</p>
<p>That's it! As long as you follow these two simple rules, your life with <code>git svn</code> will be easy. If you forget to follow them, you will be bitten. When you get bitten, the cool thing about Git is that even if you screw up, it is always possible to sort it out.</p>
<p>It that was all there was to it, there would be no reason to use Git instead of Subversion. Git really shines when it comes to branching and merging. You may create as many local branches as you like with <code>git branch branch_name</code> or <code>git checkout -b branch_name</code>. You can hack around in these local branches as much as you want and merge them together. But, before you merge them into the master branch, <strong>you must rebase with master! Not merge, rebase!</strong> Rebase means replay the commits on top of the named branch. It creates new commits, the same content, but with a different SHA-1.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Example session</font></i>

<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn rebase
<font color="#990000">(</font>master<font color="#990000">)</font>$ git checkout -b dev
hack<font color="#990000">,</font> hack<font color="#990000">,</font> hack<font color="#990000">,</font> <font color="#990000">...</font>
<font color="#990000">(</font>dev<font color="#990000">)</font>$ git commit -am <font color="#FF0000">'Commit the changes'</font> 

<font color="#990000">(</font>dev<font color="#990000">)</font>$ git checkout master
<font color="#990000">(</font>master<font color="#990000">)</font>$ git checkout -b bugfix
hack<font color="#990000">,</font> hack<font color="#990000">,</font> hack<font color="#990000">,</font> <font color="#990000">...,</font> <b><font color="#0000FF">done</font></b>

<font color="#990000">(</font>bugfix<font color="#990000">)</font>$ git checkout master
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn rebase
<font color="#990000">(</font>master<font color="#990000">)</font>$ git checkout bugfix
$ git rebase master
<font color="#990000">(</font>bugfix<font color="#990000">)</font>$ git checkout master
<font color="#990000">(</font>master<font color="#990000">)</font>$ git merge --ff bugfix <i><font color="#9A1900"># --ff only fast-forwards, merges that don't need to merge. </font></i>
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn dcommit
<font color="#990000">(</font>master<font color="#990000">)</font>$ git branch -D bugfix <i><font color="#9A1900"># delete the branch it is not needed anymore</font></i>

<font color="#990000">(</font>master<font color="#990000">)</font>$ git checkout dev
hack<font color="#990000">,</font> hack<font color="#990000">,</font> hack<font color="#990000">,</font> <font color="#990000">...,</font> <b><font color="#0000FF">done</font></b>

<font color="#990000">(</font>dev<font color="#990000">)</font>$ git checkout master
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn rebase
<font color="#990000">(</font>master<font color="#990000">)</font>$ git checkout dev
<font color="#990000">(</font>dev<font color="#990000">)</font>$ git rebase master
<font color="#990000">(</font>dev<font color="#990000">)</font>$ git checkout master
<font color="#990000">(</font>master<font color="#990000">)</font>$ git merge --ff dev <i><font color="#9A1900"># --ff only fast-forwards, merges that don't need to merge.</font></i>
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn dcommit

</tt></pre>
<p>Another thing to be aware of is that <code>git svn dcommit</code> creates an extra commit, so even if you haven't changed anything in the master you need to rebase the local branch with the master. This is only needed if you don't delete the branches after you are done with a commit.</p>
<p>In the example above, I ended with a <code>git svn dcommit</code> and I didn't remove the <code>dev</code> branch.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><font color="#990000">(</font>master<font color="#990000">)</font>$ git svn dcommit <i><font color="#9A1900"># from above</font></i>
<font color="#990000">(</font>master<font color="#990000">)</font>$ git co dev
<font color="#990000">(</font>dev<font color="#990000">)</font>$ git rebase master <i><font color="#9A1900"># rebases the extra commit created by git svn dcommit</font></i>

</tt></pre>
<p>If you forget to rebase or something else happens that hinders a clean merge into the master. You can always back out of it with <code>git reset --hard</code>.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><font color="#990000">(</font>master<font color="#990000">)</font>$ git svn dcommit
<font color="#990000">...</font> failed miserably<font color="#990000">,</font> because I failed to git svn rebase<font color="#990000">,</font> bollocks<font color="#990000">!</font>
<font color="#990000">(</font>aa<font color="#990000">..</font>88dd<font color="#990000">|</font>MERGING<font color="#990000">)</font>$ git reset --hard
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn rebase
<font color="#990000">(</font>master<font color="#990000">)</font>$ git svn dcommit <i><font color="#9A1900"># Nice and clean commit</font></i>

</tt></pre>
<p>To get started you need to clone a subversion repository.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>$ git svn clone http<font color="#990000">:</font>//svn<font color="#990000">.</font>example<font color="#990000">.</font>com/project/trunk
$ cd trunk
<font color="#990000">(</font>master<font color="#990000">)</font>$ git <font color="#990000">...</font>

</tt></pre>
<p>Now, is a good time to start using Git. Get yourself anything by Scott Chacon, such as the <a href="http://www.amazon.com/Pro-Git-Scott-Chacon/dp/1430218339?tag=thtasta-20">book</a> or the <a href="http://www.gitcasts.com/">screencasts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/09/02/using-git-with-subversion/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Ruby Toolbox</title>
		<link>http://blog.jayway.com/2010/08/26/the-ruby-toolbox/</link>
		<comments>http://blog.jayway.com/2010/08/26/the-ruby-toolbox/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 13:12:55 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6166</guid>
		<description><![CDATA[One of the great things about Ruby is the community. When something is not working out as well as it could, someone figures out a way to improve it. It used to be difficult to select what gems, Ruby libraries, to use when solving a task that I was not familiar with. This is not [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about Ruby is the community. When something is not working out as well as it could, someone figures out a way to improve it.</p>
<p>It used to be difficult to select what gems, Ruby libraries, to use when solving a task that I was  not familiar with. This is not the case anymore.<br />
<a href="http://colszowka.heroku.com">Christoph Olszowka</a> decided that he wanted to solve this problem and created <a href="http://ruby-toolbox.com/">The Ruby Toolbox</a>.</p>
<blockquote><p><a href="http://ruby-toolbox.com/">The Ruby Toolbox</a>, <strong>Know your options!</strong></p>
<p>Ruby developers can choose from a variety of tools to get their job done.</p>
<p>The Ruby Toolbox gives you an overview of these tools, sorted in categories and rated by the amount of watchers and forks in the corresponding source code repository on GitHub so you can find out easily what options you have and which are the most common ones in the Ruby community.</p>
</blockquote>
<p>This is surprisingly powerful. In my latest project I used the following libraries (among others).</p>
<h2><a href="http://ruby-toolbox.com/categories/web_app_frameworks.html#rails_rails">Rails</a></h2>
<p>Rails need no introduction, it is one of the best frameworks for creating dynamic web applications. With best, I mean most productive and fun to work with!</p>
<h2><a href="http://ruby-toolbox.com/categories/template_languages.html#nex3_haml">Haml</a></h2>
<p>Haml (HTML Abstraction Markup Language) is a layer on top of XHTML or XML that's designed to express the structure of XHTML or XML documents in a non-repetitive, elegant, easy way!</p>
<h2><a href="http://www.ruby-toolbox.com/categories/http_clients.html#pauldix_typhoeus">Typhoeus</a></h2>
<p>Typhoeus is a http client library, a thick wrapper around curl, just like curl it works like a charm.</p>
<h2><a href="http://www.ruby-toolbox.com/categories/deployment_automation.html#jamis_capistrano">Capistrano</a></h2>
<p>Ahh, Capistrano, deployment scripting done right! The simplicity of the structure is worth showing since it should be copied by other lesser tools.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900"># Create the basic structure</font></i>
$ cap deploy<font color="#990000">:</font>setup
mkdir -p /var/rails/rsvc
/var/rails/rsvc/releases
/var/rails/rsvc/shared
<font color="#990000">...</font>

<i><font color="#9A1900"># Deploy from the repo into a timestamped release directory and symlink current</font></i>
$ cap deploy
svn checkout -q  -r<font color="#993399">5454</font> https<font color="#990000">:</font>//svn<font color="#990000">.</font>neo4j<font color="#990000">.</font>org/qa/matrix-framework/rsvc/trunk/rsvc /var/rails/rsvc/releases<font color="#990000">/</font><font color="#993399">20100826122450</font>
echo <font color="#993399">5454</font> <font color="#990000">&gt;</font> /var/rails/rsvc/releases<font color="#990000">/</font><font color="#993399">20100826122450</font>/REVISION
<font color="#990000">....</font>
rm -f /var/rails/rsvc/current <font color="#990000">&amp;&amp;</font> ln -s /var/rails/rsvc/releases<font color="#990000">/</font><font color="#993399">20100826122450</font> /var/rails/rsvc/current

</tt></pre>
<p>It's so simple and powerful, that it makes me cry.</p>
<h2><a href="http://www.ruby-toolbox.com/categories/server_monitoring.html#mojombo_god">God</a></h2>
<p>God, a wonderfully named, server monitoring tool written in Ruby. God aims to be the simplest, most powerful monitoring application available.</p>
<h2><a href="http://www.ruby-toolbox.com/categories/testing_frameworks.html#dchelimsky_rspec">RSpec</a></h2>
<p>RSpec, only code can do it justice.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>describe Request <b><font color="#0000FF">do</font></b>

  before <b><font color="#0000FF">do</font></b>
    <font color="#009900">@sample</font> <font color="#990000">=</font> Sample<font color="#990000">.</font>create<font color="#990000">(:</font>name <font color="#990000">=&gt;</font> <font color="#FF0000">'my_sample'</font><font color="#990000">,</font> <font color="#990000">:</font>method <font color="#990000">=&gt;</font> <font color="#FF0000">"post"</font><font color="#990000">)</font>
  <b><font color="#0000FF">end</font></b>

  context <font color="#FF0000">'#old'</font> <b><font color="#0000FF">do</font></b>
    it <font color="#FF0000">"should not return any"</font> <b><font color="#0000FF">do</font></b>
      Request<font color="#990000">.</font>old<font color="#990000">.</font>should be_empty
    <b><font color="#0000FF">end</font></b>
    <font color="#990000">...</font>
  <b><font color="#0000FF">end</font></b>

  context <font color="#FF0000">'#delete_old_non_failing'</font> <b><font color="#0000FF">do</font></b>

    it <font color="#FF0000">"should delete old non failing requests"</font> <b><font color="#0000FF">do</font></b>
      create_old_request <font color="#990000">:</font>response_status_code <font color="#990000">=&gt;</font> <font color="#993399">0</font>
      create_old_request <font color="#990000">:</font>response_status_code <font color="#990000">=&gt;</font> <font color="#993399">200</font>
      expect <font color="#FF0000">{</font>
        Request<font color="#990000">.</font>delete_old_non_failed
      <font color="#FF0000">}</font><font color="#990000">.</font>to change <font color="#FF0000">{</font> Request<font color="#990000">.</font>count <font color="#FF0000">}</font><font color="#990000">.</font>by<font color="#990000">(-</font><font color="#993399">2</font><font color="#990000">)</font>
    <b><font color="#0000FF">end</font></b>
    <font color="#990000">...</font>
  <b><font color="#0000FF">end</font></b>

  <font color="#990000">...</font>
<b><font color="#0000FF">end</font></b>

</tt></pre>
<p>Does your language have a Toolbox? Perhaps it is time to create one, or to switch to another language?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/08/26/the-ruby-toolbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good Practices for Rich Web Applications</title>
		<link>http://blog.jayway.com/2010/08/16/good-practices-for-rich-web-applications/</link>
		<comments>http://blog.jayway.com/2010/08/16/good-practices-for-rich-web-applications/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 05:54:53 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6046</guid>
		<description><![CDATA[Use jQuery jQuery is the best thing that has happened to Javascript since it got first class functions in version 1.2. The library is elegant, powerful and has exactly the right level of abstraction for working with the DOM. There is nothing more to say. Learn it and use it. Good resources are: the jQuery [...]]]></description>
			<content:encoded><![CDATA[<h2>Use jQuery</h2>
<p>jQuery is the best thing that has happened to Javascript since it got first class functions in version 1.2. The library is elegant, powerful and has exactly the right level of abstraction for working with the DOM. There is nothing more to say. Learn it and use it. Good resources are: <a href="http://api.jquery.com/l">the jQuery API</a>, <a href="http://dl.dropbox.com/u/7283184/jquery-api-1.4.2.html">my view of the jQuery API</a></p>
<h2>Learn Javascript</h2>
<p>Javascript is the <a href="http://oredev.org/2010/sessions/javascript-the-language-of-the-web">programming language of the web</a>. Learn it! Javascript is different from most other programming languages. It is dynamic, it has prototypical inheritance, and works more like Scheme than any of the languages that you are probably used to. If you want to learn Javascript you should get the following books, <a href="http://www.amazon.com/gp/product/0262560992?tag=thtasta-20">The Little Schemer</a>, <a href="http://www.amazon.com/gp/product/026256100X?tag=thtasta-20">The Seasoned Schemer</a>, <a href="http://www.amazon.com/gp/product/0596517742?tag=thtasta-20">Javascript, the Good Parts</a>, and possibly <a href="http://www.amazon.com/Performance-JavaScript-Faster-Application-Interfaces/dp/059680279X?tag=thtasta-20">High Performance Javascript</a></p>
<h2>Learn CSS</h2>
<p>Many programmers think that CSS is the language of designers and not programmers. This is not the case at all. If you are lucky enough to have a designer on your team (most people don't), CSS is the language with which you communicate. It is the interface between designers and programmers and as a programmer you should know it better than the designers. By knowing CSS well you will reduce the misunderstandings between you and your designer.</p>
<p>Unfortunately, many designers don't care about how code looks, as long as the design looks good on the surface. It will be up to you to make sure that the CSS doesn't get out out of hand. It will also be up to you to keep the HTML clean, and a good way to do this is to use semantic HTML, combined with CSS. You have no idea what the designers can come up with.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- </font></i>
<i><font color="#9A1900">  Old School rounded corners, invented by a GOOD designer. </font></i>
<i><font color="#9A1900">  All this code was actually needed to achieve the purpose.</font></i>
<i><font color="#9A1900">  --&gt;</font></i>
<b><font color="#0000FF">&lt;style&gt;</font></b>
<font color="#993399">.t</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">dot.gif</font></i>) <i><font color="#009900">0</font></i> <i><font color="#009900">0</font></i> <i><font color="#009900">repeat-x</font></i>; <font color="#0000FF">width:</font> <i><font color="#009900">20em</font></i><font color="#FF0000">}</font>
<font color="#993399">.b</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">dot.gif</font></i>) <i><font color="#009900">0</font></i> <i><font color="#009900">100%</font></i> <i><font color="#009900">repeat-x</font></i><font color="#FF0000">}</font>
<font color="#993399">.l</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">dot.gif</font></i>) <i><font color="#009900">0</font></i> <i><font color="#009900">0</font></i> <i><font color="#009900">repeat-y</font></i><font color="#FF0000">}</font>
<font color="#993399">.r</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">dot.gif</font></i>) <i><font color="#009900">100%</font></i> <i><font color="#009900">0</font></i> <i><font color="#009900">repeat-y</font></i><font color="#FF0000">}</font>
<font color="#993399">.bl</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">bl.gif</font></i>) <i><font color="#009900">0</font></i> <i><font color="#009900">100%</font></i> <i><font color="#009900">no-repeat</font></i><font color="#FF0000">}</font>
<font color="#993399">.br</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">br.gif</font></i>) <i><font color="#009900">100%</font></i> <i><font color="#009900">100%</font></i> <i><font color="#009900">no-repeat</font></i><font color="#FF0000">}</font>
<font color="#993399">.tl</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">tl.gif</font></i>) <i><font color="#009900">0</font></i> <i><font color="#009900">0</font></i> <i><font color="#009900">no-repeat</font></i><font color="#FF0000">}</font>
<font color="#993399">.tr</font> <font color="#FF0000">{</font><font color="#0000FF">background:</font> <i><font color="#009900">url</font></i>(<i><font color="#009900">tr.gif</font></i>) <i><font color="#009900">100%</font></i> <i><font color="#009900">0</font></i> <i><font color="#009900">no-repeat</font></i>; <font color="#0000FF">padding:</font><i><font color="#009900">10px</font></i><font color="#FF0000">}</font>
<b><font color="#0000FF">&lt;/style&gt;</font></b>
<b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"t"</font><b><font color="#0000FF">&gt;</font></b>
  <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"b"</font><b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"l"</font><b><font color="#0000FF">&gt;</font></b>
      <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"r"</font><b><font color="#0000FF">&gt;</font></b>
        <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"bl"</font><b><font color="#0000FF">&gt;</font></b>
          <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"br"</font><b><font color="#0000FF">&gt;</font></b>
            <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"tl"</font><b><font color="#0000FF">&gt;</font></b>
              <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"tr"</font><b><font color="#0000FF">&gt;</font></b>
                Lorem ipsum dolor sit amet consectetur adipisicing elit
              <b><font color="#0000FF">&lt;/div&gt;</font></b>
            <b><font color="#0000FF">&lt;/div&gt;</font></b>
          <b><font color="#0000FF">&lt;/div&gt;</font></b>
        <b><font color="#0000FF">&lt;/div&gt;</font></b>
      <b><font color="#0000FF">&lt;/div&gt;</font></b>
    <b><font color="#0000FF">&lt;/div&gt;</font></b>
  <b><font color="#0000FF">&lt;/div&gt;</font></b>
<b><font color="#0000FF">&lt;/div&gt;</font></b>

</tt></pre>
<p>As an additional benefit you will become better at jQuery. Not only is CSS the query language of the browsers it is the query language of jQuery. Jariba!</p>
<p><a href="http://www.amazon.com/Bulletproof-Web-Design-flexibility-protecting/dp/0321509021?tag=thtasta-20">Bulletproof Web Design</a> is a good book web design, including CSS.</p>
<h2>Decide how "Rich" your application should be</h2>
<p>How rich should your application be? The scale varies from no Javascript to only Javascript, but you will probably want to land somewhere in between. Here are a few suggestions.</p>
<ul>
<li>No Javascript, everything is server generated.</li>
<li>Slightly enhanced pages, simple validations, but no Ajax.</li>
<li>Ajax enhanced pages, but every page still reloads frequently.</li>
<li>Single page per area, entire area is handled by Javascript.</li>
<li>Only Javascript, Ajax interaction with the server</li>
<li>Only Javascript, no interaction with the server.</li>
</ul>
<p>The important thing is to <em>make a decision</em>. If you don't make the decision, everyone will do different things on different parts of the application and you will loose consistency. In GUI, consistency is king. Make a decision and move on, you can always change your decision later.</p>
<h2>Organize your code</h2>
<h3>Javascript</h3>
<p>Make sure that all your Javascript code is namespaced properly. It is impolite to pollute the global namespace and it will bite you in the end. A simple variable declaration will do.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// Common namespace for your entire application</font></i>
<i><font color="#9A1900">// This declaration lets you split your code of multiple files.</font></i>
<i><font color="#9A1900">// If MyNamespace is defined use it, otherwise declare it.</font></i>
MyNamespace <font color="#990000">=</font> window.MyNamespace <font color="#990000">||</font> <font color="#FF0000">{}</font><font color="#990000">;</font>

</tt></pre>
<p>But, of course, it is also possible to get fancy and encapsulate the functions that you don't want to expose, if that is your cup of tea.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>MyNamespace <font color="#990000">=</font> window.MyNamespace <font color="#990000">||</font> <font color="#FF0000">{}</font><font color="#990000">;</font>

MyNamespace<font color="#990000">.</font>Tournament <font color="#990000">=</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
	<i><font color="#9A1900">// Private stuff</font></i>
	<b><font color="#0000FF">var</font></b> tournamentCount <font color="#990000">=</font> <font color="#993399">0</font><font color="#990000">;</font>
	<b><font color="#0000FF">function</font></b> <b><font color="#000000">addTournament</font></b><font color="#990000">(</font>tournament<font color="#990000">)</font> <font color="#FF0000">{</font>
		tournamentCount<font color="#990000">++;</font>
	<font color="#FF0000">}</font>

	<b><font color="#0000FF">return</font></b> <font color="#FF0000">{</font>
		<i><font color="#9A1900">//public stuff</font></i>
		numberOfTournaments<font color="#990000">:</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
			<b><font color="#0000FF">return</font></b> tournamentCount<font color="#990000">;</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font><font color="#990000">();</font>

</tt></pre>
<p>You should also separate your Javascript code into different files. The namespace idiom above helps to have the same namespace across multiple files. The same principles as with other type of code is valid with Javascript, organize the code by area, when it changes, where it is used, etc. Don't be afraid of the additional load time, splitting the files will give you. The files can easily be concatenated with tools  like <a href="http://rake.rubyforge.org/">Rake</a>, <a href="http://www.scons.org/">SCons</a>,  <a href="http://ant.apache.org/">Ant</a> or even a simple:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt>$ cat file1<font color="#990000">.</font>js file2<font color="#990000">.</font>js file3<font color="#990000">.</font>js <font color="#990000">&gt;</font> all<font color="#990000">.</font>js

</tt></pre>
<p>They can also be compressed with <a href="http://www.crockford.com/javascript/jsmin.html">JSMin</a> or <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>.</p>
<p><em>Optimize your environment for development, not for production!</em></p>
<h3>HTML</h3>
<p>HTML is code! Divide your pages into partials by responsibilities. It allows you to keep your pages DRY and readable. <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">The Single Responsibility Principle</a> applies to HTML too.</p>
<p>Make sure you keep the Javascript with the code that it manipulates. If you, for example, have a calendar partial that uses <a href="http://jqueryui.com/demos/datepicker/">jQuery DatePicker</a>, you have to make sure that the partial includes all the necessary Javascript to configure the calendar. Don't keep Javascript code in the page away from the partial. <a href="http://c2.com/cgi/wiki?CommonClosurePrinciple">Things that change together should be together</a>.</p>
<h3>CSS</h3>
<p>Stylesheets are code too. They should also be split into areas that allow you to easily find and navigate them. Use <a href="http://sass-lang.com/">Sass or SCCS</a> to keep your CSS files DRY. Sass is good for designers to. It gives them the ability to use variable names, mixins, etc. and simplifies their usage of semantic names such as <em>notice</em>, and <em>sidebar</em> instead of <em>yellow</em> and <em>left</em>.</p>
<p><em>Optimize your environment for development, not for production!</em></p>
<h2>Separate your Javascript from your HTML</h2>
<p>All too often I see generated HTML pages with Javascript code in them. Don't do it. Keep the HTML free from Javascript.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- DON'T DO THIS!  --&gt;</font></i>
<b><font color="#0000FF">&lt;button</font></b> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">'update-button'</font> <font color="#009900">onclick</font><font color="#990000">=</font><font color="#FF0000">"MyNamespace.updateList();"</font><b><font color="#0000FF">&gt;</font></b>Update List<b><font color="#0000FF">&lt;/button&gt;</font></b>

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// In the Javascript file for the page.</font></i>
MyNamespace<font color="#990000">.</font>updateList <font color="#990000">=</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font><font color="#990000">...</font><font color="#FF0000">}</font>

<font color="#990000">&lt;!--</font> DO THIS<font color="#990000">!</font> <font color="#990000">--&gt;</font>
<font color="#990000">&lt;</font>button id<font color="#990000">=</font><font color="#FF0000">'update-button'</font><font color="#990000">&gt;</font>Update List<font color="#990000">&lt;/</font>button<font color="#990000">&gt;</font>

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// In the Javascript file for the page.</font></i>
MyNamespace<font color="#990000">.</font>updateList <font color="#990000">=</font> <b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font><font color="#990000">...</font><font color="#FF0000">}</font>

$<font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
  $<font color="#990000">(</font><font color="#FF0000">"#update-button"</font><font color="#990000">).</font><b><font color="#000000">click</font></b><font color="#990000">(</font><b><font color="#0000FF">function</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>
    MyNamespace<font color="#990000">.</font><b><font color="#000000">updateList</font></b><font color="#990000">();</font>
  <font color="#FF0000">}</font><font color="#990000">);</font>
<font color="#FF0000">}</font><font color="#990000">);</font>

</tt></pre>
<p>It may seem like there is a lot more code in the good example, but notice the symmetry. The code that attaches the listener is in the same file as the code that uses the listener. This is good. Symmetry is good.</p>
<h3>Use clone()</h3>
<p>Separating the HTML and the Javascript goes both ways. Don't generate HTML code in Javascript. It doesn't matter that it is super simple to do it using <em>jQuery.html()</em>. Keep them separate, use <em>jQuery.clone()</em> instead.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// DON'T DO THIS</font></i>
$<font color="#990000">(</font><font color="#FF0000">"&lt;li data-id='123'&gt;My new item&lt;/li&gt;"</font><font color="#990000">).</font><b><font color="#000000">appendTo</font></b><font color="#990000">(</font><font color="#FF0000">"ul"</font><font color="#990000">);</font>
<i><font color="#9A1900">// OR THIS</font></i>
$<font color="#990000">(</font><font color="#FF0000">"ul"</font><font color="#990000">).</font><b><font color="#000000">append</font></b><font color="#990000">(</font><font color="#FF0000">"&lt;li data-id='123'&gt;My new item&lt;/li&gt;"</font><font color="#990000">);</font>

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- DO THIS --&gt;</font></i>
<b><font color="#0000FF">&lt;ul&gt;</font></b>
<b><font color="#0000FF">&lt;li</font></b> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">"list-template"</font> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"template"</font><b><font color="#0000FF">&gt;</font></b>All .template are hidden (display: none) in the CSS<b><font color="#0000FF">&lt;/li&gt;</font></b>
<b><font color="#0000FF">&lt;/ul&gt;</font></b>

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// AND THIS</font></i>
<b><font color="#0000FF">var</font></b> $clone <font color="#990000">=</font> $<font color="#990000">(</font><font color="#FF0000">"#list-template"</font><font color="#990000">).</font><b><font color="#000000">clone</font></b><font color="#990000">();</font>
$clone<font color="#990000">.</font><b><font color="#000000">attr</font></b><font color="#990000">(</font><font color="#FF0000">"data-id"</font><font color="#990000">,</font> <font color="#FF0000">"123"</font><font color="#990000">).</font><b><font color="#000000">text</font></b><font color="#990000">(</font><font color="#FF0000">"My new item"</font><font color="#990000">).</font><b><font color="#000000">removeClass</font></b><font color="#990000">(</font><font color="#FF0000">"template"</font><font color="#990000">);</font>
$<font color="#990000">(</font><font color="#FF0000">"ul"</font><font color="#990000">).</font><b><font color="#000000">append</font></b><font color="#990000">(</font>$clone<font color="#990000">);</font>

</tt></pre>
<p>The point of this is, again, to keep the HTML separate from the Javascript.</p>
<h2>Decide how content flows between, the page, Javascript and the Server.</h2>
<p>Once you have decided how rich your application should be, you have to decide on a method for moving the data between the HTML Page, Javascript and the Server. My preferred choice is to have every page that is served from the server include a <em>context object</em> with all the static data for the page and to have additional data that belongs to parts of the page be sent as <em>data-attributes</em> on the elements concerned.</p>
<p>The context object will contain all the data that is commonly needed in the page.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">// Sample context object that is generated with the page.</font></i>
MyNamespace <font color="#990000">=</font> MyNamespace <font color="#990000">||</font> <font color="#FF0000">{}</font><font color="#990000">;</font>
MyNamespace<font color="#990000">.</font>Context <font color="#990000">=</font> <font color="#FF0000">{</font>
  user<font color="#990000">:</font> <font color="#FF0000">{</font>
    id<font color="#990000">:</font> <font color="#FF0000">"28"</font><font color="#990000">,</font>
    name<font color="#990000">:</font> <font color="#FF0000">"Anders Janmyr"</font>
  <font color="#FF0000">}</font><font color="#990000">,</font>
  tournament<font color="#990000">:</font> <font color="#FF0000">{</font>
    id<font color="#990000">:</font> <font color="#FF0000">"78344"</font><font color="#990000">,</font>
    name<font color="#990000">:</font> <font color="#FF0000">"Fifa World Cup"</font>
  <font color="#FF0000">}</font>
<font color="#FF0000">}</font><font color="#990000">;</font>
</tt></pre>
<p>I use the context object(s) to keep state on the client to. If it is important that the page looks exactly the same, even if the user reloads the page, I make sure that the state I stick into my context object is synched back to the server. This can easily be done, asynchronously, with Ajax and does not affect performance noticeable.</p>
<p>Elements specific data is sent along with the element it defines.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- Element specific data attached to the elements with data-attributes --&gt;</font></i>
<b><font color="#0000FF">&lt;ul</font></b> <font color="#009900">id</font><font color="#990000">=</font><font color="#FF0000">"tournament-menu"</font><b><font color="#0000FF">&gt;</font></b>
  <b><font color="#0000FF">&lt;li</font></b> <font color="#009900">data-id</font><font color="#990000">=</font><font color="#FF0000">"78344"</font> <font color="#009900">data-participant-count</font><font color="#990000">=</font><font color="#FF0000">"64"</font><b><font color="#0000FF">&gt;</font></b>Fifa World Cup<b><font color="#0000FF">&lt;/li&gt;</font></b>
  <b><font color="#0000FF">&lt;li</font></b> <font color="#009900">data-id</font><font color="#990000">=</font><font color="#FF0000">"666"</font>  <font color="#009900">data-participant-count</font><font color="#990000">=</font><font color="#FF0000">"44"</font><b><font color="#0000FF">&gt;</font></b>Americas Cup<b><font color="#0000FF">&lt;/li&gt;</font></b>
  <b><font color="#0000FF">&lt;li</font></b> <font color="#009900">data-id</font><font color="#990000">=</font><font color="#FF0000">"1464"</font> <font color="#009900">data-participant-count</font><font color="#990000">=</font><font color="#FF0000">"32"</font><b><font color="#0000FF">&gt;</font></b>Rugby World Cup<b><font color="#0000FF">&lt;/li&gt;</font></b>
<b><font color="#0000FF">&lt;/ul&gt;</font></b>

</tt></pre>
<p>The same argument as with the context object applies, as soon as I change an element in the GUI i need to feed that information back to the server. With elements I usually send the information to the server before updating the GUI, since element specific data is usually permanent data and not just session data.</p>
<p>An alternative solution to the <em>context object</em> above is to use the body element as the data-container, like this:</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><b><font color="#0000FF">&lt;body</font></b> <font color="#009900">data-user-id</font><font color="#990000">=</font><font color="#FF0000">"28"</font> <font color="#009900">data-user-name</font><font color="#990000">=</font><font color="#FF0000">"Anders Janmyr"</font> <font color="#009900">data-tournament-id</font><font color="#990000">=</font><font color="#FF0000">"78344"</font> <font color="#009900">data-tournament-name</font><font color="#990000">=</font><font color="#FF0000">"Fifa World Cup"</font><b><font color="#0000FF">&gt;</font></b>

</tt></pre>
<p>I tend to use the context object because I find it easier to add functionality to it than to the DOM element.</p>
<p>Only send the data that is needed to the client with the page. The rest of the data should be loaded on demand, with Ajax. Both JSON data and HTML templates can be loaded on demand. There is no need to deliver the entire page at once. Experiment and do what gives the best user experience.</p>
<h2>Use file watchers to speed up feedback</h2>
<p>If you compare the feedback cycle of Javascript and HTML development to Java and C# development, you are probably very happy with the short, tight feedback loop. This doesn't mean that you should be content. A feedback cycle cannot be too short.</p>
<p><a href="http://xrefresh.binaryage.com/">xrefresh</a> for Firefox and IE, and <a href="http://github.com/mockko/livereload">LiveReload</a> for Safari and Chrome, are a couple of tools that will tighten your feedback loop even more.</p>
<p>Both tools are file watchers that listen to changes for files and refresh the browser when they change. If you combine this with two screens, you will have one screen for the browser, that updates continuously, while you edit your code on the other screen. Fabulous!</p>
<h2>Conclusion</h2>
<p>Rich web applications are very close to the traditional client-server model. We have to keep state on the client side to give the user a good experience. At the same time the application state, and indeed the entire application, can be swept away by a click of a button or a page reload.</p>
<p>This puts new demands on us as developers. We have to realize that we are responsible for the entire application, not just the business logic, but the HTML and CSS too. More that anything, we have to realize that Javascript is a first class programming language with its own programming techniques, which we need to master to be able to develop good web applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/08/16/good-practices-for-rich-web-applications/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>No Deadlines</title>
		<link>http://blog.jayway.com/2010/08/12/no-deadlines/</link>
		<comments>http://blog.jayway.com/2010/08/12/no-deadlines/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 05:15:58 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5991</guid>
		<description><![CDATA[deadline, from New Oxford American Dictionary the latest time or date by which something should be completed : the deadline for submissions is February 5th. historical a line drawn around a prison beyond which prisoners were liable to be shot. Deadlines in software development induce unnecessary stress, and transform otherwise enjoyable activities into chores. We [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>deadline</strong>, from New Oxford American Dictionary</p>
<ol>
<li>
<p>the latest time or date by which something should be completed : the deadline for submissions is February 5th.</p>
</li>
<li>
<p>historical a line drawn around a prison beyond which prisoners were liable to be shot.</p>
</li>
</ol>
</blockquote>
<p>Deadlines in software development induce unnecessary stress, and transform otherwise enjoyable activities into chores.</p>
<p>We need to replace deadlines with livelines. A liveline is a deployed, working version of a product. It is after the fact, a milestone set when you are done.</p>
<h2>How Deadlines Are Set</h2>
<p>Deadlines are set in different ways in different projects.</p>
<p>In the classic <a href="http://www.amazon.com/Death-March-2nd-Edward-Yourdon/dp/013143635X?tag=thtasta-20">Death March </a> project, a deadline is set by someone outside the project with no clue whatsoever about how long it will take. Typically this kind of deadline is based on some external event, where the project will be demonstrated. The event may be a conference, or something similar.</p>
<p>In Scrum, the deadlines are set by the process itself. Most Scrum projects I have worked with use a two week deadline instead of the recommended 30 days. The team decides how many stories they can complete by that deadline, based on rough estimations done in the sprint planning.</p>
<p>Some projects set deadlines by a developer being asked (or asking himself) when the task will be done. The developer will say: "I'll have it done by 3 o'clock", a wild guess.</p>
<p>The problem with all these approaches is that the deadlines are, <em>at best</em>, based on guesses of the team doing the development. But guesses are guesses, not something you can set a deadline by.</p>
<p>And deadlines work both ways, a task is <em>very</em> rarely completed before it's deadline.</p>
<h2>Kinds of Deadlines</h2>
<h3>Real Deadlines</h3>
<p>If a task is not completed by a certain time someone will die. This kind of task is really common in novels and movies. A clock is ticking and if the task is not completed by the time the clock reaches zero, BOOM!</p>
<p>If have never seen this kind of deadline in software development, but they may exist.</p>
<h3>Planning Deadlines</h3>
<p>Some deadlines are set in order to simplify planning. In the large Death March projects, the plan is to show the project at a certain date. In integration projects, the deadlines are set to be able to synchronize with other teams working on the same product.</p>
<p>This kind of deadlines are actually understandable, but I am arguing that they actually slow down projects by introducing stress, which introduces bugs. The bugs introduce bugs in the dependent projects, and slows down many projects instead of just one.</p>
<h3>Motivational Deadlines</h3>
<p>Motivation deadlines are set to get people to work. The assumption is that if a deadline is not set people will procrastinate over different ideas, or just slack off since there is no date set for the release anyway.</p>
<p>This is, absolutely, the worst kind of deadline. If the team is not motivated enough to do a good job without a deadline, you need to get a new team, not set a deadline.</p>
<h2>Why Deadlines Are Bad</h2>
<h3>Stress</h3>
<p>Consider walking on a one foot wide plank. If it is on the ground it is easy. If it is two meters up it is difficult, but if you put it at twenty meters it is a whole different ballgame. That is what its like with deadlines, they put an unnecessary pressure on people.</p>
<h3>Loss of Motivation</h3>
<blockquote><p><em>Work</em> consist of whatever a body is obliged to do, and <em>play</em> consists of whatever one is not obliged to do. -- Mark Twain</p>
</blockquote>
<p>In his book, <a href="http://www.amazon.com/Drive-Surprising-Truth-About-Motivates/dp/1594488843?tag=thtasta-20">Drive</a>, Dan Pink differentiates between <em>intrinsic</em> motivation and <em>extrinsic</em> motivation.</p>
<p>Extrinsic motivation is motivation driven by carrots and sticks, rewards and punishment. It works well for simple, well-defined tasks.</p>
<p>Intrinsic motivation works well for creative, ill-defined tasks, like programming.</p>
<p>Intrinsic motivation is fragile, <em>it disappears in the presence of carrots and sticks</em>. A deadline is a stick, whether set by ourselves or someone else.</p>
<h2>Livelines</h2>
<p>A liveline is, quite simply, a task that is done. When a task is done, it is easy for others to use it, in their plans or in reality. If we want to build a stable house, we should build it on stable ground.</p>
<p>When using livelines the iteration length is the same length as a task. This is usually a lot shorter than the usual one to four weeks common in Scrum and other agile methods. It also leads to variations in the iteration length.</p>
<p>If you implement your task all the way into production, you get <a href="http://timothyfitz.wordpress.com/2009/02/10/continuous-deployment-at-imvu-doing-the-impossible-fifty-times-a-day/">Continuous Deployment</a> or <a href="http://www.amazon.com/gp/product/0321601912?tag=thtasta-20">Continuous Delivery</a>. This puts a lot of pressure on your tooling and on automation but, I believe this is the way to go.</p>
<p>It is 2010, it is time to get rid of deadlines.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/08/12/no-deadlines/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Ruby and Rails Summer Reading</title>
		<link>http://blog.jayway.com/2010/06/20/ruby-and-rails-summer-reading/</link>
		<comments>http://blog.jayway.com/2010/06/20/ruby-and-rails-summer-reading/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 06:26:49 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5782</guid>
		<description><![CDATA[If you ask a Ruby programmer why he is using Ruby, you will probably get several answers like: It is dynamic. I allows me too keep my code DRY. I get results faster. Aside from all these statements, there is one statement that almost always comes up. I use Ruby because it makes me happy! [...]]]></description>
			<content:encoded><![CDATA[<p>If you ask a Ruby programmer why he is using Ruby, you will probably get several answers like: It is dynamic. I allows me too keep my code DRY. I get results faster. Aside from all these statements, there is one statement that almost always comes up.</p>
<blockquote><p><strong>I use Ruby because it makes me happy!</strong></p>
</blockquote>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" src="http://3.bp.blogspot.com/_ELioussa2vo/TB2yZpIl5XI/AAAAAAAAAfw/vCZCP2XcwZE/s320/smiley.jpg" /></div>
<p>If you search Amazon for Ruby you get 8,829 Results and if you search for Rails you get<br />
14,113 Results. What books should you get?</p>
<p>Here are the books I recommend:</p>
<h2>Ruby</h2>
<p><a href="http://www.amazon.com/Beginning-Ruby-Novice-Professional/dp/1590597664?tag=thtasta-20">Beginning Ruby</a> is a superb book for people coming new to Ruby.</p>
<p>If you are not a complete novice, you should instead go for<br />
<a href="http://www.amazon.com/Well-Grounded-Rubyist-David-Black/dp/1933988657?tag=thtasta-20">The Well Grounded Rybyist</a>.</p>
<p>When you have finished <em>the Well Grounded Rubyist</em>, you are ready for <a href="http://www.amazon.com/Metaprogramming-Ruby-Facets-Paolo-Perrotta/dp/1934356476/?tag=thtasta-20">Metaprogramming Ruby</a> where you learn metaprogramming techniques to help you keep your code DRY and readable.</p>
<h2>Rails</h2>
<p>Since Rails is currently being updated to version 3, there are not that many books available. I can recommend <a href="http://www.amazon.com/Rails-Way-Addison-Wesley-Professional-Ruby/dp/0321601661?tag=thtasta-20">The Rails3 Way</a>, currently only <a href="http://www.informit.com/store/product.aspx?isbn=0132480336">available as a Rough-Cut</a>.</p>
<p>The lack of updated books is not a problem. The <a href="http://guides.rails.info/getting_started.html">The Ruby on Rails Guides</a> are excellent. Make sure you select the edge version.</p>
<p>Even more information is available through the videos from <a href="http://en.oreilly.com/rails2010">Railsconf</a>, <a href="http://railsconf.blip.tv/rss/itunes/">subscribe to them in iTunes</a>. The first videos where published, the day after they were filmed. Fantastic!</p>
<p>Have a happy Ruby-red summer.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/06/20/ruby-and-rails-summer-reading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seamless Web Development</title>
		<link>http://blog.jayway.com/2010/06/08/seamless-web-development/</link>
		<comments>http://blog.jayway.com/2010/06/08/seamless-web-development/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 19:48:36 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5713</guid>
		<description><![CDATA[Do you remember the time before 9/11 when you could arrive to the airport 10 minutes before the plane's departure and just walk on to the plane. Now, you have to arrive at least an hour before the plane departs and you have to strip to get through a security. The security is just for [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember the time before 9/11 when you could arrive to the airport 10 minutes before the plane's departure and just walk on to the plane. Now, you have to arrive at least an hour before the plane departs and you have to strip to get through a security.</p>
<p>The security is just for show. I don't know how many times I have forgotten a bottle in my carry-on and I haven't noticed it until I unpacked the bag at home. There a number of ways to get a big bang for your bucks, that aren't checked at security</p>
<p><img src="http://imgs.xkcd.com/comics/bag_check.png" title="XKCD Bag Check" alt="XKCD Bag Check" /></p>
<p>So what does this have to do with web development? Web development in a compiled language is like traveling after the terrorist attacks. You have to go through a lot of time consuming controls that are, mostly, for show. This dramatically slows down your development speed.</p>
<p>Since I did my <a href="http://anders.janmyr.com/2010/04/aspnet-mvc-vs-rails3.html">comparison between Rails and .NET MVC</a> I have done a lot of coding in both environments, and my conclusion is that Rails is a lot faster to develop with than .NET MVC.</p>
<p>The reason for this is that .NET MVC puts up a lot of seams that hinders development.</p>
<h2>Seam #1, Runtime Development</h2>
<p>When I'm using Rails for development I very rarely restart the server. When I make a change to the view, controller, model or database,  the changes are visible on the next request. Immediate feedback!</p>
<p>In Visual Studio, there is something called "Edit and Continue", that you can set to allow you to make changes to the code while you are debugging. Mooah ha ha ha ha ha ha ha haaaaaaa! This sucks so bad that I cannot help bursting out in a crazy laughter. The flow disappears.</p>
<p><img src="http://2.bp.blogspot.com/_ELioussa2vo/TA5505v-TUI/AAAAAAAAAfg/fo658aXs84U/s320/Edit_and_continue.png" title="Edit and Continue in your face!" alt="Edit and Continue" /></p>
<p>If I make changes <em>to the view</em> I can continue, unless I make changes to the I18N resources, because these are obviously not mutable. WTF!</p>
<p>Sometimes it is actually possible to make changes to the code, but when you try to save the changes "Edit and Continue"'s big brother appears and smacks you in the head, and the flow is gone again!</p>
<p><img src="http://1.bp.blogspot.com/_ELioussa2vo/TA555qfu4AI/AAAAAAAAAfo/dfsQxw_s-H8/s1600/Edit_and_continue2.png" title="Mooah ha ha ha ha ha haaaaaaa!" alt="Edit and Continue2" /></p>
<p>There is really no <em>good</em> reason for this. I'm sure there are a number of technical reasons having to do with type safety and class reloading, but I don't care. Just reload everything, do it fast and unnoticeable!</p>
<h2>Seam #2, The Javascript Impedance Mismatch</h2>
<p>Hopefully everyone has realized that Javascript is the hottest thing in web development! A language that has been spat on, and laughed at, has become the most wide spread programming language in the world. It's available in every browser and on every programming platform.</p>
<p>I have been talking about the greatness of Javascript for a few years now and it is great to see when someone finally get it.</p>
<blockquote><p>"Its great, I just change this little thing, then click reload, and it works immediately!" -- A collegue, seeing the coolness of Javascript.</p>
</blockquote>
<p>Javascript is Lisp in C-clothing and Lisp is the language that God used to create the world.</p>
<p><img src="http://imgs.xkcd.com/comics/lisp.jpg" title="XKCD Lisp" alt="Lisp" /></p>
<p>OK, Javascript is cool, and we all use it, but what is this impedance mumbo jumbo? Since a large part of a modern web application will be developed in Javascript, when we move over to the server side and everything is static and compiled, we are in for a lot of frustration.</p>
<p><strong>Common Development Flow</strong></p>
<ul>
<li>Make a change to the GUI, in Javascript.</li>
<li>Reload</li>
<li>Make another change</li>
<li>Reload</li>
<li>Realizing that you need to change the controller for the next change.</li>
<li>Aaaaaaaaaarrrrrrrrrrrggggghhhhhhhh, I have to restart the application.</li>
<li>What was it that I was working on again? I'll go for a cup of coffee!</li>
</ul>
<p>This is the Javascript impedance mismatch. If you are developing in a dynamic language you expect it to work like this all the time. You get used to it. It is better!</p>
<p>With Rails and Ruby, you get the same flow on the backend as you do on the client. The static-dynamic mismatch is gone. (There is still a language mismatch, between Javascript and Ruby, but it is minuscule by comparison.)</p>
<h2>Seam #3, Cruft</h2>
<p>Compare the following two partials</p>
<p><!-- Generator: GNU source-highlight 3.0.1<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- BookedCustomer.aspx partial in .NET MVC--&gt;</font></i>
&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<b><font color="#0000FF">&lt;Bergqvists.Models.Booking&gt;</font></b>" %&gt;

<b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"view"</font><b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"customer title"</font><b><font color="#0000FF">&gt;</font></b>&lt;%= Html.ActionLink(Model.Customer.Fullname, "Details", "Customers", new { Id = Model.CustomerID}, null) %&gt;<b><font color="#0000FF">&lt;/div</font></b> <b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"status"</font><b><font color="#0000FF">&gt;</font></b>&lt;%= Html.Encode(Model.BookingStatus.Name) %&gt;<b><font color="#0000FF">&lt;/div</font></b> <b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"booking-date"</font><b><font color="#0000FF">&gt;</font></b>&lt;%=ViewRes.BookingStrings.BookedOn %&gt;
    &lt;%= string.Format("{0:D}", Model.BookingDate) %&gt;<b><font color="#0000FF">&lt;/div&gt;</font></b>

<b><font color="#0000FF">&lt;/div&gt;</font></b>
&lt;% Html.RenderPartial("BookingStatusLinks", Model);%&gt;

</tt></pre>
<p><!-- Generator: GNU source-highlight 3.0.1<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><i><font color="#9A1900">&lt;!-- _booked_customer.html.erb partial --&gt;</font></i>
<b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"view"</font><b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"customer title"</font><b><font color="#0000FF">&gt;</font></b>&lt;%= link_to customer.fullname, customer_path(customer)<b><font color="#0000FF">&lt;/div</font></b> <b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"status"</font><b><font color="#0000FF">&gt;</font></b>&lt;%= customer.booking_status.name %&gt;<b><font color="#0000FF">&lt;/div</font></b> <b><font color="#0000FF">&gt;</font></b>
    <b><font color="#0000FF">&lt;div</font></b> <font color="#009900">class</font><font color="#990000">=</font><font color="#FF0000">"booking-date"</font><b><font color="#0000FF">&gt;</font></b>&lt;%=t :booked_on %&gt;
    &lt;%= customer.booking_date, :format =&gt; :short %&gt; %&gt;<b><font color="#0000FF">&lt;/div&gt;</font></b>

<b><font color="#0000FF">&lt;/div&gt;</font></b>
&lt;%= render :booking_status_links, :locals =&gt; {:customer =&gt; customer} %&gt;

</tt></pre>
<p>As you can see the corresponding ERB is not much different, but at least everything in the code is meaningful. And what customer value does the first line bring?</p>
<p>  &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Bergqvists.Models.Booking>" %></p>
<p>Nada! Zilch! It is only there to please the compiler. Cruft!</p>
<p>Now, most Ruby programmers I know use HAML instead of ERB and this cleans the view up considerably.</p>
<p><!-- Generator: GNU source-highlight 3.0.1<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --></p>
<pre><tt><font color="#990000">.</font>view
  <font color="#990000">.</font>customer<font color="#990000">.</font><font color="#009900">title</font><font color="#990000">=</font> link_to customer<font color="#990000">.</font>fullname<font color="#990000">,</font> customer_path<font color="#990000">(</font>customer<font color="#990000">)</font>
  <font color="#990000">.</font><font color="#009900">status</font><font color="#990000">=</font> customer<font color="#990000">.</font>booking_status<font color="#990000">.</font>name
  <font color="#990000">.</font>booking-date
    <font color="#990000">=</font> t <font color="#990000">:</font>booked_on
    <font color="#990000">=</font> customer<font color="#990000">.</font>booking_date<font color="#990000">,</font> <font color="#990000">:</font>format <font color="#990000">=&gt;</font> <font color="#990000">:</font>short

<font color="#990000">=</font> render <font color="#990000">:</font>booking_status_links<font color="#990000">,</font> <font color="#990000">:</font>locals <font color="#990000">=&gt;</font> {<font color="#990000">:</font>customer <font color="#990000">=&gt;</font> customer}

</tt></pre>
<p>Beautiful!</p>
<p>Comparing Haml with ASP is not really fair, since it is possible to use <a href="todo">NHaml</a> instead of ASP in .NET MVC, but no one I know does that, maybe I hang with the wrong .Net people. NHAML is great, please switch it to the default for .NET MVC.</p>
<p>The example above is in the view layer. But the cruft spreads all over the stack.</p>
<h2>Seam #4, Testing</h2>
<p>Testing is always an interesting topic. The number one reason testing is not done is that it is to cumbersome. In .NET MVC I find it painful to test the controller and view layers, to the extent that I don't do it. With Rails on the other hand, you are setup for test from the start.</p>
<p>I always try to put as much code as possible into the model layer, but some code belongs in the controller. I want it to be easily tested too.</p>
<p>It is also worth noting that the Ruby community is driving the testing profession forward, with tools like RSpec, Capybara, Webkit and Cucumber.</p>
<p>Yes, I know that there are several implementation languages for Cucumber. It's possible to write your steps, in C# or Java or almost any other language. My advice is, <em>don't do it!</em> There is compilation, waiting, pain and suffering down that road. Use a dynamic language to test your code.</p>
<h2>Conclusion</h2>
<p>So what's my point? It's time to stop compiling and quit slacking off.</p>
<p><img src="http://imgs.xkcd.com/comics/compiling.png" title="XKCD Compiling" alt="XKCD Compiling" /></p>
<p><strong>Rails should be the default choice for web development.</strong> You should have a very good reason for not choosing Rails on your next web project.</p>
<p>I know it may feel tough to have invested many years in something else, but as the turks say.</p>
<blockquote><p>No matter how far down the wrong path you've gone, turn back! --- Turkish Proverb</p>
</blockquote>
<p>Don't take my word for it. Check out <a href="http://www.infoq.com/articles/architecting-tekpub">this interview</a> and <a href="http://www.infoq.com/presentations/ford-large-rails">this presentation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/06/08/seamless-web-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC vs. Rails3</title>
		<link>http://blog.jayway.com/2010/04/23/asp-net-mvc-vs-rails3/</link>
		<comments>http://blog.jayway.com/2010/04/23/asp-net-mvc-vs-rails3/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 08:58:55 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5422</guid>
		<description><![CDATA[I recently was contacted to implement an ASP.NET MVC application and I saw this as a great opportunity to compare it with Rails3. What immediately strikes you when you start with ASP.NET MVC is how similar it is to Rails. No one can steal ideas like Microsoft! Rails ASP.NET MVC Purpose (if not obvious) /app/models [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was contacted to implement an ASP.NET MVC application and I saw this as a great opportunity to compare it with Rails3.<br />
What immediately strikes you when you start with ASP.NET MVC is how similar it is to Rails. No one can steal ideas like Microsoft!</p>
<table>
<tbody>
<tr>
<th>Rails
        </th>
<th>ASP.NET MVC
        </th>
<th>Purpose (if not obvious)
    </th>
</tr>
<tr>
<td>/app/models</td>
<td>/Models</td>
<td></td>
</tr>
<tr>
<td>/app/controllers</td>
<td>/Controllers</td>
<td></td>
</tr>
<tr>
<td>/app/views</td>
<td>/Views</td>
<td></td>
</tr>
<tr>
<td>/public/javascript</td>
<td>/Scripts</td>
<td></td>
</tr>
<tr>
<td>/public</td>
<td>/Content</td>
<td></td>
</tr>
<tr>
<td>/db</td>
<td>/App_Data</td>
<td>Database data, such as migrations, and models.</td>
</tr>
<tr>
<td>/test (unit, functional, fixtures, performance)</td>
<td>Separate VS projects</td>
</tr>
<tr>
<td>/config</td>
<td>/Global.asax, /Properties, Web.config</td>
<td>Configuration</td>
</tr>
</tbody>
</table>
<h3>
<p>Project Generation</h3>
<p>Both Rails and ASP.NET MVC relies on code generation to get you started, but the methods they use are different.<br />
Rails uses the command line, which is natural since the Rails approach is to not rely on anything but a good programming editor and the command line.</p>
<pre><tt>$ rails tapir
      create  <span style="color: #990000;">.</span>gitignore
	  <span style="color: #990000;">...</span>
      create  app/controllers/application_controller<span style="color: #990000;">.</span>rb
      <span style="color: #990000;">...</span>
      create  app/views/layouts
      create  config/database<span style="color: #990000;">.</span>yml
      create  db/seeds<span style="color: #990000;">.</span>rb
	  <span style="color: #990000;">...</span>
      create  public/javascripts/application<span style="color: #990000;">.</span>js
	  <span style="color: #990000;">...</span>
      create  test/unit
	  <span style="color: #990000;">...</span>
</tt></pre>
<p>ASP.NET MVC uses Wizards inside Visual Studio, which is also expected since Microsoft has a long history of IDE-centric application building.<br />
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/_ELioussa2vo/S9FeCW86GtI/AAAAAAAAAeg/Fa-g7QGSjkw/s1600/new_asp_mvc.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" src="http://3.bp.blogspot.com/_ELioussa2vo/S9FeCW86GtI/AAAAAAAAAeg/Fa-g7QGSjkw/s320/new_asp_mvc.png" /></a></div>
<p>Worth noting is that I didn't really want to use VS Unit Test, since I normally prefer NUnit, but after one hour of googling and testing I gave up and went with VS Unit Test anyway.</p>
<h3>
<p>Environment</h3>
<p>When Rails is installed it comes pre-configured with three different environment, development, test and production. It is as easy as writing RAILS_ENV=test to switch from the default development environment to the test environment. In this area Rails really shines. There is nothing similar in ASP.NET MVC.</p>
<h3>
<p>The Model</h3>
<h4>
<p>Rails Model</h4>
<p>Rails comes pre-configured with an ActiveRecord model, if you don't write otherwise. If you want to use something else it is very easy. Supported frameworks are among others, Neo4J, MongoDB, and DataMapper.<br />
To create a model in Rails3 you use a command line generator. The generator generates a model, a migration and tests. And you can, of course, choose what kind of model you wish to generate (-o), as well as what kind of testing framework you want to use (-t). Here I just go with the default:</p>
<pre><tt>$ rails g model customer name<span style="color: #990000;">:</span>string email<span style="color: #990000;">:</span>string
      invoke  active_record
      create    db/migrate<span style="color: #990000;">/</span>20100419094010_create_customers<span style="color: #990000;">.</span>rb
      create    app/models/customer<span style="color: #990000;">.</span>rb
      invoke    test_unit
      create      test/unit/customer_test<span style="color: #990000;">.</span>rb
      create      test/fixtures/customers<span style="color: #990000;">.</span>yml

</tt></pre>
<p>Rails comes preconfigured with Sqlite3, and you don't have to do anything to configure the default database. Moreover, the configuration is set up to use three different databases, one for each environment.<br />
Rails, performs all database changes through scripts, migrations. This is invaluable when you want to upgrade a database that is in production, or when multiple developers are changing the same database model. The migration scripts allow seamless migrations between the different databases.</p>
<pre><tt><i><span style="color: #9a1900;"># An example of a migration</span></i>
<b><span style="color: blue;">class</span></b> CreateCustomers <span style="color: #990000;">&lt;</span> ActiveRecord<span style="color: #990000;">::</span>Migration

  <i><span style="color: #9a1900;"># Called when migrating up to this version</span></i>
  <b><span style="color: blue;">def</span></b> <b><span style="color: blue;">self</span></b><span style="color: #990000;">.</span>up
    create_table <span style="color: #990000;">:</span>customers <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>t<span style="color: #990000;">|</span>
      t<span style="color: #990000;">.</span>string <span style="color: #990000;">:</span>name
      t<span style="color: #990000;">.</span>string <span style="color: #990000;">:</span>email
      t<span style="color: #990000;">.</span>timestamps
    <b><span style="color: blue;">end</span></b>
  <b><span style="color: blue;">end</span></b>

  <i><span style="color: #9a1900;"># Called when migrating down from this version</span></i>
  <b><span style="color: blue;">def</span></b> <b><span style="color: blue;">self</span></b><span style="color: #990000;">.</span>down
    drop_table <span style="color: #990000;">:</span>customers
  <b><span style="color: blue;">end</span></b>
<b><span style="color: blue;">end</span></b>

</tt></pre>
<p>To move between the different versions of the database we use the <i>rake db:migrate</i> command.</p>
<pre><tt><i><span style="color: #9a1900;"># Migrate to the latest version</span></i>
$ rake db<span style="color: #990000;">:</span>migrate 

<i><span style="color: #9a1900;"># Migrate to a specific version</span></i>
$ rake db<span style="color: #990000;">:</span>migrate <span style="color: #009900;">VERSION</span><span style="color: #990000;">=</span><span style="color: #993399;">20080906120000</span> 

<i><span style="color: #9a1900;"># Rollback one version</span></i>
$ rake db<span style="color: #990000;">:</span>rollback 

<i><span style="color: #9a1900;"># Rollback three versions</span></i>
$ rake db<span style="color: #990000;">:</span>rollback <span style="color: #009900;">STEP</span><span style="color: #990000;">=</span><span style="color: #993399;">3</span>

</tt></pre>
<p>Read more about migrations in the <a href="http://guides.rubyonrails.org/migrations.html">Migrations Guide</a></p>
<h4>
<p>ASP.NET MVC Model</h4>
<p>ASP.NET MVC is not a full stack framework and it does not come preconfigured with a model. It is possible to choose between many solutions, such as NHibernate, Entity Framework or LINQ-to-SQL. I choose LINQ-to-SQL because I think it is an elegant, lightweight OR-Mapper, that is easy to work with. Too bad, it isn't prioritized by Microsoft.<br />
Unfortunately, there is nothing like migrations in LINQ-to-SQL. So I use the LINQ-to-SQL design tool to design the classes.<br />
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/_ELioussa2vo/S9FezPBTiuI/AAAAAAAAAeo/2-hPL_i1h70/s1600/linq_to_sql_designer.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" src="http://3.bp.blogspot.com/_ELioussa2vo/S9FezPBTiuI/AAAAAAAAAeo/2-hPL_i1h70/s320/linq_to_sql_designer.png" /></a></div>
<p>
You can then create the database from the model. It is also possible to do it the opposite way, to create the database first and then generate the model, but I prefer this way.</p>
<pre><tt><i><span style="color: #9a1900;">// Creating a database from a LINQ-to-SQL DataContext</span></i>
public <span style="color: teal;">void</span> <b><span style="color: black;">CreateDatabase</span></b><span style="color: #990000;">(</span><span style="color: #009900;">bool</span> force<span style="color: #990000;">)</span>
<span style="color: red;">{</span>
    <span style="color: teal;">var</span> db <span style="color: #990000;">=</span> <span style="color: teal;">new</span> <b><span style="color: black;">TapirDataContext</span></b><span style="color: #990000;">(</span>@<span style="color: red;">"c:</span><span style="color: #cc33cc;">\t</span><span style="color: red;">apirdb.mdf"</span><span style="color: #990000;">);</span>
    <b><span style="color: blue;">if</span></b> <span style="color: #990000;">(</span>db<span style="color: #990000;">.</span><b><span style="color: black;">DatabaseExists</span></b><span style="color: #990000;">()</span> <span style="color: #990000;">||</span> force<span style="color: #990000;">)</span>
    <span style="color: red;">{</span>
        Console<span style="color: #990000;">.</span><b><span style="color: black;">WriteLine</span></b><span style="color: #990000;">(</span><span style="color: red;">"Deleting old database..."</span><span style="color: #990000;">);</span>
        db<span style="color: #990000;">.</span><b><span style="color: black;">DeleteDatabase</span></b><span style="color: #990000;">();</span>
    <span style="color: red;">}</span>
    db<span style="color: #990000;">.</span><b><span style="color: black;">CreateDatabase</span></b><span style="color: #990000;">();</span>
<span style="color: red;">}</span>

</tt></pre>
<p>As you understand this is not a viable solution for migrating a production database, but it is good enough (almost) for development and testing, since I can allow myself to recreate the database every time. Once I get a larger database, the time it takes to set it up will force me to go with a better way. The lack of migrations hurts.</p>
<h4>
<p>Query Language</h4>
<p>Rails3 uses AREL, Active Record Relations, and LINQ-to-SQL uses LINQ (surprise!). They are both beautiful solutions and remarkably similar. Both solutions create lazy, composable queries, that are not executed until the latest possible time, when a result is needed. In LINQ, you can see Eric Meijer's Haskell shining through like magic, and in AREL, the beauty of Ruby.</p>
<pre><tt><i><span style="color: #9a1900;"># A simple query with AREL</span></i>
User<span style="color: #990000;">.</span>where<span style="color: #990000;">(</span>users<span style="color: #990000;">[:</span>name<span style="color: #990000;">].</span>eq<span style="color: #990000;">(</span><span style="color: red;">'Anders'</span><span style="color: #990000;">)).</span>order<span style="color: #990000;">(</span><span style="color: red;">'users.id DESC'</span><span style="color: #990000;">).</span>limit<span style="color: #990000;">(</span><span style="color: #993399;">20</span><span style="color: #990000;">)</span>

</tt></pre>
<pre><tt><i><span style="color: #9a1900;">// The same with C#</span></i>
<i><span style="color: #9a1900;">// Lambda Syntax</span></i>
db<span style="color: #990000;">.</span>Users<span style="color: #990000;">.</span><b><span style="color: black;">where</span></b><span style="color: #990000;">(</span>u <span style="color: #990000;">=&gt;</span> u<span style="color: #990000;">.</span>Name <span style="color: #990000;">==</span> <span style="color: red;">"Anders"</span><span style="color: #990000;">).</span><b><span style="color: black;">orderBy</span></b><span style="color: #990000;">(</span>u <span style="color: #990000;">=&gt;</span> u<span style="color: #990000;">.</span>Id<span style="color: #990000;">).</span><b><span style="color: black;">Take</span></b><span style="color: #990000;">(</span><span style="color: #993399;">20</span><span style="color: #990000;">)</span>

<i><span style="color: #9a1900;">// LINQ Syntax</span></i>
<span style="color: #990000;">(</span>from u <span style="color: teal;">in</span> db<span style="color: #990000;">.</span>Users
<span style="color: teal;">where</span> u<span style="color: #990000;">.</span>Name <span style="color: #990000;">==</span> <span style="color: red;">"Anders"</span>
<span style="color: teal;">orderby</span> u<span style="color: #990000;">.</span>Id descending
<span style="color: teal;">select</span> u<span style="color: #990000;">).</span><b><span style="color: black;">Take</span></b><span style="color: #990000;">(</span><span style="color: #993399;">20</span><span style="color: #990000;">);</span>

</tt></pre>
<p>One thing that you don't get with LINQ-to-SQL is all the methods that are dynamically created, by need, in Rails, such as <i>find_by_name</i>, <i>find_by_name_and_age</i>, etc.</p>
<h3>
<p>The Controller</h3>
<p>In ASP.NET MVC, it is easy to create a controller, you just right-click on the controllers folder and select <i>Add &gt; Controller</i>. You then get a dialog where you can type in the name of the controller and, optionally, if you like to create default methods for the standard CRUD scenario, just select the checkbox.<br />
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/_ELioussa2vo/S9Fe8OUhFHI/AAAAAAAAAe4/8ftOIxwf2Vg/s1600/generate_controller.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" src="http://1.bp.blogspot.com/_ELioussa2vo/S9Fe8OUhFHI/AAAAAAAAAe4/8ftOIxwf2Vg/s320/generate_controller.png" /></a></div>
<p></p>
<pre><tt>
public <span style="color: teal;">class</span> CustomersController <span style="color: #990000;">:</span> Controller <span style="color: red;">{</span>
      <i><span style="color: #9a1900;">// GET: /Customers/</span></i>
      public <span style="color: teal;">ActionResult</span> <b><span style="color: black;">Index</span></b><span style="color: #990000;">()</span> <span style="color: red;">{</span>
          <b><span style="color: blue;">return</span></b> <b><span style="color: black;">View</span></b><span style="color: #990000;">();</span>
      <span style="color: red;">}</span>

      <i><span style="color: #9a1900;">// GET: /Customers/Details/5</span></i>
      public <span style="color: teal;">ActionResult</span> <b><span style="color: black;">Details</span></b><span style="color: #990000;">(</span><span style="color: #009900;">int</span> id<span style="color: #990000;">)</span> <span style="color: red;">{</span>
          <b><span style="color: blue;">return</span></b> <b><span style="color: black;">View</span></b><span style="color: #990000;">();</span>
      <span style="color: red;">}</span>

      <i><span style="color: #9a1900;">// GET: /Customers/Create</span></i>
      public <span style="color: teal;">ActionResult</span> <b><span style="color: black;">Create</span></b><span style="color: #990000;">()</span> <span style="color: red;">{</span>
          <b><span style="color: blue;">return</span></b> <b><span style="color: black;">View</span></b><span style="color: #990000;">();</span>
      <span style="color: red;">}</span>

      <i><span style="color: #9a1900;">// POST: /Customers/Create</span></i>
      <span style="color: #990000;">[</span>HttpPost<span style="color: #990000;">]</span>
      public <span style="color: teal;">ActionResult</span> <b><span style="color: black;">Create</span></b><span style="color: #990000;">(</span><span style="color: teal;">FormCollection</span> collection<span style="color: #990000;">)</span> <span style="color: red;">{</span>
          try <span style="color: red;">{</span>
              <i><span style="color: #9a1900;">// TODO: Add insert logic here</span></i>
              <b><span style="color: blue;">return</span></b> <b><span style="color: black;">RedirectToAction</span></b><span style="color: #990000;">(</span><span style="color: red;">"Index"</span><span style="color: #990000;">);</span>
          <span style="color: red;">}</span> <b><span style="color: blue;">catch</span></b> <span style="color: red;">{</span>
              <b><span style="color: blue;">return</span></b> <b><span style="color: black;">View</span></b><span style="color: #990000;">();</span>
          <span style="color: red;">}</span>
      <span style="color: red;">}</span>
<span style="color: red;">}</span>

</tt></pre>
<p>As you can see the code generated is simple and clear. That is the benefit of MVC, simpler models, views, and controllers.<br />
The automatic generation of code, that will probably be changed later is commonly known as scaffolding.</p>
<blockquote><p>
<b>Scaffolding</b> is a temporary structure used to support people and material in the construction or repair of buildings and other large structures. --Wikipedia</p></blockquote>
<p>Scaffolding is not meant to be used as is. It is meant to get you started!<br />
In Rails3 you add controllers with a generator. You have a lot more options, below are just a few of them.</p>
<pre><tt>$ rails g controller
Usage<span style="color: #990000;">:</span>
  rails generate controller NAME <span style="color: #990000;">[</span>action action<span style="color: #990000;">]</span> <span style="color: #990000;">[</span>options<span style="color: #990000;">]</span>

Options<span style="color: #990000;">:</span>
  -e<span style="color: #990000;">,</span> <span style="color: #990000;">[</span>--template-engine<span style="color: #990000;">=</span>NAME<span style="color: #990000;">]</span>  <i><span style="color: #9a1900;"># Template engine to be invoked</span></i>
                                <i><span style="color: #9a1900;"># Default: erb</span></i>
  -t<span style="color: #990000;">,</span> <span style="color: #990000;">[</span>--test-framework<span style="color: #990000;">=</span>NAME<span style="color: #990000;">]</span>   <i><span style="color: #9a1900;"># Test framework to be invoked</span></i>
                                <i><span style="color: #9a1900;"># Default: test_unit</span></i>

</tt></pre>
<p>As you can see it is possible to choose what template-engine you want to use, and exactly what actions you want to create, the view templates are created automatically with every action you create.</p>
<pre><tt>$ rails g controller Customer index create
    conflict  app/controllers/customer_controller<span style="color: #990000;">.</span>rb
      create  app/controllers/customer_controller<span style="color: #990000;">.</span>rb
       route  get <span style="color: red;">"customer/create"</span>
       route  get <span style="color: red;">"customer/index"</span>
      invoke  erb
      create    app/views/customer
      create    app/views/customer/index<span style="color: #990000;">.</span>html<span style="color: #990000;">.</span>erb
      create    app/views/customer/create<span style="color: #990000;">.</span>html<span style="color: #990000;">.</span>erb
      invoke  test_unit
    create    test/functional/customer_controller_test<span style="color: #990000;">.</span>rb
      invoke  helper
    create    app/helpers/customer_helper<span style="color: #990000;">.</span>rb
      invoke    test_unit
    create      test/unit/helpers/customer_helper_test<span style="color: #990000;">.</span>rb

If you want to create all the default actions you should instead invoke the scaffold_generator<span style="color: #990000;">.</span>

</tt></pre>
<pre><tt>$ rails g scaffold_controller
Usage<span style="color: #990000;">:</span>
  rails generate scaffold_controller NAME <span style="color: #990000;">[</span>options<span style="color: #990000;">]</span>

</tt></pre>
<p>It will create all the default actions and the views that go with them.</p>
<h4>
<p>The generated code is shown below. Again, you see the similarity between the two solutions. Rails, automatically generates support for both HTML and XML, making it easy to consume the data from other clients.</p>
<pre><tt><b><span style="color: blue;">class</span></b> FishController <span style="color: #990000;">&lt;</span> ApplicationController
  <i><span style="color: #9a1900;"># GET /fish</span></i>
  <i><span style="color: #9a1900;"># GET /fish.xml</span></i>
  <b><span style="color: blue;">def</span></b> index
    <span style="color: #009900;">@fish</span> <span style="color: #990000;">=</span> Fish<span style="color: #990000;">.</span>all

    respond_to <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>format<span style="color: #990000;">|</span>
      format<span style="color: #990000;">.</span>html <i><span style="color: #9a1900;"># index.html.erb</span></i>
      format<span style="color: #990000;">.</span>xml  <span style="color: red;">{</span> render <span style="color: #990000;">:</span>xml <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span> <span style="color: red;">}</span>
    <b><span style="color: blue;">end</span></b>
  <b><span style="color: blue;">end</span></b>

  <i><span style="color: #9a1900;"># GET /fish/1</span></i>
  <i><span style="color: #9a1900;"># GET /fish/1.xml</span></i>
  <b><span style="color: blue;">def</span></b> show
    <span style="color: #009900;">@fish</span> <span style="color: #990000;">=</span> Fish<span style="color: #990000;">.</span>find<span style="color: #990000;">(</span>params<span style="color: #990000;">[:</span>id<span style="color: #990000;">])</span>

    respond_to <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>format<span style="color: #990000;">|</span>
      format<span style="color: #990000;">.</span>html <i><span style="color: #9a1900;"># show.html.erb</span></i>
      format<span style="color: #990000;">.</span>xml  <span style="color: red;">{</span> render <span style="color: #990000;">:</span>xml <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span> <span style="color: red;">}</span>
    <b><span style="color: blue;">end</span></b>
  <b><span style="color: blue;">end</span></b>

  <i><span style="color: #9a1900;"># GET /fish/new</span></i>
  <i><span style="color: #9a1900;"># GET /fish/new.xml</span></i>
  <b><span style="color: blue;">def</span></b> new
    <span style="color: #009900;">@fish</span> <span style="color: #990000;">=</span> Fish<span style="color: #990000;">.</span>new

    respond_to <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>format<span style="color: #990000;">|</span>
      format<span style="color: #990000;">.</span>html <i><span style="color: #9a1900;"># new.html.erb</span></i>
      format<span style="color: #990000;">.</span>xml  <span style="color: red;">{</span> render <span style="color: #990000;">:</span>xml <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span> <span style="color: red;">}</span>
    <b><span style="color: blue;">end</span></b>
  <b><span style="color: blue;">end</span></b>

  <i><span style="color: #9a1900;"># GET /fish/1/edit</span></i>
  <b><span style="color: blue;">def</span></b> edit
    <span style="color: #009900;">@fish</span> <span style="color: #990000;">=</span> Fish<span style="color: #990000;">.</span>find<span style="color: #990000;">(</span>params<span style="color: #990000;">[:</span>id<span style="color: #990000;">])</span>
  <b><span style="color: blue;">end</span></b>

  <i><span style="color: #9a1900;"># POST /fish</span></i>
  <i><span style="color: #9a1900;"># POST /fish.xml</span></i>
  <b><span style="color: blue;">def</span></b> create
    <span style="color: #009900;">@fish</span> <span style="color: #990000;">=</span> Fish<span style="color: #990000;">.</span>new<span style="color: #990000;">(</span>params<span style="color: #990000;">[:</span>fish<span style="color: #990000;">])</span>

    respond_to <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>format<span style="color: #990000;">|</span>
      <b><span style="color: blue;">if</span></b> <span style="color: #009900;">@fish</span><span style="color: #990000;">.</span>save
        format<span style="color: #990000;">.</span>html <span style="color: red;">{</span> redirect_to<span style="color: #990000;">(</span><span style="color: #009900;">@fish</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span>notice <span style="color: #990000;">=&gt;</span> <span style="color: red;">'Fish was successfully created.'</span><span style="color: #990000;">)</span> <span style="color: red;">}</span>
        format<span style="color: #990000;">.</span>xml  <span style="color: red;">{</span> render <span style="color: #990000;">:</span>xml <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span>status <span style="color: #990000;">=&gt;</span> <span style="color: #990000;">:</span>created<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>location <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span> <span style="color: red;">}</span>
      <b><span style="color: blue;">else</span></b>
        format<span style="color: #990000;">.</span>html <span style="color: red;">{</span> render <span style="color: #990000;">:</span>action <span style="color: #990000;">=&gt;</span> <span style="color: red;">"new"</span> <span style="color: red;">}</span>
        format<span style="color: #990000;">.</span>xml  <span style="color: red;">{</span> render <span style="color: #990000;">:</span>xml <span style="color: #990000;">=&gt;</span> <span style="color: #009900;">@fish</span><span style="color: #990000;">.</span>errors<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>status <span style="color: #990000;">=&gt;</span> <span style="color: #990000;">:</span>unprocessable_entity <span style="color: red;">}</span>
      <b><span style="color: blue;">end</span></b>
    <b><span style="color: blue;">end</span></b>
  <b><span style="color: blue;">end</span></b>
<span style="color: #990000;">...</span>
<b><span style="color: blue;">end</span></b>

</tt></pre>
<p>Filters</h4>
<p>In Rails it is easy to create filters, that can be applied to specific actions.</p>
<pre><tt><b><span style="color: blue;">class</span></b> ItemsController <span style="color: #990000;">&lt;</span> ApplicationController
  before_filter <span style="color: #990000;">:</span>require_user_admin<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>only <span style="color: #990000;">=&gt;</span> <span style="color: #990000;">[</span> <span style="color: #990000;">:</span>destroy<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>update <span style="color: #990000;">]</span>
  before_filter <span style="color: #990000;">:</span>require_user<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>only <span style="color: #990000;">=&gt;</span> <span style="color: #990000;">[</span> <span style="color: #990000;">:</span>new<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>create<span style="color: #990000;">]</span>
<b><span style="color: blue;">end</span></b>

</tt></pre>
<p>In ASP.NET MVC, the same thing can be accomplished by overriding <b>OnActionExecuting</b>, in the controller.</p>
<pre><tt>override <span style="color: teal;">void</span> <b><span style="color: black;">OnActionExecuting</span></b><span style="color: #990000;">(</span><span style="color: teal;">ActionExecutingContext</span> filterContext<span style="color: #990000;">)</span>
<span style="color: red;">{</span>
    <span style="color: teal;">var</span> action <span style="color: #990000;">=</span> filterContext<span style="color: #990000;">.</span>ActionDescriptor<span style="color: #990000;">.</span>ActionName<span style="color: #990000;">;</span>
    <b><span style="color: blue;">if</span></b> <span style="color: #990000;">(</span><span style="color: teal;">new</span> List<span style="color: #990000;">&lt;</span>string<span style="color: #990000;">&gt;</span><span style="color: red;">{</span><span style="color: red;">"Delete"</span><span style="color: #990000;">,</span> <span style="color: red;">"Edit"</span><span style="color: red;">}</span><span style="color: #990000;">.</span><b><span style="color: black;">Contains</span></b><span style="color: #990000;">(</span>action<span style="color: #990000;">))</span> <span style="color: red;">{</span>
        <b><span style="color: black;">RequireUserAdmin</span></b><span style="color: #990000;">();</span>
    <span style="color: red;">}</span>
    <b><span style="color: blue;">if</span></b> <span style="color: #990000;">(</span><span style="color: red;">"Create"</span><span style="color: #990000;">.</span><b><span style="color: black;">Equals</span></b><span style="color: #990000;">(</span>action<span style="color: #990000;">))</span> <span style="color: red;">{</span>
        <b><span style="color: black;">RequireUserAdmin</span></b><span style="color: #990000;">();</span>
    <span style="color: red;">}</span>
<span style="color: red;">}</span>

</tt></pre>
<p>The functionality may also be extracted into an ActionFilter, which can be applied to the controller or an individual method via an Attribute.</p>
<pre><tt><span style="color: #990000;">[</span><b><span style="color: black;">RequireUserAdmin</span></b><span style="color: #990000;">(</span><span style="color: red;">"Delete"</span><span style="color: #990000;">,</span> <span style="color: red;">"Edit"</span><span style="color: #990000;">)]</span>
<span style="color: #990000;">[</span><b><span style="color: black;">RequireUser</span></b><span style="color: #990000;">(</span><span style="color: red;">"Create"</span><span style="color: #990000;">)]</span>
public <span style="color: teal;">class</span> CustomersController <span style="color: #990000;">:</span> Controller

</tt></pre>
<p>This is a pattern that can be seen everywhere, when Rails uses <i>Class Macros</i>, a term from <a href="http://www.amazon.com/gp/product/1934356476?ie=UTF8&amp;tag=thtasta-20">Meta-Programming Ruby</a>, ASP.NET MVC uses attributes. And this is appropriate since it usually constitutes meta-data.</p>
<h3>
<p>Routing</h3>
<p>The Routing in both Rails and ASP.NET MVC is incredibly flexible.</p>
<h4>
<p>Rails Routing</h4>
<p>In Rails it is possible to put constraints right in the routing table on just about anything.</p>
<pre><tt><i><span style="color: #9a1900;"># config/routes.rb</span></i>
Tapir<span style="color: #990000;">::</span>Application<span style="color: #990000;">.</span>routes<span style="color: #990000;">.</span>draw <b><span style="color: blue;">do</span></b> <span style="color: #990000;">|</span>map<span style="color: #990000;">|</span>
  resources <span style="color: #990000;">:</span>animals

  get <span style="color: red;">"customer/index"</span>
  get <span style="color: red;">"customer/create"</span>

  match <span style="color: red;">"/:year(/:month(/:day))"</span> <span style="color: #990000;">=&gt;</span> <span style="color: red;">"info#about"</span><span style="color: #990000;">,</span>
	<span style="color: #990000;">:</span>constraints <span style="color: #990000;">=&gt;</span> <span style="color: red;">{</span> <span style="color: #990000;">:</span>year <span style="color: #990000;">=&gt;</span> <span style="color: #ff6600;">/\d{4}/</span><span style="color: #990000;">,</span>
		<span style="color: #990000;">:</span>month <span style="color: #990000;">=&gt;</span> <span style="color: #ff6600;">/\d{2}/</span><span style="color: #990000;">,</span>
		<span style="color: #990000;">:</span>day <span style="color: #990000;">=&gt;</span> <span style="color: #ff6600;">/\d{2}/</span> <span style="color: red;">}</span>
  match <span style="color: red;">"/secret"</span> <span style="color: #990000;">=&gt;</span> <span style="color: red;">"info#about"</span><span style="color: #990000;">,</span>
	<span style="color: #990000;">:</span>constraints <span style="color: #990000;">=&gt;</span> <span style="color: red;">{</span> <span style="color: #990000;">:</span>user_agent <span style="color: #990000;">=&gt;</span> <span style="color: #ff6600;">/Firefox/</span> <span style="color: red;">}</span>
<b><span style="color: blue;">end</span></b>

</tt></pre>
<p>As you can see Rails comes pre-configures with RESTful routing, and it is indeed the recommended way to set up your routes in Rails. To see what the routing result is is easy:</p>
<pre><tt>$ rake routes
                GET    /animals<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>          {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"index"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
        animals POST   /animals<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>          {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"create"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
     new_animal GET    /animals/new<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>      {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"new"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
                GET    /animals<span style="color: #990000;">/:</span>id<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>      {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"show"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
                PUT    /animals<span style="color: #990000;">/:</span>id<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>      {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"update"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
         animal DELETE /animals<span style="color: #990000;">/:</span>id<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span>      {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"destroy"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
    edit_animal GET    /animals<span style="color: #990000;">/:</span>id/edit<span style="color: #990000;">(.:</span>format<span style="color: #990000;">)</span> {<span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"edit"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"animals"</span>}
 customer_index GET    /customer/index             {<span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"customer"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"index"</span>}
customer_create GET    /customer/create            {<span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"customer"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"create"</span>}
                       <span style="color: #990000;">/:</span>year<span style="color: #990000;">(/:</span>month<span style="color: #990000;">(/:</span>day<span style="color: #990000;">))</span>      {<span style="color: #990000;">:</span><span style="color: #009900;">year</span><span style="color: #990000;">=&gt;/\</span>d{<span style="color: #993399;">4</span>}<span style="color: #990000;">/,</span> <span style="color: #990000;">:</span><span style="color: #009900;">month</span><span style="color: #990000;">=&gt;/\</span>d{<span style="color: #993399;">2</span>}<span style="color: #990000;">/,</span> <span style="color: #990000;">:</span><span style="color: #009900;">day</span><span style="color: #990000;">=&gt;/\</span>d{<span style="color: #993399;">2</span>}<span style="color: #990000;">/,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"info"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"about"</span>}
                       /secret                     {<span style="color: #990000;">:</span><span style="color: #009900;">user_agent</span><span style="color: #990000;">=&gt;</span>/Firefox<span style="color: #990000;">/,</span> <span style="color: #990000;">:</span><span style="color: #009900;">controller</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"info"</span><span style="color: #990000;">,</span> <span style="color: #990000;">:</span><span style="color: #009900;">action</span><span style="color: #990000;">=&gt;</span><span style="color: red;">"about"</span>}

</tt></pre>
<h4>
<p>ASP.NET MVC Routing</h4>
<p>ASP.NET MVC Routing is not quite as easy, but everything is possible. The standard routing with paths and parameters works easily out of the box.</p>
<pre><tt><i><span style="color: #9a1900;">// Global.asax.cs</span></i>
 public <span style="color: teal;">class</span> MvcApplication <span style="color: #990000;">:</span> System<span style="color: #990000;">.</span>Web<span style="color: #990000;">.</span>HttpApplication <span style="color: red;">{</span>
	public <b><span style="color: blue;">static</span></b> <span style="color: #009900;">void</span> <b><span style="color: black;">RegisterRoutes</span></b><span style="color: #990000;">(</span><span style="color: teal;">RouteCollection</span> routes<span style="color: #990000;">)</span> <span style="color: red;">{</span>
		routes<span style="color: #990000;">.</span><b><span style="color: black;">IgnoreRoute</span></b><span style="color: #990000;">(</span><span style="color: red;">"{resource}.axd/{*pathInfo}"</span><span style="color: #990000;">);</span> 

		<i><span style="color: #9a1900;">// Constrained route</span></i>
		routes<span style="color: #990000;">.</span><b><span style="color: black;">MapRoute</span></b><span style="color: #990000;">(</span> <span style="color: red;">"Product"</span><span style="color: #990000;">,</span> <span style="color: red;">"Product/{productId}"</span><span style="color: #990000;">,</span>
			new <span style="color: red;">{</span>controller<span style="color: #990000;">=</span><span style="color: red;">"Product"</span><span style="color: #990000;">,</span> action<span style="color: #990000;">=</span><span style="color: red;">"Details"</span><span style="color: red;">}</span><span style="color: #990000;">,</span>
			new <span style="color: red;">{</span>productId <span style="color: #990000;">=</span> @<span style="color: red;">"</span><span style="color: #cc33cc;">\d</span><span style="color: red;">+"</span> <span style="color: red;">}</span> <span style="color: #990000;">);</span> <i><span style="color: #9a1900;">// Constraint</span></i>

		<i><span style="color: #9a1900;">// Route with custom constraint, defined below	</span></i>
		routes<span style="color: #990000;">.</span><b><span style="color: black;">MapRoute</span></b><span style="color: #990000;">(</span> <span style="color: red;">"Admin"</span><span style="color: #990000;">,</span> <span style="color: red;">"Admin/{action}"</span><span style="color: #990000;">,</span>
			new <span style="color: red;">{</span>controller<span style="color: #990000;">=</span><span style="color: red;">"Admin"</span><span style="color: red;">}</span><span style="color: #990000;">,</span>
			new <span style="color: red;">{</span>isLocal<span style="color: #990000;">=</span><span style="color: teal;">new</span> <b><span style="color: black;">LocalhostConstraint</span></b><span style="color: #990000;">()</span><span style="color: red;">}</span> <span style="color: #990000;">);</span>
	<span style="color: red;">}</span>
<span style="color: #990000;">...</span>
<span style="color: red;">}</span>

public <span style="color: teal;">class</span> LocalhostConstraint <span style="color: #990000;">:</span> IRouteConstraint <span style="color: red;">{</span>
	public <span style="color: teal;">bool</span> <b><span style="color: black;">Match</span></b> <span style="color: #990000;">(</span> <span style="color: teal;">HttpContextBase</span> httpContext<span style="color: #990000;">,</span> <span style="color: teal;">Route</span> route<span style="color: #990000;">,</span>
	 	<span style="color: teal;">string</span> parameterName<span style="color: #990000;">,</span> <span style="color: teal;">RouteValueDictionary</span> values<span style="color: #990000;">,</span>
	 	<span style="color: teal;">RouteDirection</span> routeDirection <span style="color: #990000;">)</span>
	<span style="color: red;">{</span>
		<b><span style="color: blue;">return</span></b> httpContext<span style="color: #990000;">.</span>Request<span style="color: #990000;">.</span>IsLocal<span style="color: #990000;">;</span>
	<span style="color: red;">}</span>
<span style="color: red;">}</span> 

</tt></pre>
<p>As you can see, you have the same power in the ASP.NET MVC Routing, but it requires you to add constraints separately.</p>
<h3>
<p>Views</h3>
<p>In both Rails and ASP.NET MVC, the views are automatically matched in the directory with the same name as the controller, and the file name is named by the action.</p>
<pre><tt>app/controllers/customer_controller<span style="color: #990000;">.</span>rb
app/views/customer
app/views/customer/create<span style="color: #990000;">.</span>html<span style="color: #990000;">.</span>erb
app/views/customer/index<span style="color: #990000;">.</span>html<span style="color: #990000;">.</span>erb

</tt></pre>
<p>In Visual Studio it is possible to scaffold a view based on a model, just as it is in Rails. Rails is, of course, more flexible, allowing me to scaffold all or some actions at once. In Visual Studio, it is only possible to do one at a time.</p>
<h4>
<p>Partials</h4>
<p>In both Rails and ASP.NET MVC, a partial is a file that contains a part of a HTML file. The file is written in ASP in ASP.NET MVC and in Rails it is possible to pick the templating language of your choice. Default is ERB, but HAML is also a popular choice.</p>
<pre><tt><span style="color: #990000;">&lt;!--</span> Rails <span style="color: #990000;">--&gt;</span>
<span style="color: #990000;">&lt;%=</span> render <span style="color: red;">'form'</span> <span style="color: #990000;">%&gt;</span>

</tt></pre>
<pre><tt><span style="color: #990000;">&lt;!--</span> ASP<span style="color: #990000;">.</span>NET MVC <span style="color: #990000;">--&gt;</span>
<span style="color: #990000;">&lt;%</span> Html<span style="color: #990000;">.</span><b><span style="color: black;">RenderPartial</span></b><span style="color: #990000;">(</span><span style="color: red;">"Form"</span><span style="color: #990000;">,</span> Model<span style="color: #990000;">);%&gt;</span>

</tt></pre>
<p>In ASP.NET MVC version 2, the preferred way is actually to use two alternate methods to generate the code. They have the advantage that they are type-safe (if that gives you pleasure) and they keep track of nesting.</p>
<pre><tt><span style="color: #990000;">&lt;%=</span> <b><span style="color: black;">DisplayFor</span></b><span style="color: #990000;">(</span><span style="color: red;">"Address"</span><span style="color: #990000;">,</span> m <span style="color: #990000;">=&gt;</span> m<span style="color: #990000;">.</span>Address <span style="color: #990000;">)</span> <span style="color: #990000;">%&gt;</span>

<span style="color: #990000;">&lt;%=</span> <b><span style="color: black;">EditorFor</span></b><span style="color: #990000;">(</span><span style="color: red;">"Address"</span><span style="color: #990000;">,</span> m <span style="color: #990000;">=&gt;</span> m<span style="color: #990000;">.</span>Address <span style="color: #990000;">)</span> <span style="color: #990000;">%&gt;</span>

</tt></pre>
<p>(For Rubyist, the <b>m =&gt; m.Address</b> is not a hash key-value expression, but an anonymous function.)</p>
<h4>
<p>Helpers</h4>
<p>A helper is similar to a partial, but it is not a template, it's a method that generates code.<br />
In Rails you create a helper by creating a simple method.</p>
<pre><tt><b><span style="color: blue;">module</span></b> ApplicationHelper
   <b><span style="color: blue;">def</span></b> label target<span style="color: #990000;">,</span> text
     <span style="color: red;">"&lt;label for='#{target}'&gt;#{text}&lt;/label&gt;"</span>
   <b><span style="color: blue;">end</span></b>
 <b><span style="color: blue;">end</span></b>

</tt></pre>
<p>And so you can in ASP.NET MVC.</p>
<pre><tt>public <span style="color: teal;">class</span> LabelHelper <span style="color: red;">{</span>
    public <b><span style="color: blue;">static</span></b> <span style="color: teal;">string</span> <b><span style="color: black;">Label</span></b><span style="color: #990000;">(</span><span style="color: teal;">string</span> target<span style="color: #990000;">,</span> <span style="color: teal;">string</span> text<span style="color: #990000;">)</span> <span style="color: red;">{</span>
        <b><span style="color: blue;">return</span></b> String<span style="color: #990000;">.</span><b><span style="color: black;">Format</span></b><span style="color: #990000;">(</span><span style="color: red;">"&lt;label for='{0}'&gt;{1}&lt;/label&gt;"</span><span style="color: #990000;">,</span> target<span style="color: #990000;">,</span> text<span style="color: #990000;">);</span>
    <span style="color: red;">}</span>
<span style="color: red;">}</span>

</tt></pre>
<h3>
<p>Conclusion</h3>
<p>All in all, ASP.NET MVC with LINQ-to-SQL, is not a bad experience. Eric Meijer and friends have managed to overcome a lot of the hurdles of static typing. They also, let go of it in cases, where dynamic typing is obviously better, such as in the routing and when populating objects from forms. The lambda expressions and the type inference also removes a lot of the boilerplate common in statically typed languages.<br />
Rails is of course a lot more flexible, and faster to work with. The scaffolding is faster and more flexible, the migrations are, as I said before, invaluable when it comes to move between versions.<br />
But, if I'm thrown out of the dynamic Garden of Eden, I'd rather pick my static apples in Redmond, than in California.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/04/23/asp-net-mvc-vs-rails3/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Static Typing is the Root of All Evil</title>
		<link>http://blog.jayway.com/2010/04/14/static-typing-is-the-root-of-all-evil/</link>
		<comments>http://blog.jayway.com/2010/04/14/static-typing-is-the-root-of-all-evil/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 11:44:49 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5346</guid>
		<description><![CDATA[I can't take credit for this statement, since I am only drawing the logical conclusion from a statement said by a man far smarter than I. Donald Knuth wrote in his famous paper Structured Programming with go to Statements (PDF): We should forget about small efficiencies, say about 97% of the time: pre-mature optimization is [...]]]></description>
			<content:encoded><![CDATA[<p>I can't take credit for this statement, since I am only drawing the logical conclusion from a statement said by a man far smarter than I.</p>
<p>Donald Knuth wrote in his famous paper <a href="http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf">Structured Programming with go to Statements (PDF)</a>:</p>
<blockquote><p>We should forget about small efficiencies, say about 97% of the time: pre-mature optimization is the root of all evil</p>
</blockquote>
<p>Since compilation is premature optimization, it is therefore, the root of all evil. Simple!</p>
<h2>Static typing, weak typing, dynamic typing, strong typing, WTF?</h2>
<p>There are a number of misconceptions concerning typing.</p>
<p>The meaning of <em>weak</em> and <em>strong</em> typing, <a href="http://en.wikipedia.org/wiki/Strong_typing">is not clearly defined</a> but is mostly interpreted like this.</p>
<blockquote><p>In a weakly typed language, such as C, the types of the variables can be automatically converted from one representation to another, while in a strongly typed language an explict conversion is required.</p>
</blockquote>
<p>This is not what I am writing about here. I am referring to <em>static</em> vs. <em>dynamic</em> typing and the definition I am using is:</p>
<ul>
<li><em>Static typing</em>, the type checking is performed at compile-time.</li>
<li><em>Dynamic typing</em>, the type ckecking is performed at run-time.</li>
</ul>
<h2>Development Mode</h2>
<p>Every time I build my code, my entire code base is type-checked. <em>EVERY TIME!</em> When I am working in development mode, I only care about the method and class that I am currently working on. As long as my tests pass I am happy, type-safe or not.</p>
<p>In development mode it is actually more accurate to call static typing pre-mature <em>de-optimization</em> than pre-mature optimization.</p>
<h2>Production Mode</h2>
<p>The <em>only</em> reason, to <em>ever</em> use static typing, is because the code may be optimized better, to give better performance.</p>
<p>So, the strategy should be:</p>
<ul>
<li>Use a dynamically typed language during development. It gives you faster feedback, turn-around time, and development speed.</li>
<li>If you ever get lucky enough to have performance problems, which cannot be solved with caching or algorithmic optimization, rewrite the problematic code in a statically typed language.</li>
</ul>
<p>This is what Twitter did, and it has worked well for them.</p>
<h2>Other Misconceptions</h2>
<p>Proponents of Java often have the misconception that dynamically typed languages are unsafe, yet they use frameworks, like Spring, and Qi4J, that are littered with reflective code, and without the so-called safety net that static typing is believed to give. The reason these frameworks are popular is because they allow us to be more productive. The reason they are more productive is because they use dynamic programming techniques.</p>
<p>Anyone, who has worked seriously with a modern dynamically typed language like Ruby or Smalltalk, know that they are more productive. Working with waterfall languages after working with agile languages is just painful. (Thanks to Andreas Ronge for coining the term Waterfall Language.)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/04/14/static-typing-is-the-root-of-all-evil/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Maven, the new Elephant on the Block</title>
		<link>http://blog.jayway.com/2010/01/23/maven-the-new-elephant-on-the-block/</link>
		<comments>http://blog.jayway.com/2010/01/23/maven-the-new-elephant-on-the-block/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 15:39:11 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[build systems]]></category>
		<category><![CDATA[buildr]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven2]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4500</guid>
		<description><![CDATA[Some of you may remember the article, by Bruce Tate, Don't Make Me Eat the Elephant Again. It was an article about EJB, and Bruce was begging Sun not to make the same mistakes with EJB3 as they had done with EJB, and EJB2. They didn't, Spring came along as better alternative and forced EJB3 [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you may remember the article, by Bruce Tate, <a href="http://today.java.net/pub/a/today/2004/06/15/ejb3.html">Don't Make Me Eat the Elephant Again</a>.<br />
It was an article about EJB, and Bruce was begging Sun not to make the same mistakes with EJB3 as they had done with EJB, and EJB2. They didn't, Spring came along as better alternative and forced EJB3 to become slimmer and better. If not for Spring, EJB3 would probably look very different from what it looks like today.<br />
Well, guess what, there is a new elephant on the block and its name Maven2.</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/_ELioussa2vo/S1sVp6WDq8I/AAAAAAAAAbU/Vsll0_5GjII/s1600-h/maven2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" height="289" src="http://4.bp.blogspot.com/_ELioussa2vo/S1sVp6WDq8I/AAAAAAAAAbU/Vsll0_5GjII/s320/maven2.png" width="320" /></a></div>
<p>
Just like EJB2, Maven2 was born out of something so unbearable that anything else was bliss. <a href="http://commons.apache.org/jelly/">Jelly</a> anyone!</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/_ELioussa2vo/S1sWI6lm3KI/AAAAAAAAAbc/A6a_3zHxpsc/s1600-h/Maven.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" height="246" src="http://3.bp.blogspot.com/_ELioussa2vo/S1sWI6lm3KI/AAAAAAAAAbc/A6a_3zHxpsc/s320/Maven.png" width="320" /></a></div>
<p>
But just as EJB was fundamentally flawed, so is Maven2. Build systems, even advanced ones like Maven is fundamentally about two things.</p>
<ol>
<li>Check if something that something else depends on has changed</li>
<li>If so, do something.</li>
</ol>
<p>That's it, that is what is important.<br />
The checking may contain various sophisticated methods for detecting if files, subsets of files, all files, web pages, twitter feeds, etc, has changed, but that is really it.<br />
And the doing can be anything, Copy files, commit files, build websites, run tests, generate code, launch missiles, whatever!<br />
But the key to doing this efficiently is a programming language with easy access to system commands and the ability to create simple abstractions, with methods, variables, and objects. The language should also, preferably, be one without a lot of ceremony, like Ruby, Javascript or Python.<br />
There are already build systems like this out there, <a href="http://buildr.apache.org/">Buildr</a>, <a href="http://www.scons.org/">SCons</a>, and <a href="http://rake.rubyforge.org/">Rake</a> come to mind, but they do not have the momentum of Maven, so a merger between Maven and Buildr would be wonderful.</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/_ELioussa2vo/S1sWTI-SneI/AAAAAAAAAbk/Z0AI_sUPXMs/s1600-h/Maven3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" rel="lightbox"><img border="0" src="http://4.bp.blogspot.com/_ELioussa2vo/S1sWTI-SneI/AAAAAAAAAbk/Z0AI_sUPXMs/s320/Maven3.png" /></a></div>
<p>So, Jason, hear my plea, Don't make me eat the elephant again!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/23/maven-the-new-elephant-on-the-block/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

