<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>AaronHardy.com &#187; architecture</title>
	<atom:link href="http://aaronhardy.com/tag/architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronhardy.com</link>
	<description>For all your Aaron Hardy needs.</description>
	<lastBuildDate>Fri, 20 Apr 2012 14:53:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript for the Flex Dev Slides</title>
		<link>http://aaronhardy.com/flex/javascript-for-flex-devs-slides/</link>
		<comments>http://aaronhardy.com/flex/javascript-for-flex-devs-slides/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 02:02:04 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[backbonejs]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[requirejs]]></category>
		<category><![CDATA[underscorejs]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=1351</guid>
		<description><![CDATA[Presented at 360&#124;Flex 2012 in Denver, Colorado. View slides as PDF]]></description>
			<content:encoded><![CDATA[<p>Presented at <a href="http://www.360flex.com/" target="_blank">360|Flex</a> 2012 in Denver, Colorado.</p>
<div style="width:100%" id="__ss_12565929"><object id="__sse12565929" width="100%" height="480"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=javascriptforflexdevs-120416205021-phpapp02&#038;stripped_title=javascript-for-flex-devs&#038;userName=Aaronius" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><param name="wmode" value="transparent"/><embed name="__sse12565929" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=javascriptforflexdevs-120416205021-phpapp02&#038;stripped_title=javascript-for-flex-devs&#038;userName=Aaronius" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="100%" height="480"></embed></object></div>
<p><a href='http://aaronhardy.com/wp-content/uploads/2012/04/JavaScriptForFlexDevs.pdf'>View slides as PDF</a></p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/javascript-for-flex-devs-slides/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Architecture: Underscore.js</title>
		<link>http://aaronhardy.com/javascript/javascript-architecture-underscore-js/</link>
		<comments>http://aaronhardy.com/javascript/javascript-architecture-underscore-js/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 06:18:18 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[backbone.js]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[underscore.js]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=1103</guid>
		<description><![CDATA[Underscore.js, like jQuery, is a toolbox of utilities. Check out the website for a list of functionality it provides, but I&#8217;ll split it into two parts: Array/Object/Function manipulation As we create software is seems like we come in contact with the same patterns over and over. Usually, we end up re-writing them over and over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://documentcloud.github.com/underscore/" target="_blank">Underscore.js</a>, like <a href="/javascript/javascript-architecture-jquery/" target="_blank">jQuery</a>, is a toolbox of utilities.  Check out the website for a list of functionality it provides, but I&#8217;ll split it into two parts:</p>
<h3>Array/Object/Function manipulation</h3>
<p>As we create software is seems like we come in contact with the same patterns over and over.  Usually, we end up re-writing them over and over as well.  Take an array of user objects, each with a <code>username</code> property.  We need an array of all the usernames from all the objects.  So, like many times before, we create a new array to populate, create a for loop, snag the object at the current index, grab the username and push it into the array.</p>
<p>To me, that&#8217;s boring.  It&#8217;s mundane.  Underscore makes it fun again.  With Underscore, we just use <a href="http://documentcloud.github.com/underscore/#pluck" target="_blank">pluck()</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> usernames <span style="color: #339933;">=</span> _.<span style="color: #660066;">pluck</span><span style="color: #009900;">&#40;</span>users<span style="color: #339933;">,</span> <span style="color: #3366CC;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ah&#8230;concise, fast, and boilerplate is gone.  Want to find all objects within an array that pass a specific test?  Use the <code><a href="http://documentcloud.github.com/underscore/#filter" target="_blank">filter()</a></code> function.  Just want a reference to the first one that passes the test? Use the <code><a href="http://documentcloud.github.com/underscore/#find" target="_blank">find()</a></code> function.  Want to retrieve the union of two arrays, that is, retrieve a single array of all unique objects contained within multiple other arrays?  Try the <code><a href="http://documentcloud.github.com/underscore/#union" target="_blank">union()</a></code> function.  Merge properties of multiple objects into a single object?  Use <code><a href="http://documentcloud.github.com/underscore/#extend" target="_blank">extend()</a></code>.</p>
<p>Once you grasp the power of Underscore you&#8217;ll find yourself being more productive with less code while having more fun.  Some have called it the bowtie for jQuery&#8217;s tux.  I concur.<span id="more-1103"></span></p>
<h3>Templating</h3>
<p>The <a href="http://documentcloud.github.com/underscore/#template" target="_blank">template()</a> function is only one of the many listed for Underscore, but its purpose is <em>very</em> important.</p>
<p>Let&#8217;s say you&#8217;re building an app to manage your todo items.  When the user creates a todo, it&#8217;s your job to create a todo row for the view.  And maybe you want to allow them to select the row using a checkbox, delete the row, or edit the row in-line.  Without using a templating engine, you might end up with something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> todoTemplate <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span>
	<span style="color: #3366CC;">'&lt;div class=&quot;todo '</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>done <span style="color: #339933;">?</span> <span style="color: #3366CC;">'done'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span>
		<span style="color: #3366CC;">'&lt;div class=&quot;display&quot;&gt;'</span> <span style="color: #339933;">+</span>
			<span style="color: #3366CC;">'&lt;input class=&quot;check&quot; type=&quot;checkbox&quot; '</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>done <span style="color: #339933;">?</span> <span style="color: #3366CC;">'checked=&quot;checked&quot;'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' /&gt;'</span> <span style="color: #339933;">+</span>
			<span style="color: #3366CC;">'&lt;div class=&quot;todo-text&quot;&gt;&lt;/div&gt;'</span> <span style="color: #339933;">+</span>
			<span style="color: #3366CC;">'&lt;span class=&quot;todo-destroy&quot;&gt;&lt;/span&gt;'</span> <span style="color: #339933;">+</span>
		<span style="color: #3366CC;">'&lt;/div&gt;'</span> <span style="color: #339933;">+</span>
		<span style="color: #3366CC;">'&lt;div class=&quot;edit&quot;&gt;'</span> <span style="color: #339933;">+</span>
			<span style="color: #3366CC;">'&lt;input class=&quot;todo-input&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;'</span> <span style="color: #339933;">+</span>
		<span style="color: #3366CC;">'&lt;/div&gt;'</span> <span style="color: #339933;">+</span>
	<span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is troublesome.  First, your html is likely buried somewhere inside your JavaScript code. This makes re-skinning an app much harder since you now have to track down all your html strings scattered throughout your logic.  Second, concatenation takes time, is risky, and falls under the categories of mundane and frustrating. You have to deal with escaping and indentation. Your IDE probably isn&#8217;t going to color-code, code-hint, or auto-indent as well as it could if it were plain html. It&#8217;s easy to make a mistake and, since JavaScript is loosely typed, you might not find these mistakes until runtime. Also, if you dish your code off to HTML/CSS gurus, they now have to deal with these problems too.
</ul>
<p>Templating engines provide a better way of managing the situation.  What if we added this at the bottom of our <code>body</code> tag instead:</p>

<div class="wp_syntax"><div class="code"><pre class="html4stric" style="font-family:monospace;">&lt;script type=&quot;text/template&quot; id=&quot;item-template&quot;&gt;
	&lt;div class=&quot;todo &lt;%= done ? 'done' : '' %&gt;&quot;&gt;
		&lt;div class=&quot;display&quot;&gt;
			&lt;input class=&quot;check&quot; type=&quot;checkbox&quot; &lt;%= done ? 'checked=&quot;checked&quot;' : '' %&gt; /&gt;
			&lt;div class=&quot;todo-text&quot;&gt;&lt;/div&gt;
			&lt;span class=&quot;todo-destroy&quot;&gt;&lt;/span&gt;
		&lt;/div&gt;
		&lt;div class=&quot;edit&quot;&gt;
			&lt;input class=&quot;todo-input&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/script&gt;</pre></div></div>

<p>There are a few things to note here.  </p>
<ul>
<li>The code is plain HTML except for those things that can be dynamic which are inline (<code><%=...%></code>).</li>
<li>The dynamic portions reference a variable called <code>done</code>.  We&#8217;ll talk about that in a minute.</li>
<li>The code is wrapped in a script tag with a type of <code>text/template</code> meaning it won&#8217;t be interpreted as JavaScript (the default script type) by the browser.  If it were, the browser would throw errors.</li>
<li>The script tag has an ID which allows us to later access its content.</li>
</ul>
<p>At this point, the code inside the <code>script</code> tag is just innate text.  The browser won&#8217;t try to execute it or anything of that nature.  That&#8217;s where Underscore&#8217;s templating engine comes in.  In our app JavaScript code, we can then take action like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> template <span style="color: #339933;">=</span> _.<span style="color: #660066;">template</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#item-template'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>$('#item-template')</code> code uses jQuery to reference the <code>script</code> element. Calling <code>html()</code> on the element then returns the inner html of the element&#8211;in this case the html that will represent a todo item.  Here comes the underscore templating engine: <code>_.template()</code>.  At this point a <em>compiled template</em> has been built.  This allows us to now pass a <em>context object</em> into the compiled template to produce html we can then use for our todo item.  This might look like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> todoHTML <span style="color: #339933;">=</span> template<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>done<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The portions of the template that depend on the <code>done</code> variable will be evaluated using the <code>done</code> property on the context object.  The resulting <code>todoHTML</code> will be a string of html that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;todo done&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;check&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">checked</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checked&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;todo-text&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;todo-destroy&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edit&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;todo-input&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>You can then use jQuery to create an element out of the html.  It&#8217;s as easy as</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>todoHTML<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I should mention that a portion of this code is pulled from <a href="http://documentcloud.github.com/backbone/" target="_blank">Backbone.js</a>&#8216;s <a href="http://documentcloud.github.com/backbone/examples/todos/index.html" target="_blank">Todos example</a>.</p>
<h3>Template management</h3>
<p>You might rightfully be wondering how this is scalable if you start to have hundreds of templates in your html file or how you might be able to load them asynchronously only when needed.  If you&#8217;ve asked yourself these things, congratulations!  These are questions architects should be asking.  We&#8217;ll talk about how to manage this in a later post.</p>
<h3>Templating syntax</h3>
<p>Underscore by default uses ERB-style template syntax (<code><%=...%></code>) which to me feels nasty and isn&#8217;t fun to type. You can change that to some other style like <code>{{...}}</code> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_.<span style="color: #660066;">templateSettings</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  interpolate <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/\{\{(.+?)\}\}/g</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Much better.</p>
<h3>Other templating engines</h3>
<p>The concept of a templating engine is not unique to Underscore or even JavaScript.  There are many others in many languages.  I feel the most notable ones in JavaScript outside of Underscore are <a href="http://mustache.github.com/" target="_blank">Mustache.js</a> and <a href="http://www.handlebarsjs.com/" target="_blank">Handlebars.js</a>.  I use Underscore because it&#8217;s a top-notch templating engine, includes other Underscore functionality I use anyway, and it&#8217;s a dependency of <a href="http://documentcloud.github.com/backbone/" target="_blank">Backbone.js</a> which we&#8217;ll be discussing in a later post.</p>
<h3>Where to use templates</h3>
<p>After reading this, you may think templates are only appropriate for elements whose count is undetermined until runtime like our todo items. If so, may I suggest considering that everything should be a template?</p>
<p>What if you took everything in your body tag, broke it up into reasonably small templates, and left your body tag with nothing in it except for your templates?  What if each DOM element and its contents produced from each small template were managed by a single JavaScript object?  What kind of freedom would that bring your code?  Could you move these components around more freely?  Ship them over to be used in a different app?  Would they be less coupled from one another and provide stronger APIs? Could you more appropriately add, remove, re-arrange, and manipulate these components rather than having a big fat HTML file written up front?  Could you provide your users an all-around more dynamic experience?  I hope you catch the vision.  In upcoming posts we&#8217;ll see how this takes shape.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/javascript/javascript-architecture-underscore-js/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[JavaScript Architecture]]></series:name>
	</item>
		<item>
		<title>JavaScript Architecture: Organization and Quality</title>
		<link>http://aaronhardy.com/javascript/javascript-architecture-organization-and-quality/</link>
		<comments>http://aaronhardy.com/javascript/javascript-architecture-organization-and-quality/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 05:14:46 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[cohesion]]></category>
		<category><![CDATA[conventions]]></category>
		<category><![CDATA[decoupling]]></category>
		<category><![CDATA[jshint]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jstestdriver]]></category>
		<category><![CDATA[qunit]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[strict mode]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=1040</guid>
		<description><![CDATA[Never build large apps Justin Meyer, the main guy behind JavaScriptMVC, said something I feel is a very simple principle every architect should ingrain into their brain: The secret to building large apps is NEVER build large apps. Break up your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application. [...]]]></description>
			<content:encoded><![CDATA[<h3>Never build large apps</h3>
<p>Justin Meyer, the main guy behind <a href="http://javascriptmvc.com/" target="_blank">JavaScriptMVC</a>, <a href="http://jupiterjs.com/news/organizing-a-jquery-application" target="_blank">said something</a> I feel is a very simple principle every architect should ingrain into their brain:</p>
<blockquote><p>
The secret to building large apps is NEVER build large apps. Break up your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application.
</p></blockquote>
<p>I don&#8217;t think any other principle will carry more weight in architecture&#8211;especially with JavaScript.  You may be thinking, &#8220;Oh, my application isn&#8217;t big enough to follow this principle.&#8221;  Re-think this.  Every application I&#8217;ve architected&#8211;even the smallest of the small&#8211;have benefited from this principle.  The pieces of your application <em>must</em> be <a href="http://en.wikipedia.org/wiki/Decoupling#Software_Development" target="_blank">decoupled</a> and <a href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)" target="_blank">cohesive</a> as much as humanly possible to withstand the test of time.  Each piece should be as black-box as possible&#8211;pass information over the wall and the next component does its job.  And I don&#8217;t just mean different views of your app; I also mean individual, small components of your views, models, and everything you build.  This presentation on <a href="http://www.slideshare.net/nzakas/scalable-javascript-application-architecture" target="_blank">Scalable JavaScript Application Architecture</a> does a great job of explaining these principles in more detail.<span id="more-1040"></span></p>
<p>When you think of JavaScript apps you may think of a few gigantic, spaghetti JavaScript files and a few beefy HTML files and a mess throughout.  You might even think of fat strings of html text hand-concanated, escaped, and declared right within your JavaScript.  <a href="http://jstorimer.com/2012/01/09/the-hungry-programmer.html" target="_blank">Escape this mindset.</a>  That, indeed, is a rightful stigma of JavaScript apps because so many exist that fall into this category.  We&#8217;re not going to be following that trend.  We want to favor many small classes, files, and HTML templates over a few large ones.  Small classes lead to small scope.  Small scope leads to better maintainability and easier debugging because there is less your mind needs to comprehend.  It also encourages strong, well-planned APIs and low coupling.</p>
<p>You may be thinking, &#8220;But JavaScript doesn&#8217;t have namespaces and everything&#8217;s asynchronously loaded and depends on everything else!  And what about all the requests to the server? And how do I get all those HTML templates into my app?  This is going to be a nightmare!&#8221;  Don&#8217;t worry about it.  We&#8217;ll get there.  Don&#8217;t fall into the trap of making excuses for poor design just because you&#8217;re in JavaScript.</p>
<h3>Admit what you don&#8217;t know</h3>
<p>The only thing that&#8217;s guaranteed not to change is change.  Therefore, never act like you know how your app will grow and change.  Nicholas Zakas <a href="http://radar.oreilly.com/2011/06/big-javascript-apps-teams.html" target="_blank">said it</a> well:</p>
<blockquote><p>
The key is to acknowledge from the start that you have no idea how this will grow. When you accept that you don&#8217;t know everything, you begin to design the system defensively.
</p></blockquote>
<h3>Naming conventions</h3>
<p>There are many conventions in JavaScript&#8211;some more popular than the others.  I find it important to maintain consistency with most JavaScript code out there.  Unfortunately, I haven&#8217;t found empirical evidence showing which conventions are most popular, but I have done some rudimentary research and will propose a few seemingly most-popular conventions.  Again, these are only suggestions.  The most important point is that you agree on a standard as a team and stay consistent.</p>
<ul>
<li>Names of directories and files should be all lowercase.  Add hyphens between words.</li>
<li>Variable names should be <a href="http://en.wikipedia.org/wiki/CamelCase">camelCase</a>.</li>
<li>Objects (functions) meant to be instantiated should start with a capital letter.</li>
<li>Private scope doesn&#8217;t exist in JavaScript except through closures.  See the poorly-named <a href="http://yuiblog.com/blog/2007/06/12/module-pattern/" target="_blank">module pattern</a> for more info but I <a href="http://snook.ca/archives/javascript/no-love-for-module-pattern" target="_blank">don&#8217;t really love the consequences of the pattern</a>.  Instead, I suggest prefixing names of members meant to be private with an underscore.  It doesn&#8217;t provide true privacy, but it&#8217;s a pretty solid red flag that you shouldn&#8217;t use it publicly.</li>
</ul>
<h3>Testing</h3>
<p>Strongly-typed languages (e.g., Java, C#, ActionScript 3) specify the type of each variable throughout the application.  This means the compiler can raise red flags when you try to use an object in a manner that wasn&#8217;t intended by its type.  In essence, strong typing can be considered to be a bunch of built-in, simple unit tests that are run each time the app is compiled.</p>
<p>JavaScript is not a strongly-typed language but instead is a very loosely-typed language.  It&#8217;s much easier to make mistakes by using objects in ways that weren&#8217;t intended or just fat-fingering a variable name and you won&#8217;t find out about it until you run the code and see the error (a good IDE can also help).  For this reason, I believe testing is even more important in loosely-typed languages.</p>
<p>There are many JS libraries available to help you test your code.  <a href="http://docs.jquery.com/QUnit" target="_blank">QUnit</a> seems to work well and is popular.  If you don&#8217;t already have a favorite, I suggest to start there and research outward.</p>
<p><a href="http://code.google.com/p/js-test-driver/wiki/GettingStarted" target="_blank">JsTestDriver</a> can also be very useful (and happens to have a <a href="http://code.google.com/p/js-test-driver/wiki/QUnitAdapter" target="_blank">QUnit adapter</a>).  JsTestDriver allows you to run your tests within a variety of browsers.  This can be very helpful since browsers aren&#8217;t consistent in their implementation.  For example, I might write code using <code>myArray.indexOf()</code> which will work on most browsers but not older Internet Explorers.  By running your tests within actual browsers these issues will crop up and you&#8217;ll find out about them before your users do.</p>
<h3>Strict mode</h3>
<p>The 5th version of <a href="http://en.wikipedia.org/wiki/ECMAScript" target="_blank">ECMAScript</a> (the standard upon which JavaScript is based) provides an option with which you can tell browsers to be more strict with your code.  Newer browsers will respect it while older browser will stay lenient.  Why would you want browsers to be <em>more</em> strict with your code?  Because you want to decrapify your code.  Strict mode provides the following (<a href="https://developer.mozilla.org/en/JavaScript/Strict_mode" target="_blank">copied from MDN</a>):</p>
<ul>
<li>Strict mode eliminates some JavaScript pitfalls that didn&#8217;t cause errors by changing them to produce errors.</li>
<li>Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that&#8217;s not strict mode (Firefox 4 generally hasn&#8217;t optimized strict mode yet, but subsequent versions will).</li>
<li>Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.</li>
</ul>
<p>I highly recommend <a href="https://developer.mozilla.org/en/JavaScript/Strict_mode#Invoking_strict_mode" target="_blank">invoking strict mode</a> to keep your code squeaky clean.</p>
<h3>Code Analysis Tools</h3>
<p>A couple popular code analysis tools exist to likewise help decrapify your code.  They scan your code and raise flags when they see you&#8217;ve done something dangerous or undesirable.  They mainly focus on syntax, style, and structure.  They do not verify that your logic is correct.</p>
<p>The first is <a href="http://www.jslint.com/" target="_blank">JSLint</a>.  It&#8217;s &#8220;the original&#8221;.  The second is <a href="http://www.jshint.com/" target="_blank">JSHint</a>.  JSHint explains the difference:</p>
<blockquote><p>
JSHint is a fork of JSLint, the tool written and maintained by Douglas Crockford.</p>
<p>The project originally started as an effort to make a more configurable version of JSLint—the one that doesn&#8217;t enforce one particular coding style on its users—but then transformed into a separate static analysis tool with its own goals and ideals.
</p></blockquote>
<p>Both tools have online versions where you can copy-paste a snippet into a textarea and have the code analysis do its thing.  This works okay for bits of code here and there, but even better is setting up the analysis code to run on your full app.  Both tools have integrations to you allow you to analyze your app&#8217;s code from your IDE or command line.  On our team, we run the tool from an Ant script that we can then run from the command line, our IDE, or our deployment servers.  I highly recommend you run these tools often right off the bat.  Note that each have flags you can set to be more or less strict in its analysis.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/javascript/javascript-architecture-organization-and-quality/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[JavaScript Architecture]]></series:name>
	</item>
		<item>
		<title>JavaScript Architecture: The Basics</title>
		<link>http://aaronhardy.com/javascript/javascript-architecture-the-basics/</link>
		<comments>http://aaronhardy.com/javascript/javascript-architecture-the-basics/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 00:06:18 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[architect]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[WebStorm]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=1022</guid>
		<description><![CDATA[This post is intended to be the first of a series. I want to be clear about what it entails and its intended audience. For the past several years I&#8217;ve been an architect in enterprise-level RIAs. This is a fancy way of saying I oversee the design and construction of apps that are web-based but [...]]]></description>
			<content:encoded><![CDATA[<p>This post is intended to be the first of a series.  I want to be clear about what it entails and its intended audience.</p>
<p>For the past several years I&#8217;ve been an architect in enterprise-level RIAs.  This is a fancy way of saying I oversee the design and construction of apps that are web-based but have a lot of the same characteristics as desktop applications.  Some of the applications I work on <i>are</i> in fact desktop applications but heavily communicate with the web.  The line becomes very blurry, but the main point is&#8211;I deal with <i>applications</i> which in my world are quite different than what is often considered a &#8220;website&#8221; even though they both live within a browser.</p>
<p>There are others in the industry who do what I do or would like to do what I do.  Of those there are some who are new to JavaScript.  This is my intended audience.  An architect arriving at JavaScript for the first time has a lot to learn and many decisions to make.  As the lead architect on a product and being fairly new to JavaScript (well, modern JavaScript) I likewise had to go through this process and have spent countless hours re-learning JavaScript, testing IDEs, discovering communities, exploring deployment procedures, learning what questions to ask, evaluating libraries (mvc, dependency management, dom manipulation, testing, deployment, utilities) and putting all these things together to lead a team and produce an application worthy of enterprise consumption. My hope is to help others along the way.<span id="more-1022"></span></p>
<p>You might think this is a broad effort, but my goal is not to simply regurgitate everything I&#8217;ve learned but to help boil things down to what&#8217;s important to get you on your way.  We&#8217;ll discuss core principles that are common in dynamic apps regardless of the languages or libraries used.  However, to illustrate them and hopefully give you enough of a concrete example to help you hit the ground running, I&#8217;ll be using specific, hand-picked libraries for this series.  In your own research you may find other libraries are more appropriate for your particular project.  That&#8217;s fine; the principles in this series are still valuable and applicable.</p>
<h3>Choose wisely</h3>
<p>JavaScript is merely a language.  HTML is, for the most part, a set of dilapidated, relatively primitive components.  To reduce boilerplate, abstract browser inconsistencies, and provide engineers with higher-level tools to be more productive, all kinds of libraries have come out of the woodwork.  Because JavaScript and HTML leave out some basic structures and components that are so commonly needed, these libraries often overlap in functionality and features.  This also means some don&#8217;t play well together.  This is both exciting because you have so many options to choose from and yet frustrating because you have so many options to choose from.</p>
<p>As an architect, specifically in an enterprise setting, these decisions will be very important and can ultimately determine the fate of your product. If I were farting around on a side-project, I&#8217;d probably feel comfortable picking the most awesome-sounding libraries and running with them.  In the corporate world though, we have to answer some important questions:  Is our product part of a larger suite?  If so, what is the rest of the suite using?  If the rest of the suite were to standardize on a particular library, how hard would it be for us to refactor?  What if we chose a &#8220;full-stack&#8221; library and a portion of it is or becomes inferior to other options?  What&#8217;s the chance that a different library may overtake my library in community and technical progression?  What&#8217;s the community like around those libraries?  Which libraries are gaining steam or fizzling out?  What&#8217;s their learning curve? How easy is it to hire developers for these libraries?  It&#8217;s a very different ballgame so take care in your decisions. In my case, I found the best option was to pick a handful of high-quality tools with healthy communities that worked well together but were decoupled in their implementations.  <a href="http://grooveshark.com/" target="_blank">Grooveshark</a> provides <a href="http://blog.jerodsanto.net/2010/12/the-tech-behind-the-new-grooveshark/" target="_blank">a very interesting related case study</a>.</p>
<h3>Rolling your own</h3>
<p>There comes a time in every developer&#8217;s life where we get caught up in rolling our own libraries.  Maybe we think our work is superior, maybe we don&#8217;t do enough research on what&#8217;s already available, or maybe there&#8217;s just one small thing that isn&#8217;t quite right in a library so we rewrite it from scratch.  I&#8217;ve done it and you likely have too.  When determining what your team will use, it&#8217;s very important to keep in mind some of the significant benefits of going with a wisely-chosen 3rd-party/open-source library:</p>
<ul>
<li>Free development resources</li>
<li>Free documentation</li>
<li>Free bug fixes</li>
<li>Free testing by automated tests and other community members</li>
<li>Collaboration amongst generally intelligent industry leaders</li>
<li>A place to learn more and ask questions</li>
</ul>
<p>Far too often I&#8217;ve seen very intelligent architects roll their own libraries because they feel their skills are superior or are OCD that the libraries work exactly like they want and carries no extra baggage.  Very often, if that architect were to stay on the project forever, this would probably work out quite well and the product would be successful.  In reality, the architect leaves.  Almost always they were time-crunched and didn&#8217;t fully document the library, set up testing, or cross-train their co-workers.  They developed the library for the purpose of fulfilling the specific needs of a project and even though the library may have been a fantastic work of art, the engineers left with that work of art don&#8217;t understand its intended usage or how to make modifications.  From there, the engineers begin using it incorrectly and if they have questions they have nobody to ask and nowhere to look except for undocumented, increasingly morbid code.  Inevitably, the product starts its death-spin, the engineers blame the architect that left, and technical debt grows until the product declares bankruptcy.  At this point it&#8217;s time to rebuild or sink.</p>
<p>Truthfully, most of the fault really does fall on the architect.  The role of the architect is not only to code awesomeness, but also to understand the software circle of life and make wise decisions.</p>
<p>Don&#8217;t get me wrong&#8211;there is a time and place for rolling our own libraries.  If you come across this time and place, make sure you have the necessary resources behind it.  Do it right.  Where possible, create the libraries as though other companies might also use them.  Document them&#8211;not only in code but in API documents, examples, and tutorials.  Open-source them on <a href="https://github.com/" target="_blank">GitHub</a> and make it a home for external resources to congregate around and contribute.  Pay attention to others&#8217; contributions and incorporate their ideas and code where possible.  Many if not most of the great libraries available today originated from this concerted effort.</p>
<p>One last note about open-source libraries.  The historical perception is that these libraries are siloed away in some remote part of the web and their developers are anonymous and unapproachable.  This usually isn&#8217;t the case anymore&#8211;especially with GitHub.  Oftentimes if you have a legitimate concern the community is very approachable and will respond quickly and respectfully.  A great example of this is when I recently found that <a href="http://documentcloud.github.com/underscore/" target="_blank">underscore.js</a> lacked a function for finding the symmetric difference between multiple arrays.  I provided the underscore.js community an example of where it would be beneficial and <a href="https://github.com/documentcloud/underscore/pull/309#issuecomment-2852147" target="_blank">five days later it was added to the trunk with a unit test and documentation</a>.  Good libraries have good communities.  Choose wisely.</p>
<h3>We don&#8217;t need no stinkin&#8217; frameworks!</h3>
<p>A long-standing debate among some in the community is whether formal frameworks are needed at all.  The argument against frameworks usually goes something like, &#8220;We&#8217;re all smart engineers&#8211;we can architect an app well without having to resort to a formal framework.&#8221;  That may be true, but it&#8217;s usually a fallacy.  If it&#8217;s not a fallacy, it&#8217;s very difficult to do with most teams.  If you are able to successfully execute on that concept, you very likely unconsciously re-wrote large portions of a framework you could have used in the first place.</p>
<p>When working in a team, a framework provides an important benefit of consistency and a game plan.  Good frameworks generally prescribe examples of best-practice usage but provide some flexibility in implementation.  No doubt engineers can abuse the best of frameworks, but having a blueprint of how architecture and implementation should, in general, be performed goes a long way in getting everyone on the team to have a consistent vision and understanding.</p>
<h3>Learn JavaScript</h3>
<p>Learning JavaScript is not a one-time event.  You&#8217;ll be learning over a long period of time, but it&#8217;s important that you actually make it a priority to learn the language.  A lot of the libraries we&#8217;ll be looking at do a lot of abstracting for you&#8211;keeping you away from the nitty-gritty of JavaScript.  That&#8217;s very helpful and can speed up your development dramatically, but it should not be used as a crutch.  Far too often people learn libraries but not the language.  This is a mistake and will bite you in the end.</p>
<p>I&#8217;ll be the first to admit that JavaScript has some really nasty parts.  Frankly, it&#8217;s not my favorite language.  But you&#8217;ll be living in it.  Accept it and learn how to take advantage of its good parts and avoid its bad parts.  To that end, I recommend starting with Douglas Crockford&#8217;s <a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742" target="_blank">JavaScript: The Good Parts</a>.  Ironically, it&#8217;s a small book and very approachable by JavaScript noobs.</p>
<h3>IDE</h3>
<p>Get yourself a quality development environment.  If we&#8217;re talking web we&#8217;re primarily talking HTML, JavaScript, and CSS.  There are all kinds of free web-oriented IDEs (<a href="http://aptana.com" target="_blank">Aptana</a> comes to mind) but I recommend you pick up a copy of <a href="http://www.jetbrains.com/webstorm/" target="_blank">WebStorm</a> by <a href="http://www.jetbrains.com" target="_blank">JetBrains</a>.  A good IDE will give you great JavaScript code-hinting to shield you from some of the woes that come with the dynamically-typed language.  Other things I look for: coding workflow, local and remote debugging, version control integration, remote server deployment, plugins, etc.</p>
<p>Have comments or questions?  Post them below!</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/javascript/javascript-architecture-the-basics/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<series:name><![CDATA[JavaScript Architecture]]></series:name>
	</item>
		<item>
		<title>Nimbus &#8211; MVC Framework Intro</title>
		<link>http://aaronhardy.com/flex/nimbus-mvc-framework-intro/</link>
		<comments>http://aaronhardy.com/flex/nimbus-mvc-framework-intro/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 22:51:45 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[model view controller]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[nimbus]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[redo]]></category>
		<category><![CDATA[sequence]]></category>
		<category><![CDATA[undo]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=221</guid>
		<description><![CDATA[UPDATE: Since the writing of this post, I&#8217;ve converted to and fully endorse Robotlegs as my MVC framework of choice. I invite you to check it out. For those who don&#8217;t know, Nimbus is a Flex MVC framework (or micro-architecture for the technically-inclined). Sponsored by Rain, we develop it primarily for use on our applications [...]]]></description>
			<content:encoded><![CDATA[<hr/>
<b>UPDATE: Since the writing of this post, I&#8217;ve converted to and fully endorse Robotlegs as my MVC framework of choice. I invite you to <a href="http://www.robotlegs.org" target="_blank">check it out</a>.</b></p>
<hr/>
<p>For those who don&#8217;t know, <a href="http://code.google.com/p/nimbus-as3" target="_blank">Nimbus</a> is a Flex MVC framework (or micro-architecture for the technically-inclined).  Sponsored by <a href="http://mediarain.com" target="_blank">Rain</a>, we develop it primarily for use on our applications but make it available for public use and encourage the community to <a href="http://code.google.com/p/nimbus-as3" target="_blank">contribute</a>.</p>
<p>Nimbus pulls core concepts from <a href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a> but is meant to cut out a lot of the plumbing developers groan about when they hear &#8220;Cairngorm.&#8221;  It&#8217;s light, but it&#8217;s not <a href="http://i260.photobucket.com/albums/ii22/shastymax/fluffy.jpg" target="_blank">fluffy</a>.  Baked in are those rare and tasty flavor morsels you thought only existed in those <a href="http://laparisiennecentre.com/blog/nfblog/wp-content/uploads/2007/07/confetticupcakes.JPG" target="_blank">Funfetti cupcakes</a> <a href="http://www.chaos.gwdg.de/~sinan/resimler/GRANDMA_KISS.GIF" target="_blank">your grandmother</a> bakes with love on your birthday.<span id="more-221"></span></p>
<p>Until the end of 2008, Nimbus had been shaped, re-shaped, patched, and plugged.  A complete re-write was merited so that&#8217;s what we did.  And really, what makes for a better Christmas than sitting down with your stocking stuffed with jelly beans and re-writing an MVC framework.  A few months later, the code is settling down along with the sugar and we&#8217;re ready for action.</p>
<p>Here are some features of Nimbus:</p>
<ul>
<li>Notification system of command completion so portions of the application can know when a command has finished executing.</li>
<li>Nestable &#8220;macro&#8221; commands.  Think &#8220;I want commands A and B to run in parallel, then C, then D and E in parallel, but inside D I want F and G to run sequentially&#8230;&#8221;</li>
<li>Undo/Redo functionality.</li>
<li>Progress feedback system.  You as a developer can associate a processing message with each command or macro command to provide feedback to users.</li>
<li>Interaction blocking system.  While commands are executing, easily disable user interaction to protect your service calls.</li>
<li>Combinable commands.  Ensure that similar command instances can be grouped appropriately in the undo/redo history.</li>
</ul>
<p><em><a href="/flex/the-best-flex-mvc-framework/">Nimbus may not be right for everyone</a>.  Call your system administrator if your scalability problem worsens or is not better within 7 to 10 days.  This may mean that there is another condition causing your RTE problems.  Do not take Nimbus with alcohol.  Call your system administrator right away if after taking Nimbus you are unable to walk, drive, eat, or engage in other activities.  In rare cases, severe allergic reactions can occur.  Most MVC frameworks carry some risk of dependency.  Do not use MVC frameworks for extended periods without first talking to your system administrator.  You are encouraged to report negative side effects of Nimbus.  Visit <a href="http://code.google.com/p/nimbus-as3" target="_blank">http://code.google.com/p/nimbus-as3</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/nimbus-mvc-framework-intro/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

