<?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>g:h &#187; bestpractices</title>
	<atom:link href="http://hreidarsson.com/tag/bestpractices/feed/" rel="self" type="application/rss+xml" />
	<link>http://hreidarsson.com</link>
	<description></description>
	<lastBuildDate>Mon, 09 Aug 2010 07:47:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making jQuery code readable</title>
		<link>http://hreidarsson.com/2009/06/making-jquery-code-readable/</link>
		<comments>http://hreidarsson.com/2009/06/making-jquery-code-readable/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 18:26:28 +0000</pubDate>
		<dc:creator>gummi</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[bestpractices]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://hreidarsson.com/?p=4</guid>
		<description><![CDATA[As a long time jQuery user I use many of it&#8217;s features, and much more than just the most basic ones. I love it, especially when I find new tricks to simplify or shorten my code. But my code is most often only read by me.
Recently I&#8217;ve been working with developers new to jQuery, and [...]]]></description>
			<content:encoded><![CDATA[<p>As a long time jQuery user I use many of it&#8217;s features, and much more than just the most basic ones. I love it, especially when I find new tricks to simplify or shorten my code. But my code is most often only read by me.</p>
<p>Recently I&#8217;ve been working with developers new to jQuery, and it has come clear to me that my code – as simple and smooth I find it – is often hard to understand. I&#8217;ve gotten the same looks I would get presenting a Perl-golf program. Ah well, maybe not quite. But you get my point.</p>
<p>I have set to make my code more readable, without making it ugly. Below are a few best-practices I&#8217;ve made for myself.</p>
<h3>Break down long chains</h3>
<p>Here is a typical bit of code I might write:</p>
<pre>$('table')
    .find('.activate').click(function () { activate(this.id) }).end()
    .find('.deactivate').click(function () { deactivate(this.id) }).end()
    .find('a').show();</pre>
<p>Long chains of code tend to use jQuery commands like <code>end</code> and <code>andSelf</code> to modify the wrapped set. Those methods are good to have when you know them, but their semantics can&#8217;t be easily deducted from their names. A long chain performing many tasks can also be harder to walk through than smaller independent bits. So it is wise to break chains down:</p>
<pre>var table = $('table');
$(table).find('.activate').click(function () { activate(this.id) });
$(table).find('.deactivate').click(function () { deactivate(this.id) });
$(table).find('a').show();</pre>
<p>This version – without doubt – lacks the beauty of the chain above, but is more readable, especially to someone unfamiliar.</p>
<h3>Bind events using explicitly using <code>bind</code></h3>
<p>Having the same method to bind and fire an event is bound to confuse – hell, I get confused again and again:</p>
<pre>$('select')
    .change(function () { ... })
    .change();</pre>
<p>Explicitly calling the <code>bind</code> command to bind to the change event is much easier to grasp:</p>
<pre>$('select')
    .bind('change', function () { ... })
    .change();</pre>
<p>This holds for all events who have shortcuts for binding, e.g <code>blur</code>, <code>click</code> and <code>mousedown</code>.</p>
<h3>Clarify what <code>this</code> means</h3>
<p>The <code>this</code> keyword is probably one of the most infamous elements in the JavaScript language. And even when you&#8217;ve gotten fluent in using it, you never can be quite sure it&#8217;s used the same way in the same contexts.</p>
<p>This is a typical code snippet:</p>
<pre>$('a')
    .addClass('logged')
    .bind('click', function() {
        logUrl(this.href);
    });</pre>
<p>What does <code>this</code> point to? This example is that simple, and the <code>href</code> attribute that specific, that it&#8217;s maybe not hard to guess. But in more complex situations it&#8217;s hard to find out.</p>
<p>Copying <code>this</code> to a clearly named variable does a great deal in clarifying what&#8217;s going on:</p>
<pre>$('a')
    .addClass('logged')
    .bind('click', function() {
        var clickedLink = this;
        logUrl(clickedLink.href);
    });</pre>
]]></content:encoded>
			<wfw:commentRss>http://hreidarsson.com/2009/06/making-jquery-code-readable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
