<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Naildrivin' &#10106;]]></title>
  <link href="http://www.naildrivin5.com/atom.xml" rel="self"/>
  <link href="http://www.naildrivin5.com/"/>
  <updated>2013-05-17T12:26:52-04:00</updated>
  <id>http://www.naildrivin5.com/</id>
  <author>
    <name><![CDATA[David Bryant Copeland]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[&#10106;&#10144; Source Code Typography]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/05/17/source-code-typography.html"/>
    <updated>2013-05-17T08:53:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/05/17/source-code-typography</id>
    <content type="html"><![CDATA[<p>What&#8217;s the one thing we, as developers, do with source code more than anything else?  We <em>read</em> it.  Sure, we change it, and even occasionally we write new source code, but, by and large, we <em>read</em> source code.  We read it to know how to use it.  We read it to know where to change it.  We read it to understand what it does.</p>

<p>But, it&#8217;s not just the content - the code itself - that affects readability.  How it&#8217;s presented matters and if we&#8217;re going to talk about presentation, we have to talk about typography.</p>

<!-- more -->


<p>One could argue that, all things being equal, source code should be written in a way optimized for reading.  Sure, occasional performance concerns require putting readability in the backseat, but this is rare.  Source code should be written to be understood by people.</p>

<p>This isn&#8217;t a radical concept, and it&#8217;s why we have structured programming, why we use descriptive variable names, and why we have conventions about where the files containing source code live. If a codebase is like a book, we&#8217;re all agreed how to title the chapters, where the table of contents should go, and what the index looks like.</p>

<p>But there&#8217;s also how the code is presented to us - the code&#8217;s <em>typography</em>.  Typography is, according to Wikipedia, &#8220;the art and technique of arranging type in order to make language visible.&#8221;  <em>Type</em> of course, is the presented text, so typography, then, answers the question &#8220;what does each letter look like, and where does it go?&#8221;</p>

<p>We use typography all the time in our code.  We indent it whenever a new scope is created.  We limit our line lengths to a certain number of characters.  We color-code bits of code according to their purpose, aka syntax highlighting.  Some of us even align other parts of our code, such repeated inline comments, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kt">int</span> <span class="n">left</span> <span class="o">=</span> <span class="n">shape</span><span class="o">.</span><span class="na">positionLeft</span><span class="o">();</span> <span class="c1">// Our display is left-oriented</span>
</span><span class='line'><span class="kt">int</span> <span class="n">top</span> <span class="o">=</span> <span class="n">shape</span><span class="o">.</span><span class="na">positionTop</span><span class="o">();</span>   <span class="c1">// We start drawing pixels from the top</span>
</span></code></pre></td></tr></table></div></figure>


<p>This sort of &#8220;code formatting&#8221; isn&#8217;t about correctness, it&#8217;s about aesthetics, all in aid of making the code easier to read.  Typography doesn&#8217;t address what <em>should</em> be written but rather how it should be presented to make what <em>was</em> written as readable as possible.</p>

<p>A designer friend recommended <a href="http://www.amazon.com/Elements-Typographic-Style-Robert-Bringhurst/dp/0881791326">&#8220;The Elements of Typographic Style&#8221;</a>, by Robert Bringhurst.  This is &#8220;The Art of Computer Programming&#8221; for typography.  Very early in the book, Bringhurst has this to say:</p>

<blockquote><p>Well-chosen words deserve well-chosen letters; these in turn deserve to be
set with affection, intelligence, knowledge and skill.  Typography is a link,
and as it ought, as a matter of honor, courtesy and pure delight, to be as
strong as others in the chain.</p></blockquote>

<p>This is a passion for &#8220;fonts and stuff&#8221; that I never knew existed.  But, he&#8217;s right.  A well-typeset book is a leisurely stroll on a spring day, while a poorly set document is an encumbered march through a muddy field on a rainy day.</p>

<p>If you are at all interested in typography, typesetting, or fonts, I highly recommend the book.  It&#8217;s easily read, well-written, and - of course - beautifully typeset.</p>

<p>In the book, he talks about font choices, line heights, kerning, alignment, grids, tables, and anything else you could possibly imagine needing to make a design decision about when putting words onto a page.  But, it&#8217;s all about prose.  Can we apply any of these lessons to source code?</p>

<p>We&#8217;ve already established that programmers generally get value out of typography, via indentation and whitespace.  Many of us have a favorite fixed-width font for editing, and we can all argue about what the proper length of a line of code should be.</p>

<p>Some languages, like CoffeeScript and Python, require adherence to certain typographic principles - you must indent new blocks of scope.  The Go language comes bundled with its own typesetting program: <code>gofmt</code>.  Thus, all Go programs adhere to a strict set of typographic principles.  There&#8217;s a certain logic to this.</p>

<p>Is indenting and color-coding our source enough?  Can we make our code even more readable by heavier use of typography?</p>

<p>Let&#8217;s find out.</p>

<p>We&#8217;ll start with C, one of the oldest &#8220;high-level&#8221; languages.  Here is the source code for the <code>strcpy</code> function from the BSD kernel:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="kt">char</span> <span class="o">*</span>
</span><span class='line'><span class="n">strcpy</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">from</span><span class="p">)</span>
</span><span class='line'>        <span class="k">register</span> <span class="kt">char</span> <span class="o">*</span><span class="n">to</span><span class="p">;</span>
</span><span class='line'>        <span class="k">register</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">from</span><span class="p">;</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>        <span class="kt">char</span> <span class="o">*</span><span class="n">save</span> <span class="o">=</span> <span class="n">to</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">for</span> <span class="p">(;</span> <span class="p">(</span><span class="o">*</span><span class="n">to</span> <span class="o">=</span> <span class="o">*</span><span class="n">from</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">;</span> <span class="o">++</span><span class="n">from</span><span class="p">,</span> <span class="o">++</span><span class="n">to</span><span class="p">);</span>
</span><span class='line'>        <span class="k">return</span><span class="p">(</span><span class="n">save</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>It&#8217;s a short routine that packs a lot of functionality.  It&#8217;s set according to the BSD guidelines, with an 8-character indent (likely a tab).  Other than that, there&#8217;s not much going on typographically.  Does this make it hard to read?  Can we make it easier to read without changing the code?</p>

<p>Let&#8217;s start with the function declaration.  I like how the return type (line 1) is on a different line than the function and argument names (line 2), as it breaks up the signature into different chunks.  The argument declarations themselves (lines 3 and 4), which each appear on their own line after the function name, could be better set, in my opinion.</p>

<p>A C argument declaration is made up of modifiers (<code>register</code>, <code>const</code>), a data type (<code>char *</code>), and a name (<code>from</code>).  In this case, the elements of each declaration are vertically aligned, but in an odd way (likely their alignment is merely by happenstance - both arguments are declared <code>register</code>).  The <code>char</code> for the argument <code>to</code> aligns with the <code>const</code> of the argument <code>from</code>.  <code>const</code> is a modifier, and <code>char</code> is a data type.  Things that are different are aligned with each other, which implies a sameness that doesn&#8217;t exist.</p>

<p>Let&#8217;s re-set the argument list, aligning like with like, forming a table structure, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="kt">char</span> <span class="o">*</span>
</span><span class='line'><span class="n">strcpy</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">from</span><span class="p">)</span>
</span><span class='line'>        <span class="k">register</span>       <span class="kt">char</span><span class="o">*</span> <span class="n">to</span><span class="p">;</span>
</span><span class='line'>        <span class="k">register</span> <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">from</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, the arguments block forms a table of three columns.  The modifiers make up the first column, the data types are aligned in the second column, and the names are in the third column (also notice how the syntax highlighting reinforces the new table structure).  We&#8217;ve also re-set the data type such that there is no space between <code>char</code> and <code>*</code> - the data type of both of these variables is &#8220;pointer to char&#8221;, so it makes more sense to put the space before the argument name, not in the middle the data type&#8217;s name.</p>

<p>Let&#8217;s move onto the <code>for</code> loop.  I&#8217;ve always found the <code>for</code> loop in C to be an odd construct, and it squeezes quite a bit of functionality into a small space.  It has two parts: the loop declaration and the loop body.  The declaration itself has three sub-parts: an initializing section, a test to see if the loop should continue, and code to run each time through the loop (the body is also executed each time through the loop, which is one reason I find this construct strage).</p>

<p>This particular <code>for</code> loop is tricky, because there is no initializing code in the declaration, nor is there a loop body.  To make this loop easier to read, we could rewrite it, but typography isn&#8217;t about changing the words, it&#8217;s about honoring them and making them easier to understand.  Can we re-set this code without changing it, but make it more readable?</p>

<p>One thing that would be handy is to make it more clear that there is <em>intentionally</em> no initializing code nor loop body.  We&#8217;d also like to make it simpler to see each part of the <code>for</code> loop for what it is.  Let&#8217;s add some whitespace and vertical alignment to see if we can improve it.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">for</span> <span class="p">(</span>
</span><span class='line'>                          <span class="p">;</span>
</span><span class='line'>        <span class="p">(</span><span class="o">*</span><span class="n">to</span> <span class="o">=</span> <span class="o">*</span><span class="n">from</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>        <span class="o">++</span><span class="n">from</span><span class="p">,</span> <span class="o">++</span><span class="n">to</span>
</span><span class='line'>    <span class="p">)</span>
</span><span class='line'>    <span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>By vertically aligning the semi-colons, the missing initializer jumps right out at you.  The whitespace created by the semicolons now looks intentional - that space couldn&#8217;t be there by accident.  We&#8217;ve done the same with the loop body, although we took care <em>not</em> to vertically align that semi-colon, thus creating a visual distinction between the two parts of the <code>for</code> loop.</p>

<p>With regard to the intentionally missing elements, developers might reveal this intent via a comment, like <code>/* no-op */</code>.  What I find interesting is that we can do this with mere typography.  The reader feels the intention without being directly told.</p>

<p>Let&#8217;s look at another example, this time using a much more recent programming language: Ruby.  Ruby takes its syntactic cues from Smalltalk and relies much less on analphabetic characters like braces and parens.  It also supports a powerful literal syntax for arrays and hash tables.  You can&#8217;t read too much Ruby code before coming across a <code>Hash</code> literal.</p>

<p>Let&#8217;s look at the routine from Ruby on Rails for creating a text field tag in HTML:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">text_field_tag</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
</span><span class='line'>  <span class="n">tag</span> <span class="ss">:input</span><span class="p">,</span> <span class="p">{</span> <span class="ss">:type</span> <span class="o">=&gt;</span> <span class="s2">&quot;text&quot;</span><span class="p">,</span> <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="nb">name</span><span class="p">,</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="n">sanitize_to_id</span><span class="p">(</span><span class="nb">name</span><span class="p">),</span> <span class="ss">:value</span> <span class="o">=&gt;</span> <span class="n">value</span> <span class="p">}</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">stringify_keys</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Wow, that is way off the page (or wrapping horribly, depending on how you&#8217;re reading this).</p>

<p>An important role that typography plays is to establish a &#8220;rhythm&#8221; to the typeset prose.  In a book, this means making decisions about font size, margins, line lengths, and so on.    The idea is to set each line long enough to let the reader take it in, but not so long that the reader becomes exhausted or loses their place.  This code has no rhythm and is exhausting to read.</p>

<p>Compounding the problem of overall length is that the information here is at varying levels of abstraction.  We have a method call to <code>tag</code>, some symbols, a <code>Hash</code>, and more method calls at the end.  By the time you get to the end of this line, it&#8217;s hard to remember what it&#8217;s even supposed to be doing.  We need to break this up with some sort of rhythm that will allow us to process it in chunks.</p>

<p>In English, we would establish this rhythm by analyzing the length of words and sentences. In source code, we have tokens and expressions. This Ruby method has 29 tokens, all on one line.  That line has several expressions as well, and both of these facts are what&#8217;s making this line so hard to read.</p>

<p>Let&#8217;s re-set this code to keep the tokens-per-line low but avoid splitting expressions across lines.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">text_field_tag</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
</span><span class='line'>  <span class="n">tag</span> <span class="ss">:input</span><span class="p">,</span>
</span><span class='line'>      <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="ss">:type</span>  <span class="o">=&gt;</span> <span class="s2">&quot;text&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="ss">:name</span>  <span class="o">=&gt;</span> <span class="nb">name</span><span class="p">,</span>
</span><span class='line'>        <span class="ss">:id</span>    <span class="o">=&gt;</span> <span class="n">sanitize_to_id</span><span class="p">(</span><span class="nb">name</span><span class="p">),</span>
</span><span class='line'>        <span class="ss">:value</span> <span class="o">=&gt;</span> <span class="n">value</span>
</span><span class='line'>
</span><span class='line'>      <span class="p">}</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">stringify_keys</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Each line now has only one or two expressions, and many fewer tokens.  We&#8217;ve also used indentation and whitespace to delineate each part of this code.</p>

<p>Both arguments to <code>tag</code> are left-aligned at the seventh column - similar things have similar alignment.  The biggest change, however, is in the <code>Hash</code> literal.</p>

<p>First, we&#8217;ve offset its contents by whitespace, primarily to distinguish it from the call to the <code>update</code> method.  Without a blank line after the final value in the <code>Hash</code>, there&#8217;s an unfortunate vertical alignment between the hash key <code>:value</code> and the method call to <code>update</code>.  We want to make it clear that these are distinct, and want to keep the reader from getting confused.</p>

<p>The <code>Hash</code> contents themselves have been re-formatted to form a visual table.  We&#8217;ve used the &#8220;hashrockets&#8221; as a dividing line between key and value, which results in both being vertically left-aligned.</p>

<p>The code now has more of a rhythm to it, and can be read more easily, with line breaks inserted to give the reader a rest at the appropriate times.</p>

<p>Before we finish, I&#8217;d like to examine a typographical convention I&#8217;ve seen in JavaScript code, and discuss why I believe it actually impairs readability, failing to honor the content and thus fail to help the reader.</p>

<p>The convention was born out of a common idiom in in JavaScript where variables are declared in a comma-delimited list after a single <code>var</code>, with the list terminated by a semicolon. The typographical convention I&#8217;m referring to is in how this list is formatted, namely, it&#8217;s formatted with leading commas, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">left</span><span class="p">()</span>
</span><span class='line'>  <span class="p">,</span> <span class="nx">y</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">right</span><span class="p">()</span>
</span><span class='line'>  <span class="p">,</span> <span class="nx">numSides</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">sides</span><span class="p">()</span>
</span><span class='line'>  <span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is typeset poorly, insomuch as it does not honor the readability of the content, instead hindering it.</p>

<p>The most important part of a variable declaration is the name of the variable (the second being its default value).  Putting a comma first creates at typographic roadblock between the reader and the information they need (the variable name).  That a comma is required between variable declarations is one of the least important pieces of information in this code, yet it&#8217;s front and center.  This is bad typography.</p>

<p>The more conventional approach is much better, as the language&#8217;s peculiarities simply fade away:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">left</span><span class="p">(),</span>
</span><span class='line'>    <span class="nx">y</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">right</span><span class="p">(),</span>
</span><span class='line'>    <span class="nx">numSides</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">sides</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>Each line can now be easily read from left to right.  The <code>var</code> establishes this block as a &#8220;paragraph&#8221;, and each declaration is a sentence. Each line is eminently readable - it starts with the name of the variable, which is what the reader is most interested in, and is followed by the default value.  The names are left-aligned and are the first thing on each line (save for the initial <code>var</code> which establishes that these are variables).  The commas, being incidental, are hidden nicely at the end of the line.</p>

<p>We can reinforce this structure with vertical alignment of the <code>=</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">x</span>        <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">left</span><span class="p">(),</span>
</span><span class='line'>    <span class="nx">y</span>        <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">right</span><span class="p">(),</span>
</span><span class='line'>    <span class="nx">numSides</span> <span class="o">=</span> <span class="nx">shape</span><span class="p">.</span><span class="nx">sides</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>We could go even further and align the commas and semi-colon, however this draws attention to aspects of the code that aren&#8217;t important (much as the comma-first style does).  Although JavaScript needs to know when each declaration ends, we, the human reader, already know - the line merely ends.  The declaration block itself ends with whitespace - that JavaScript requires a semicolon is incidental to reading this code.</p>

<p>This is why the &#8220;comma-first&#8221; typesetting is so distracting.  It puts the comma - something unimportant to reading the code - right up front, forcing the reader to slog through it to get to the real meat of the declaration.</p>

<p>I could go on about many more idiomatic typesetting in various languages, but I thought it would be a fun exercise to make an argument against this style, based purely on tyopgraphical concerns, and see if it sticks.</p>

<p>Thinking about code typography has made more more bold in my code formatting choices, but there is a practical cost to this: source control diffs.</p>

<p>Unlike a printed book, code changes frequently.  When we use heavier typography, we end up having to re-set the code around a particular change, and this creates non-functional changes to source code, making the overall changeset larger than it needs to be.  I suppose we&#8217;d have to start applying typographic principles to our diffs as well :)</p>

<p>Despite these problems, I still think it&#8217;s worth taking a fresh look at code typography - anything that helps us read and understand code better has to be a good thing.  At the very least, having a clear understanding of why code is set in a certain way can help us better understand the purpose of the code, and the trade-offs we make between reading, modifying, and writing it.</p>

<hr />

<p>Special thanks to <a href="https://twitter.com/mrmrs_">@mrmrs_</a> for the book recommendation and to <a href="https://twitter.com/jxnblk">@jxblk</a> for
reviewing this post.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Getting OmniAuth with Google Apps to Work on Heroku]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/04/29/getting-omniauth-with-google-apps-to-work-on-heroku.html"/>
    <updated>2013-04-29T11:01:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/04/29/getting-omniauth-with-google-apps-to-work-on-heroku</id>
    <content type="html"><![CDATA[<p>At <a href="http://www.stitchfix.com">Stitch Fix</a>, we outsource pretty much <em>all</em> of our hosting and technical needs to Heroku or their add-ons.  Given
where we are now as a company, it makes total sense: we don&#8217;t need to hire an admin, we don&#8217;t need to adminster actual boxes, and
we can easily add/remove/change our technical infrastructure.  If you are a small startup and you are messing with Linode slices,
   you are probably wasting time.</p>

<p>One thing Heroku doesn&#8217;t provide out of the box is a login system for &#8220;internal&#8221; users.  The vast majority of the software at
Stitch Fix is targeted at Stitch Fix employees - to operate the warehouse, choose what goes into a fix, etc.  The natural way to
allow them to login is via Google Apps.  We can use everyone&#8217;s existing username/password, and employees can be added during
onboarding and removed when they leave the company, all in one place.</p>

<p>Getting it to work with our Rails apps <em>seemed</em> easy enough with <a href="http://github.com/intridea/omniauth">OmniAuth</a>, but it turned out to be a lot trickier, resulting in
random failures with the oh-so-helpful error &#8220;invalid_credentials&#8221;.  Here&#8217;s how to fix that, and why you can&#8217;t just use the
out-of-box configurations recommend by OmniAuth.</p>

<!-- more -->


<p><em>tl;dr scroll down</em></p>

<p>This is not a dig at OmniAuth - it&#8217;s super awesome.  It&#8217;s just that it bakes in a lot of assumptions that may not hold if you are using Heroku or are follow the <a href="http://www.12factor.net">12-factor</a> app architecture.   You end up needing to know a bit more about how things are working, and you have to stop trusting default configurations.</p>

<p>First, the general setup of OmniAuth recommends this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Builder</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span> <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>We use the <a href="https://github.com/sishen/omniauth-google-apps">omniauth-google-apps</a> gem, which is a very thin extension of <a href="https://github.com/intridea/omniauth-openid">omniauth-openid</a> that makes setup a bit simpler, and
allows us to only allow Stitch Fix employees access to our systems.</p>

<p>This setup has issues with SSL certificates, so we need to tell OpenID where the CA file is, and we just use curl&#8217;s, checked-into
our source code because of Wacky Heroku Thing #1 - no guarantees about what&#8217;s on the Dynos.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid/fetchers&#39;</span>
</span><span class='line'><span class="no">OpenID</span><span class="o">.</span><span class="n">fetcher</span><span class="o">.</span><span class="n">ca_file</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="p">,</span><span class="s1">&#39;config&#39;</span><span class="p">,</span><span class="s1">&#39;curl.pem&#39;</span><span class="p">)</span>
</span><span class='line'><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Builder</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span> <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can&#8217;t just assume that <code>curl</code> is even installed, much less make any assumptions about where the <code>pem</code> file is, so we have to
include it.  Another option would be to provide an environment variable based on where we know it is on the Dyno, but this seemed
simpler.</p>

<p>Now, the problem with this setup vis-a-vis Heroku is that there&#8217;s a configuration option being set that is not apparent, because
OmniAuth/OpenID is using what it believes to be a sensible default, but is, in fact, not correct.</p>

<p>OpenID requires the ability to store information server-side so that, after you are redirected back from the auth provider
(Google, in our case), the server can find this information and complete the login.  <em>How</em> this information is stored can be
configured via the <code>:store</code> option to <code>provider</code>.  The default is an in-memory store, so it&#8217;s equivalent to this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span> <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span><span class="p">,</span> <span class="n">store</span><span class="p">:</span> <span class="no">OpenID</span><span class="o">::</span><span class="no">Store</span><span class="o">::</span><span class="no">Memory</span><span class="o">.</span><span class="n">new</span>
</span></code></pre></td></tr></table></div></figure>


<p>For development, this seems reasonable - it doesn&#8217;t require any setup - but for deployment, it&#8217;s Just Wrong, which we can tell by
reading the RubyDoc of the <code>OpenID::Store::Memory</code> class from <code>ruby-openid</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># An in-memory implementation of Store.  This class is mainly used</span>
</span><span class='line'><span class="c1"># for testing, though it may be useful for long-running single</span>
</span><span class='line'><span class="c1"># process apps.  Note that this store is NOT thread-safe.</span>
</span><span class='line'><span class="c1">#</span>
</span><span class='line'><span class="c1"># You should probably be looking at OpenID::Store::Filesystem</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&#8217;ll get to <code>OpenID::Store::Filesystem</code>, but what&#8217;s wrong with the memory store?</p>

<p>Let&#8217;s assume Unicorn as the server (as
recommended for the Cedar stack for Rails apps).  The recommended configuration allows three unicorn processes per Dyno, which
gives use three processes, each with it&#8217;s own separate memory space.</p>

<p>Because unicorn uses <em>process-based</em> concurrency, which means that, when a new process is started, it gets a <em>copy</em> of the parent&#8217;s
memory, all three unicorns on a single Dyno <em>do not</em> share memory. Meaning if process 1 started the OpenID dance, but, after
redirect, your request was handled by process 2, it doesn&#8217;t have the necessary information stored in memory.  Boom!
invalid_credentails error.</p>

<p>So, what about that filesystem-based one?
OmniAuth&#8217;s docs <em>do</em> mention <code>OpenID::Store::Filesystem</code>, but it&#8217;s still wrong on Heroku.  Why?</p>

<p>Here&#8217;s how we&#8217;d set up the filesystem-based store:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span>
</span><span class='line'>         <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span><span class="p">,</span>
</span><span class='line'>         <span class="n">store</span><span class="p">:</span> <span class="no">OpenID</span><span class="o">::</span><span class="no">Store</span><span class="o">::</span><span class="no">Filesystem</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="p">,</span><span class="s1">&#39;tmp&#39;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can&#8217;t even be guaranteed of <code>/tmp</code> existing, so we set up the store inside our Rails app.  This configuration works great in
development, because I&#8217;m running my server on one machine - all three Unicorn processes share the same data store.</p>

<p>If we deployed to Heroku using just one Dyno, this would work.  However, the second we scale up our app to use more Dynos, the
entire thing falls apart.  Why?</p>

<p>Two reasons:</p>

<ul>
<li>filesystem is ephemeral - it could go away at any moment.  Between redirects it&#8217;s possible (however unlikely) that the files go
away.</li>
<li>Dynos don&#8217;t share filesystems.  Even if we <em>could</em> guarantee the filesystem would live forever, you still run the risk that
your OpenID dance will be handled by two different Dynos, and thus: invalid_credentials.</li>
</ul>


<p>This is especially nasty because you might run your app for quite a while on one Dyno, thinking things are working when, instead,
you&#8217;re sitting on a ticking timebomb.</p>

<p>What we need as a centralized place to store this information, accessible to all Dynos and that persists across reboots.  This brings us to the third option included with <code>ruby-openid</code>, which is <code>OpenID::Store::Memcache</code>.</p>

<p>Of course, we can&#8217;t just plop <code>store: OpenID::Store::Memcache.new</code> into our configuration.  We first need to add memcache to our app, and then extract the needed connection parameters from the environment.  We also need to provide a memcache client object.</p>

<p>On Heroku, they recommend Dalli - strongly - so I went with that.  The interface that <code>OpenID::Store::Memcache</code> expects from the
memcache client is supported by Dalli, so we&#8217;re off to the races:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="err">$</span> <span class="n">heroku</span> <span class="n">addons</span><span class="ss">:add</span> <span class="n">memcache</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Add to your Gemfile</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">gem</span> <span class="s1">&#39;dalli&#39;</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Add to config/initializers/omniauth.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid/fetchers&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid/store/filesystem&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid/store/memcache&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;dalli&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="no">OpenID</span><span class="o">.</span><span class="n">fetcher</span><span class="o">.</span><span class="n">ca_file</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="p">,</span><span class="s1">&#39;config&#39;</span><span class="p">,</span><span class="s1">&#39;curl.pem&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Builder</span> <span class="k">do</span>
</span><span class='line'>  <span class="k">if</span> <span class="no">Rails</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">staging?</span> <span class="o">||</span> <span class="no">Rails</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">production?</span> <span class="o">||</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;OPENID_STORE&#39;</span><span class="o">]</span> <span class="o">==</span> <span class="s1">&#39;memcache&#39;</span>
</span><span class='line'>    <span class="c1"># Locally, these env vars will be blank, and it will connect to the local memcached</span>
</span><span class='line'>    <span class="c1"># client running on the standard port</span>
</span><span class='line'>    <span class="n">memcached_client</span> <span class="o">=</span> <span class="no">Dalli</span><span class="o">::</span><span class="no">Client</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;MEMCACHE_SERVERS&#39;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                                         <span class="ss">:username</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;MEMCACHE_USERNAME&#39;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>                                         <span class="ss">:password</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">&#39;MEMCACHE_PASSWORD&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>    <span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span> <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span><span class="p">,</span>
</span><span class='line'>                           <span class="n">store</span><span class="p">:</span> <span class="no">OpenID</span><span class="o">::</span><span class="no">Store</span><span class="o">::</span><span class="no">Memcache</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">memcached_client</span><span class="p">)</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="n">provider</span> <span class="ss">:google_apps</span><span class="p">,</span> <span class="n">domain</span><span class="p">:</span> <span class="s1">&#39;your-domain.com&#39;</span><span class="p">,</span>
</span><span class='line'>                           <span class="n">store</span><span class="p">:</span> <span class="no">OpenID</span><span class="o">::</span><span class="no">Store</span><span class="o">::</span><span class="no">Filesystem</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="p">,</span><span class="s1">&#39;tmp&#39;</span><span class="p">))</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Whew!  This setup doesn&#8217;t require memcache for development, but allows it as an option by setting the <code>OPENID_STORE</code> environment
variable.  Although the Dalli client claims to use the environment variables automatically, the code doesn&#8217;t indicate this to be
true when there is a username and password, and I&#8217;m kindof prefering some explicit configuration after all this.</p>

<p>Now, no more &#8220;invalid_credentials&#8221; error!</p>

<p>The way Heroku makes us design our apps is a good thing, but it&#8217;s easy to forget it because many &#8220;beginner&#8221; scenarios seem to
work even if we&#8217;ve configured things incorrectly.  Anything this crucial to your application is worth your while understanding at a detail level how it works - at least orient yourself around the default configuration.  And deploy to two Dynos as quickly as you can.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[&#10106;&#10144; How to switch to Vim]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/04/24/how-to-switch-to-vim.html"/>
    <updated>2013-04-24T09:38:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/04/24/how-to-switch-to-vim</id>
    <content type="html"><![CDATA[<p>From time to time, I see people in my twitter stream attempting to switch to Vim.  This is a
good thing.  The problem is that they appear to be viewing the switch as swapping out one
tool for another.</p>

<p>This is not what switching to Vim means, nor is it a good reason to switch.</p>

<p>The reason to switch to Vim is to <em>work better</em>.  I realize &#8220;better&#8221; is subjective, but
whatever way <em>you</em> define it is what it means - code faster, edit text more easily, automate
your workflow, whatever.</p>

<p>As such, switching to Vim is to <em>throw out</em> your old editor (or plan to) and replace it with
a <em>different tool</em> that works differently and is, hopefully, better.  Stop asking for &#8220;a
plugin that does XXX like Sublime Text does things&#8221;.  If Sublime Text has a plugin for what
you want, you don&#8217;t need Vim.  Vim might very well have a plugin that does whatever XXX is,
but it&#8217;s more likely that you don&#8217;t need a plugin, or that Vim provides a way to
accomplish your <em>real</em> goal much more efficiently.</p>

<p>Here&#8217;s how to make the move.</p>

<!-- more -->


<p>First, you aren&#8217;t going to learn much Vim from this.  There are tons of great tutorials about
how to actually <em>use</em> Vim.  You will, of course, need those to switch to Vim, but you&#8217;ll also need
a bit of a plan.  This is that plan.</p>

<ol>
<li>Run stock at first.</li>
<li>Ease into it.</li>
<li>Learn to install and remove extensions.</li>
<li>Add configuration and extensions only when needed.</li>
</ol>


<h2>Run stock at first</h2>

<p>Don&#8217;t install Janus.  Don&#8217;t install someone&#8217;s dotfiles.  Don&#8217;t install anything but Vim.  Vim is hard enough to learn without dealing with someone else&#8217;s idea of a good
editing environment.  Remember, your goal is to edit text (and code) better.  You&#8217;d be surprised at just how easy that is with a vanilla install of Vim and a bit of elbow grease.</p>

<p>It&#8217;s also important that, when learning Vim, you learn it from first principles.  You need to know in your heart of hearts that Vim editing is all about combining movements with actions.  It&#8217;s like playing a musical instrument.</p>

<p>To learn to play guitar, you should grab an acoustic and a chordbook.  You should <em>not</em> get a Marshall half-stack, vintage pedalboard, and Gibson Custom Shop Les Paul Slash Special Signature Edition.  In all honesty, you&#8217;re likely to find out a Fender Strat works just fine with a good distortion pedal, so don&#8217;t start off with a bunch of crap in your <code>~/.vim</code> directory.</p>

<h2>Ease into it</h2>

<p>You don&#8217;t want to uninstall Chocolat (or whatever) and jump into your next coding assignment with Vim.  At least not at first.  That will be bad for you and your company.  Instead, tell your operating system that henceforth, all text files, Markdown files, Asciidoc files and any other text-like format shall be edited in Vim.  Then, proceed to use Vim only for editing <em>text</em>.</p>

<p>Although editing text doesn&#8217;t benefit from many of Vim&#8217;s amazing plugins and features, it requires <em>just enough</em> for you to &#8220;level up&#8221; and get better at editing text. Before you know it, you&#8217;ll be deleting words, moving the cursor with search, creating abbrevations and all the other great stuff that makes Vim Vim, but in a safe, easy environment of text editing.  If you don&#8217;t edit a lot of text, shame on you.  You should write more.  It&#8217;s good for you.</p>

<p>Now, don&#8217;t just go into insert mode and cursor around like you&#8217;re in Notepad.  This is where
those tutorials and references come in.  Follow them and use them.  My advice is:</p>

<ul>
<li>When you get a thought down, hit escape to go to command mode<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>.  A.B.C. Always Be in Command Mode.  A, Always, B, Be, C, in Command Mode.  You enter command mode mode or you hit the bricks.</li>
<li>When are ready to type, enter insert mode and type.  Then hit escape when you are done.  A.B.C.</li>
<li>Before touching the mouse, the cursor keys, the backspace key, or the delete key, ask yourself what you are <em>really</em> trying to do.  Are you <em>really</em> deleting 10 characters that
are all adjacent to each other or are you deleting a word?  Are you <em>really</em> moving down 12 lines or are you going to the next paragraph?</li>
<li>Stop thinking about characters and lines.  Think in words, sentences, paragraphs, tokens, blocks.  You are learning the Weirding Way, here.  Visualize where you want the cursor to go, and make it go there.  If you repeated a keystroke to do it, <em>try harder</em>.</li>
</ul>


<p>Eventually, you will start to discover things you want to improve about your setup.  Almost always, they can be fixed by mapping new commands or adjusting configuration.  The Vim help is truly amazing.  Read it.  Like a book.</p>

<p>On occasion, you will need more than what you can achieve with just mappings and configuration.  This is when you <em>might</em> benefit from an extension.  You need to know how to easily install (and remove) them.</p>

<h2>Learn to install and remove extensions</h2>

<p>I&#8217;m gonna get prescriptive here. Just do it this way, and when you get your brown belt, you can switch to something else.  Install <a href="https://github.com/tpope/vim-pathogen">pathogen</a> and use that to manage your Vim extensions.  Why?</p>

<p>Your <code>~/.vim</code> directory (as well as the system Vim directory) has a specific structure organized by a file&#8217;s meaning <em>to Vim</em>.  For example, all syntax files go in one place, and all
help files go in one place, etc.  This means that installing extensions using stock Vim results in a smattering of files all over the place related by Vim function and not by semantic function.  All the Ruby-related files are not in a directory called <code>ruby</code>.  It&#8217;s not good, but you can&#8217;t expect a 30+ year old editor to have got everything right the first time.</p>

<p>Pathogen solves this by allowing each extension to have its own Vim-like directory structure completely separate from all others.  This is just like a &#8220;bundle&#8221;-type system in more modern editors.  This means you can easily add and remove extensions with just a few commands.</p>

<p>Here&#8217;s how to set it up:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&gt; mkdir -p ~/.vim/autoload ~/.vim/bundle
</span><span class='line'>&gt; curl -Sso ~/.vim/autoload/pathogen.vim \
</span><span class='line'>    https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim</span></code></pre></td></tr></table></div></figure>


<p>Put this at the top of <code>~/.vimrc</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>execute pathogen#infect()
</span><span class='line'>syntax on
</span><span class='line'>filetype plugin indent on</span></code></pre></td></tr></table></div></figure>


<p>This gives you a system in which to manage extensions.  Test it by doing this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&gt; cd ~/.vim/bundle
</span><span class='line'>&gt; git clone git://github.com/nanotech/jellybeans.vim.git jellybeans.vim
</span><span class='line'>&gt; vim some_file</span></code></pre></td></tr></table></div></figure>


<p>Now, in Vim, do <code>:colorscheme jellybeans</code> and you should see your colors change (or at least you shouldn&#8217;t get an error).</p>

<p><em>Do not manage plugins by typing <code>git</code> commands</em>.  That was just a test.  We&#8217;re here to improve things and the way you do that is with a <em>command line app</em>.  When you pass the Jedi trials (or cut Darth Maul in half), you can do something fancier, but this at least keeps you from typing a bunch of <code>git</code> commands:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;optparse&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;fileutils&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;open-uri&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="n">opts</span> <span class="o">=</span> <span class="no">OptionParser</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">o</span><span class="o">|</span>
</span><span class='line'>  <span class="n">o</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="s2">&quot;--clean&quot;</span><span class="p">,</span><span class="s2">&quot;Delete everything before re-cloning&quot;</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">options</span><span class="o">[</span><span class="ss">:clean</span><span class="o">]</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">opts</span><span class="o">.</span><span class="n">parse!</span>
</span><span class='line'>
</span><span class='line'><span class="n">git_bundles</span> <span class="o">=</span> <span class="o">[</span>
</span><span class='line'>  <span class="s2">&quot;git://github.com/nanotech/jellybeans.vim.git&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;git://github.com/tpope/Vim-vividchalk.git&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="c1"># add more here</span>
</span><span class='line'><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="n">bundles_dir</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">),</span> <span class="s2">&quot;bundle&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="no">FileUtils</span><span class="o">.</span><span class="n">cd</span><span class="p">(</span><span class="n">bundles_dir</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="n">options</span><span class="o">[</span><span class="ss">:clean</span><span class="o">]</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;Trashing everything (lookout!)&quot;</span>
</span><span class='line'>  <span class="no">Dir</span><span class="o">[</span><span class="s2">&quot;*&quot;</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">d</span><span class="o">|</span>
</span><span class='line'>    <span class="no">FileUtils</span><span class="o">.</span><span class="n">rm_rf</span> <span class="n">d</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">git_bundles</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">url</span><span class="o">|</span>
</span><span class='line'>  <span class="n">dir</span> <span class="o">=</span> <span class="n">url</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">last</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sr">/\.git$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="k">if</span> <span class="no">File</span><span class="o">.</span><span class="n">exists?</span><span class="p">(</span><span class="n">dir</span><span class="p">)</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;  Skipping </span><span class="si">#{</span><span class="n">dir</span><span class="si">}</span><span class="s2">, as it already exists&quot;</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;  Unpacking </span><span class="si">#{</span><span class="n">url</span><span class="si">}</span><span class="s2"> into </span><span class="si">#{</span><span class="n">dir</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="sb">`git clone </span><span class="si">#{</span><span class="n">url</span><span class="si">}</span><span class="sb"> </span><span class="si">#{</span><span class="n">dir</span><span class="si">}</span><span class="sb">`</span>
</span><span class='line'>    <span class="k">if</span> <span class="vg">$?</span><span class="o">.</span><span class="n">success?</span>
</span><span class='line'>      <span class="no">FileUtils</span><span class="o">.</span><span class="n">rm_rf</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dir</span><span class="p">,</span> <span class="s2">&quot;.git&quot;</span><span class="p">))</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="no">STDERR</span><span class="o">.</span><span class="n">puts</span> <span class="s2">&quot;Problem with </span><span class="si">#{</span><span class="n">url</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>I keep this file in <code>update_bundles</code> in my <code>~/.vim</code> directory (which I keep in version control).  Since this is an <a href="http://www.pragprog.com/titles/dccar">awesome command-line app</a>, you can do <code>./update_bundles -h</code> and get some help.  Start off with <code>./update_bundles --clean</code>.  This will delete your one bundle and re-clone it, along with a second bundle. That&#8217;s the &#8220;vividchalk&#8221; colorscheme, which I&#8217;m not recommending per se, but it&#8217;s just a second thing you can use to check that you&#8217;ve got everything set up properly.  Do that via <code>:colorscheme vividchalk</code>.</p>

<p>To add extensions, place them inside the <code>git_bundles</code> hash and run <code>./update_bundles</code>.  To remove extensions, remove them from that hash and delete their cloned directory in <code>~/.vim/bundle</code>.  That&#8217;s it.</p>

<p>Of course, with great power, comes great responsibility…to not junk up your bundles
directory with a bunch of stupid plugins you really don&#8217;t need to write code better.</p>

<h2>Add extensions only when needed</h2>

<p>I&#8217;m not saying you shouldn&#8217;t experiment and explore, but you will get the most benefit from Vim by <em>not</em> installing plugins that re-create features of the degenerate editor you are trying to get away from.  You&#8217;re leaving it for a reason.  Install plugins to solve editing and workflow problems you can&#8217;t get around with what vim gives you.</p>

<p>For example, a lot of people install NERDTree because they like seeing the world&#8217;s most difficult-to-use tree control from Windows Explorer right there in
ASCII-art form in Vim.  It turns out that controls like this were designed for locating files in a directory structure using a mouse on Windows 95.</p>

<p>If you think you need this plugin, you may not have thought deeply about the problem you are facing.  Your problem is likely <em>not</em> &#8220;I need to navigate my project by tree structure and have it constantly there always even though I spend most of my time reading and writing particular code files&#8221;.  Your problem is &#8220;I need to open a particular file more easily&#8221;.  Vim has many ways to do that that are far better.</p>

<p>The most degenerate way is to do <code>:e .</code> which brings up a file navigator in the current directory.  You can navigate the file system <em>with Vim</em>, which is great, but this is still not very efficient.  A better way is to read about <code>gf</code> or <code>:find</code>, or look into the <code>CommandT</code> extension.  All of these allow you to quickly find a file by name or path just by typing.  Typing is fast as hell.</p>

<p>This is just an example, and it&#8217;s meant to illustrate that you should install extensions to solve <em>problems</em>, not to replicate <em>features of other editors</em>.  Sometimes they will be same, but often they will not be.</p>

<p>To find plugins, search GitHub.  Do not use Google, use GitHub.  If you find a plugin that you cannot install by having <code>update_bundles</code> clone it into your <code>.vim/bundle</code> directory, you might not be searching GitHub.  Or, you have a found a plugin that isn&#8217;t being maintained and you should avoid using it.  Or, you should clone it to GitHub and start maintaining it.</p>

<p>As you get more comfortable, start using Vim for coding.  It will be painful, but at least you&#8217;ll know how to navigate the project, copy &amp; paste, and have some grasp of what&#8217;s going, thanks to the grounding in first principles you got while editing plain text files.</p>

<p>Find the plugin for the programming language you are using.  Read the help to see what it offers and, if it looks useful, install it.  You&#8217;ll likely want it just to get the syntax highlighting and indentation stuff working.</p>

<p>Finally, share what you&#8217;ve learned with other Vim users.  Especially if they know more Vim than you.  Those conversations will go like this:</p>

<ul>
<li><em>You</em>: &#8220;Hey, did you know about <code>#</code>?  It searches <em>backward</em> for the word under the cursor!!&#8221;</li>
<li><em>Vim Master</em>: &#8220;Yes!  I love that command.  Do <strong>you</strong> know about <code>?</code>?  It repeats your last search backward.  <code>n</code> does the same forward!&#8221;</li>
</ul>


<p>And then you learn something.  On occasion, the Vim grand master will learn something from you.  Vim just keeps on giving.  It&#8217;s like that.  Vim users are never short of a few tips to share, and as smug as they are around Emacs users, and as arrogant as they are around <em>IDE</em> users, they will be super-kind to anyone learning Vim.</p>

<p>So, go forth and switch! Run stock, then ease into it, then learn about pathogen, and then start leveling up.
The next time you find yourself in Microsoft Word staring at a row of j&#8217;s, you&#8217;ll know you&#8217;ve made the switch.</p>

<hr />

<div class="footnotes">
    <ol>
        <li id='fn:1'>Yes, I realize most call it “normal” mode.  It <strong>should be</strong> the mode you are normally in.  That&#8217;s the point, but I call it command mode, and I really wanted to make a Glengarry Glen Ross reference. <a href='#fnref:1' rev='footnote'>↩</a></li>
    </ol>
</div>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Painful World of JavaScript Testing]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/04/10/the-painful-world-of-javascript-testing.html"/>
    <updated>2013-04-10T10:40:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/04/10/the-painful-world-of-javascript-testing</id>
    <content type="html"><![CDATA[<p>One of the main reasons I like working test-first is because I&#8217;m lazy.  I don&#8217;t want to fire
up a browser, log in as the right person, navigate to just the right piece of data (which I
had to set up manually in the database), and click around to see if things are working.</p>

<p>Don&#8217;t get me wrong, I <em>do</em> do that, but only when the code is working according to my tests.  Or, when I have to write
JavaScript.</p>

<p>At my <a href="http://www.naildrivin5.com/blog/2013/02/19/stitch-fix-slash.html">current job</a>, I&#8217;m doing a lot more front-end than I had been, and so more
JavaScript.  The app I&#8217;m working on is a Rails app, and so I looked into the current state of
the art with testing JavaScript.</p>

<p>In <em>can</em> be done, and it&#8217;s painful.</p>

<!-- more -->


<p>I have a few constraints or requirements for testing:</p>

<ul>
<li>I want to write CoffeeScript.  Every time I have to type <code>function() { }</code> is a part of my
life I won&#8217;t get back.</li>
<li>I want the tests to run from <code>rake</code> without having to open a browser or navigate to some
magic page that happens to run tests.</li>
<li>I want to test every bit of logic I can, including logic involving JQuery.  Basically, I
want to know from my tests if the JavaScript code has broken.</li>
</ul>


<p>The tools exist to do all of this, however they are amazingly degenerate compared to what was
available in Java 10 years ago.  We&#8217;ll get to that.</p>

<h2>Jasmine</h2>

<p>The current state-of-the art seems to be <a href="http://pivotal.github.com/jasmine">Jasmine</a>.  Our Rails app uses RSpec, and Jasmine is
very much along those lines.  You write your &#8220;spec&#8221; and then call assertions in the form
<code>expect(foo).toBe(bar)</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">describe</span> <span class="s1">&#39;my math library&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">describe</span> <span class="s1">&#39;can add&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">it</span> <span class="s2">&quot;should handle 0&quot;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>      <span class="nx">expect</span><span class="p">(</span><span class="nx">SuperMath</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">0</span><span class="p">)).</span><span class="nx">toBe</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
</span><span class='line'>    <span class="nx">it</span> <span class="s2">&quot;can handle negatives&quot;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>      <span class="nx">expect</span><span class="p">(</span><span class="nx">SuperMath</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="o">-</span><span class="mi">5</span><span class="p">)).</span><span class="nx">toBe</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Wonderful!  There&#8217;s lots of useful matchers and, with CoffeeScript, the code is pretty
readable.  The output is very RSpec-like.</p>

<p>The first step to get this working is <a href="https://github.com/searls/jasmine-rails">jasmine-rails</a>.  Jasmine-rails is mostly a
wrapper around the Jasmine JS code, and a simple engine you can mount to run the tests
in-browser.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">gem</span> <span class="s1">&#39;jasmine-rails&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will bootstrap a configuration file for you, and this is where the pain starts.  The
configuration is not well documented or well designed, so it&#8217;s hard to understand exactly
where Jasmine is going to look for files.  Further, Jasmine itself provides zero help when
things don&#8217;t work. Basically, it just shows 0 specs and declares success.  Add in the asset
pipeline and if things don&#8217;t &#8220;just work&#8221;, it&#8217;s no fun.</p>

<p>I eventually got something working through trial and error.  I would highly recommend starting
with a spec that doesn&#8217;t require any of your actual JS files to work, e.g.
<code>expect(true).toBe(true)</code> and so forth.  Once that&#8217;s working, make assertions about the
<em>existence</em> of your actual JS code before going ahead with real tests.</p>

<p>Painful setup is annoying, but it&#8217;s a one-time thing.  Once I had this going, I needed to
write some tests.</p>

<p>I have a feature where entering a number into a text field and hitting return causes certain behavior
to happen on the page.  If the number is &#8220;valid&#8221;, a radio button gets checked, and if it&#8217;s not, an error
message gets shown.</p>

<p>Implementing this is pure JQuery, and I immediately felt frozen - how the heck am I gonna test this?
Mock JQuery?  And if I <em>do</em>, I&#8217;d just end up testing the implementation, e.g. &#8220;assert that JQuery
called <code>hide()</code> on an element with selector &#8216;foo&#8217;&#8221; and so on.  Not good.</p>

<p>Enter jasmine-jquery.</p>

<h2>Fixtures with jasmine-jquery</h2>

<p><a href="https://github.com/velesin/jasmine-jquery">Jasmine-jquery</a> includes a bunch of matchers that help with JQuery-specific behavior, things
like <code>expect($("#whatever")).toBeHidden()</code>.  This is useful, but I&#8217;d still need to find some
way to load up the page and execute the JQuery-based code on a DOM.</p>

<p>I could certainly do that in a Capybara test, but those are slow and flaky.  I need a better way to control the markup that my
JQuery executes on during a test.</p>

<p>A bit of code has inputs, which we arrange as part of our test, and we check the outputs of the code to see that it&#8217;s working.
The fewer the inputs, the easier it is to test something.  This is why functional programming is so appealing - functions tend
to have fewer inputs than methods on an object (which have both their arguments and the internal state of the object as inputs).  Well, JQuery-based JavaScript basically has &#8220;every piece of markup on the Internet&#8221; as its input.</p>

<p>Practically speaking, it has as input &#8220;all the markup on the page&#8221;, which is still a lot of inputs.  I needed a way to both specify the inputs, but also to clearly document what parts of any page are the <em>real</em> inputs. Fixtures is a way to do that.  Essentially, fixtures in jasmine-jquery are bits of HTML that will be available to JQuery as the DOM during your test.</p>

<p>At first, I attempted to place the fixtures in external files.  This seems logical, but it
requires the application to serve them up.  Outside of the fact that our application requires
authentication to every page, the headless version (below) just didn&#8217;t work at all when fixtures were in external files.</p>

<p>Carrying on the tradition of Jasmine and JavaScript in general, when things didn&#8217;t work, they
just silently didn&#8217;t work, with no way of diagnosing what was going on.</p>

<p>So, inline fixtures it is.  In other words, big strings of HTML.</p>

<p>First, we set up the spec:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">describe</span> <span class="s1">&#39;returns&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">describe</span> <span class="s1">&#39;show&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">describe</span> <span class="s1">&#39;doing an item lookup&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Next, we set up our fixture in a <code>beforeEach</code> block using CoffeeScript&#8217;s handy multi-line
string syntax.  This keeps things fairly readable, despite the fact that we&#8217;re building a
giant string of HTML:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">beforeEach</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nv">fixture = </span><span class="s2">&quot;&quot;&quot;</span>
</span><span class='line'><span class="s2">  &lt;div id=&quot;</span><span class="nx">returns</span><span class="o">-</span><span class="nx">fixture</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;div id=&quot;</span><span class="nx">flash</span><span class="o">-</span><span class="nx">alert</span><span class="s2">&quot; class=&quot;</span><span class="nx">alert</span><span class="s2">&quot; style=&quot;</span><span class="nv">display: </span><span class="nx">none</span><span class="p">;</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">      &lt;div class=&quot;</span><span class="nx">msg</span><span class="s2">&quot;&gt;&lt;/div&gt;</span>
</span><span class='line'><span class="s2">    &lt;/div&gt;</span>
</span><span class='line'><span class="s2">    &lt;div id=&quot;</span><span class="nx">flash</span><span class="o">-</span><span class="nx">notice</span><span class="s2">&quot; class=&quot;</span><span class="nx">alert</span><span class="s2">&quot; style=&quot;</span><span class="nv">display: </span><span class="nx">none</span><span class="p">;</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">      &lt;div class=&quot;</span><span class="nx">msg</span><span class="s2">&quot;&gt;&lt;/div&gt;</span>
</span><span class='line'><span class="s2">    &lt;/div&gt;</span>
</span><span class='line'><span class="s2">    &lt;form id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="nx">id</span><span class="o">-</span><span class="nx">lookup</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;input type=&quot;</span><span class="nx">text</span><span class="s2">&quot; id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="nx">id</span><span class="o">-</span><span class="nx">field</span><span class="s2">&quot; name=&quot;</span><span class="nx">item_id</span><span class="s2">&quot; autofocus=&quot;</span><span class="nx">autofocus</span><span class="s2">&quot;&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="k">in</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">In</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="nx">out</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">Out</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="nx">dmg</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">Damaged</span> <span class="o">-</span> <span class="nx">Reparable</span><span class="s2">&quot;&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">2</span><span class="o">-</span><span class="k">in</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">2</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">In</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">2</span><span class="o">-</span><span class="nx">out</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">2</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">Out</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;input id=&quot;</span><span class="nx">item</span><span class="o">-</span><span class="mi">2</span><span class="o">-</span><span class="nx">dmg</span><span class="s2">&quot; type=&quot;</span><span class="nx">radio</span><span class="s2">&quot; name=&quot;</span><span class="k">return</span><span class="p">[</span><span class="nx">items</span><span class="p">][</span><span class="mi">2</span><span class="p">][</span><span class="nx">status</span><span class="p">]</span><span class="s2">&quot; value=&quot;</span><span class="nx">Damaged</span> <span class="o">-</span> <span class="nx">Reparable</span><span class="s2">&quot;&gt;</span>
</span><span class='line'><span class="s2">    &lt;/form&gt;</span>
</span><span class='line'><span class="s2">  &lt;/div&gt;</span>
</span><span class='line'><span class="s2">  &quot;&quot;&quot;</span>
</span><span class='line'>  <span class="nx">jasmine</span><span class="p">.</span><span class="nx">getFixtures</span><span class="p">().</span><span class="nx">set</span><span class="p">(</span><span class="nx">fixture</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">window</span><span class="p">.</span><span class="nx">StitchFix</span><span class="p">.</span><span class="nx">controllers</span><span class="p">.</span><span class="nx">returns</span><span class="p">.</span><span class="nx">show</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>(When editing this article,I noticed that Pygments highlighted the inline HTML.  I wish Vim did that, it makes the fixture
 pretty readable!)</p>

<p>The second-to-last line instructs Jasmine to magically do some magic and make the HTML available to
JQuery.  I don&#8217;t know what part of this is Jasmine and what part is
jasmine-jquery, but it works.  The last line is some setup that gets called by our global JS for the particular page this JS is
for.</p>

<p>This markup is somewhat duplicated from the actual ERB templates, but what can you do?  If
someone changed the ids on me, my tests will pass, but the app breaks.  It&#8217;s all about
compromise, and this seems like a decent compromise.  These are unit tests, and I have to have
some assumptions about the inputs.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">describe</span> <span class="s1">&#39;should check the proper radio button when the id is submitted&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">beforeEach</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#item-id-field&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="s2">&quot;1&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">it</span> <span class="s1">&#39;when there was no previous alert message&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#item-id-lookup&quot;</span><span class="p">).</span><span class="nx">submit</span><span class="p">()</span>
</span><span class='line'>    <span class="nx">expect</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#item-1-in&quot;</span><span class="p">)).</span><span class="nx">toBeChecked</span><span class="p">()</span>
</span><span class='line'>    <span class="nx">expect</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#item-id-field&quot;</span><span class="p">)).</span><span class="nx">toHaveValue</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="nx">waitForAnimations</span> <span class="o">-&gt;</span>
</span><span class='line'>      <span class="nx">expect</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#flash-notice&quot;</span><span class="p">)).</span><span class="nx">toBeVisible</span><span class="p">()</span>
</span><span class='line'>      <span class="nx">expect</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#flash-notice .msg&quot;</span><span class="p">)).</span><span class="nx">toHaveText</span><span class="p">(</span><span class="s2">&quot;Item 1 marked as &#39;In&#39;&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>That last bit was really fun.  We changed the hiding logic to do an animation.  When we did
that, our expectations fired before the animations had completed, making the test fail.  So,
  we have to litter our test with this crud to get consistency.  Here&#8217;s <em>that</em> function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">waitForAnimations</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">andThen</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;:animated&quot;</span><span class="p">).</span><span class="nx">promise</span><span class="p">().</span><span class="nx">done</span><span class="p">(</span><span class="nx">andThen</span><span class="p">)</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Yup, it&#8217;s in JavaScript, because a) I don&#8217;t know how to make global functions in CoffeeScript
that works in this weird context of running tests and b) a class created here to hold the
function wasn&#8217;t visible to my specs.  I&#8217;m sure this is a CoffeeScript thing, but it&#8217;s still
annoying.</p>

<p><em>But</em>, things are working. Now, let&#8217;s get it running headless.</p>

<h2>No browser was used in the execution of this test</h2>

<p>Jasmine-rails includes <a href="http://johnbintz.github.io/jasmine-headless-webkit">jasmine-headless-webkit</a> which, if you install QT properly on your Mac, will run these tests
without a browser, on the command line, via <code>rake</code>, just like you&#8217;re supposed to in the 21st
century.  It even sets up the rake task: <code>rake jasmine:headless</code>.  Not much of a name, but it works.</p>

<p>It&#8217;s slow, to be sure, but not nearly as slow as running it in the browser, <em>plus</em> it works on CI.  The planets must be aligned
inside my astrological sign.</p>

<p>It was a long, annoying trip to get here, but we finally have something sane to run tests in a pretty reasonable
way, and I only had to type <code>function() {}</code> <em>one time</em>, we don&#8217;t have to mock JQuery and it&#8217;s all happening
on the command line, where proper software development occurs.</p>

<p>Of course, all of this was done to code already written.  I want
to use my tests to drive the writing of code, and this is where the situation absolutely
sucks.</p>

<h2>Failure is the only option</h2>

<p>One thing that&#8217;s super-important about writing tests first is watching the code fail in a
specific way.  If I call the method <code>foobar</code>, I want my test failure to be because that method
doesn&#8217;t exist.  This way, when the test <em>does</em> pass, I know that it had to correct affect on
the codebase.</p>

<p>In some cases, this works OK.  Let&#8217;s change our spec above to expect <code>#item-id-field</code>  to have
the value &#8220;foo&#8221;:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">expect</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#item-id-field&quot;</span><span class="p">)).</span><span class="nx">toHaveValue</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The test failure message is very nice:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">Expected</span> <span class="s1">&#39;&lt;input type=&quot;text&quot; id=&quot;item-id-field&quot; name=&quot;item_id&quot; autofocus=&quot;autofocus&quot;&gt;&#39;</span>
</span><span class='line'><span class="nx">to</span> <span class="nx">have</span> <span class="nx">value</span> <span class="s1">&#39;foobar&#39;</span><span class="p">.</span> <span class="p">(</span><span class="nx">line</span> <span class="o">~</span><span class="mi">36</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The failure message is accurate, as is the line number.  So far, so good.</p>

<p>Now, let&#8217;s introduce a typo.  It happens, and, while annoying, is usually an easy problem to fix.
Not in the world of JavaScript unit testing:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="p">(</span><span class="err">/Users/davec/Projects/spectre/spec/javascripts/returns_spec.coffee:33)</span>
</span><span class='line'>   <span class="nv">TypeError: </span><span class="s1">&#39;undefined&#39;</span> <span class="o">is</span> <span class="o">not</span> <span class="nx">a</span> <span class="nx">function</span>
</span></code></pre></td></tr></table></div></figure>


<p>Umm, OK?  I haven&#8217;t shown you where the typo is, and in a potentially large codebase,
you might have a lot of code to look through.  Let&#8217;s use <strong>the only information we were
given</strong> and head to line 33 of our spec:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">it</span> <span class="s1">&#39;when there was no previous alert message&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Not only is there no typo here, but this <strong>isn&#8217;t even the line of code that was
executed that resulted in the error!</strong>   Basically, at this point, we have two problems,
one practical, and one more philosophical.</p>

<p>Practically speaking, we now have to just read through our source code looking for the typo.  If we can&#8217;t see one, we have to start commenting out code until we get a different
error message and then slowly comment it back in.  In 2013.  We put a man on the freakin&#8217; moon
in 1969, and JavaScript, the language of the web, has no stack traces.  This is why we can&#8217;t
have nice things.</p>

<p>On a philosophical level, it also means my ability to test-drive my JavaScript code is
severely hampered.  When starting out a new bit of code, I&#8217;m gonna have a lot of typos and unknown functions.  With test failure messages that amount to &#8220;your code <a href="http://www.urbandictionary.com/define.php?term=asplode">a splode</a>&#8221;, it&#8217;s really hard to do that.</p>

<p>Want to see what the typo was?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nx">notifications</span><span class="p">.</span><span class="nx">close_noow</span><span class="p">(</span><span class="s2">&quot;alert&quot;</span><span class="p">)</span>
</span><span class='line'><span class="nx">notifications</span><span class="p">.</span><span class="nx">close_now</span><span class="p">(</span><span class="s2">&quot;notice&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I fat-fingered the function <code>close_now</code>.  So, not only did it not point me to <em>any relevant
line of code</em> (and I&#8217;d be happy with the line of code in the generated JavaScript), it <strong>didn&#8217;t
even tell me the name of the missing symbol</strong>.</p>

<p>Yes, yes, I know that what it was trying to do was execute a function call on the value
<code>"close_noow"</code> from the <code>notifications</code> objects, which happened to be undefined.  But, can&#8217;t we do
better?</p>

<p>Interestingly, Google&#8217;s JavaScript runtime, V8, does a bit better, which isn&#8217;t surprising
(thought it still <em>boggles my mind</em> that v8 has no readline support.  You can&#8217;t even up
 arrow?!??!).  But, installing therubyracer and instructing Jasmine to use it (or Node) as the JS runtime
has no affect on the crappiness of this error message.</p>

<p>So, this is the state of things to my ability to find them.  I <em>hope</em> I&#8217;ve missed something,
but I fear I haven&#8217;t.  Just piecing this together via various searches and form posts was tricky, which means
that very few people are actually doing this in earnest.</p>

<p>It&#8217;s no wonder, because it&#8217;s a huge pain in the neck.  I can only assume
that I&#8217;ve created a ticking time-bomb in my application and, six months from now, it&#8217;s going
to go off and CI will just fail constantly with &#8220;undefined is not a function&#8221; or something.</p>

<p>I don&#8217;t have a particular opinion on Node, but I can tell you that if developing Node is like
this, I would <em>never</em> do it.  This is no way to work.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[&dagger; Technological Conservatism]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/04/08/technological-conservatism.html"/>
    <updated>2013-04-08T14:09:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/04/08/technological-conservatism</id>
    <content type="html"><![CDATA[<p>John Siracusa has, in his laser-focused, analytical style, perfectly captured something about technology that I always knew, but never <em>knew</em> I knew.   His latest post, <a href="http://hypercritical.co/2013/04/07/technological-conservatism">Technological Conservatism</a> is a must read:</p>

<blockquote><p>Beneath what seems like a reasonable feature request lurks the heart of technological conservatism: <strong>what was and is always shall be.</strong></p></blockquote>

<p>(emphasis his).</p>

<p>I love everything about this, and it&#8217;s a helluva lot better than [Steve Yegge&#8217;s stinker][yegge].</p>

<p>Siracusa&#8217;s article inspires me in two ways:</p>

<ul>
<li>Stop holding &#8220;what I know&#8221; so dearly.  &#8220;Lead or bleed&#8221;, as <a href="http://pragprog.com/book/cfcar2/the-passionate-programmer">Chad Fowler says</a></li>
<li>Stop accepting unjustified idioms &amp; conventions as being &#8220;more correct&#8221;.  &#8220;That&#8217;s just the way it&#8217;s done&#8221; is often the best
explanation for doing things a particular way, but it&#8217;s not actually a legitimate reason for anything.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What RSS Means to Me]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/03/18/what-rss-means-to-me.html"/>
    <updated>2013-03-18T08:00:00-04:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/03/18/what-rss-means-to-me</id>
    <content type="html"><![CDATA[<p>With the recent announcement that Google is shutting down Reader, there&#8217;s been a lot of talk about RSS: Is it just a geek-only thing?  Isn&#8217;t
Twitter good enough?  Why don&#8217;t you read &#8220;real&#8221; journalists instead of blogs?</p>

<p>RSS is a huge part of my online life - every time I fire up Reeder and see stories, I get very happy.  The way I use RSS (which I believe is the best way to use it) can&#8217;t be replaced by anything else that I&#8217;m aware of.</p>

<p>So, why do I think RSS is so great?</p>

<!-- more -->


<p>First, I don&#8217;t use RSS readers to &#8220;discover&#8221; anything - new blogs, what people are reading, etc.  That&#8217;s what Twitter is for.  When Google
added that feature to Reader, it became <em>really</em> annoying to clear all that crap out.</p>

<p>Instead, I view my RSS feeds as a curated, frequently updated, bookshelf just for me.
I have a list of carefully selected websites whose posts I want to read <em>all</em>
of, on a daily (or more) basis.  These websites conform to the type of reading
I like - fewer, higher quality posts, as opposed to link-bait/page-view whores
like Mashable or Engadget.</p>

<p>I&#8217;m not saying sites like Cinematical or Joystiq are worthless - there are some
great posts on there, but Twitter is sufficient to alert me to them, and I&#8217;m
rarely sad if I miss the few good posts out of the massive cruft on those
sites (I mean, do I <em>really</em> care about how many megapixels are on Sony&#8217;s
latest point-and-shoot, and do I <em>really</em> need a picture of the catering
tray at the Arrested Development shoots?).</p>

<p>It&#8217;s a shame how these sites are run, because they <em>do</em> employ connected journalists who get real scoops and write great stories. But, their business models require a massive number of posts per day and so I just can&#8217;t trim the wheat from the chaff, so I don&#8217;t visit.  Not sure if I&#8217;m alone in this.</p>

<p>So, what <em>do</em> I read?  Here&#8217;s what&#8217;s in my RSS feeds.  These are the sites I
read daily and, because this is the 21st century, I don&#8217;t want to navigate to
them one by one - I want software delivering them to me, and that&#8217;s what RSS,
and a great client, do.</p>

<h2>Comics</h2>

<ul>
<li>Dilbert - duh</li>
<li>Eye on Springfield - Screencaps from the Simpsons.  Always brings a smile.</li>
<li>Joy of Tech - Nerdy, Apple-related humor</li>
<li>xkcd - double duh</li>
<li>Thrillbent/Insufferable - Mark Waid is a genius.</li>
</ul>


<h2>Tech</h2>

<ul>
<li>Coding Horror - classic programming blog (that, honestly, is not as good as it was).</li>
<li>Daily WTF - duh.</li>
<li>Daring Fireball - quality writing and analysis by John Gruber about Apple and related stuff.  And plenty of snark.</li>
<li>Marco.org - by the creator of Instapaper and The Magazine, it&#8217;s like Daring Fireball Lite but with a bit more variety.</li>
<li>Michael Church&#8217;s blog (though I&#8217;ve skipped his recent <em>nine</em> treatises on workplace incompetence - he needs an editor).</li>
<li>Signal vs. Noise - 37 Signals blog isn&#8217;t as great as it used to be, but still some interesting stuff.</li>
<li>Github&#8217;s blog - Gotta keep up with one of the most amazing programmer tools ever made.</li>
<li>Giles Bowkett&#8217;s blog - insightful posts about Rails and other stuff.</li>
</ul>


<h2>Funtimes</h2>

<ul>
<li>Scott&#8217;s Blog of Doom - Pro Wrestling recaps and snark.  Yes, I like Pro Wrestling and yes, I have a sense of humor about it.</li>
<li>Wil Wheaton&#8217;s Blog - I came for his hilarious recaps of TNG episodes, I stay for his great writing, obsessive honesty and
general awesomeness.</li>
<li>Meeting Boy - daily quips from someone always stuck in a meeting.</li>
<li>Clients from Hell - daily reminder of why I don&#8217;t want to do consulting ever again.</li>
<li>Arrested Westeros - Arrested Development quotes atop Game of Thrones screencaps.  Gold.</li>
<li>Liquorious - drink recipes.  Yes, I make my own bitters and thus like reading cocktail recipes :)</li>
<li>Fashion It So - incredibly detailed fashion analysis of the costumes on Star Trek: The Next Generation.  I <em>do</em> work at a <a href="http://www.stitchfix.com">fashion startup</a>, you know!</li>
</ul>


<p>What all of these have in common is that they don&#8217;t generate a lot of posts (Daring Fireball is probably the most prolific, and it&#8217;s usually just a few short link posts, with occasional long-form pieces).  The posts on these sites are almost always very high quality - I want to read them and would regret missing them.</p>

<p>I can&#8217;t think of another technology besides RSS (and a great Reader client) that could more easily let me keep up with all of these websites.</p>

<h2>What makes a good reader?</h2>

<p>I&#8217;m currently using <a href="http://reederapp.com">Reeder</a>, which is available on iOS and Mac and it&#8217;s more or less perfect for my needs.  It has a minimal, yet pleasing
design, full keyboard navigation (j/k or GTFO), integration with various services (namely Instapaper) and, because it uses Google Reader as a backend,
keeps me synced everywhere.  It also works great offline, assuming you&#8217;ve downloaded your feeds when you were last online.</p>

<p>I&#8217;m hopeful that Reeder will come up with a new backend and just continue working.  Otherwise, I&#8217;ll be looking for new options.</p>

<p>But RSS is (I hope) <strong>far</strong> from dead.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Is Your DSL Really a Type System?]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/02/28/is-your-dsl-really-a-type-system.html"/>
    <updated>2013-02-28T08:21:00-05:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/02/28/is-your-dsl-really-a-type-system</id>
    <content type="html"><![CDATA[<p>The UserVoice developer blog posted <a href="https://developer.uservoice.com/blog/2013/02/27/introducing-mutations-putting-soa-on-rails-for-security-and-maintainability/">an interesting article</a> yesterday talking about
how they solve &#8220;The Rails Problem&#8221; of complex Rails apps having obese models that stymie code re-use.  The naive approach is just <a href="http://www.naildrivin5.com/blog/2013/01/02/dci-vs-just-making-classes.html">to make classes</a>.</p>

<p>UserVoice&#8217;s approach is different: they made a DSL for describing service calls.  The thing is, it&#8217;s sort
of a type system - and a verbose one at that.</p>

<!-- more -->


<p>UserVoice&#8217;s approach is called <a href="https://github.com/cypriss/mutations">&#8220;mutations&#8221;</a> and it&#8217;s more than just
method calls.  You can specify quite a bit about our service calls, all to make the underlying logic very simple.  For example,
they have a &#8220;user signup&#8221; service and, in the most naive, but safe, way, it would look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">UserSignupService</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">signup</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span><span class="n">email</span><span class="p">,</span><span class="n">birthdate</span><span class="p">,</span><span class="n">newsletter_subscribe</span><span class="o">=</span><span class="kp">false</span><span class="p">)</span>
</span><span class='line'>    <span class="k">raise</span> <span class="s2">&quot;name is required&quot;</span>    <span class="k">if</span> <span class="nb">name</span><span class="o">.</span><span class="n">nil?</span>
</span><span class='line'>    <span class="k">raise</span> <span class="s2">&quot;email is required&quot;</span>   <span class="k">if</span> <span class="n">email</span><span class="o">.</span><span class="n">nil?</span>
</span><span class='line'>    <span class="k">raise</span> <span class="s2">&quot;email must be valid&quot;</span> <span class="k">unless</span> <span class="n">email</span> <span class="o">=~</span> <span class="no">EMAIL_REGEX</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="nb">name</span><span class="p">:</span> <span class="nb">name</span><span class="p">,</span> <span class="n">email</span><span class="p">:</span> <span class="n">email</span><span class="p">,</span> <span class="n">birthdate</span><span class="ss">:birthdate</span><span class="p">)</span>
</span><span class='line'>    <span class="no">NewsletterSubscriptions</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">email</span><span class="p">:</span> <span class="n">email</span><span class="p">,</span> <span class="n">user_id</span><span class="p">:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span> <span class="k">if</span> <span class="n">newsletter_subscribe</span>
</span><span class='line'>    <span class="no">UserMailer</span><span class="o">.</span><span class="n">async</span><span class="p">(</span><span class="ss">:deliver_welcome</span><span class="p">,</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
</span><span class='line'>    <span class="n">user</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">user</span> <span class="o">=</span> <span class="no">UserSignupService</span><span class="o">.</span><span class="n">signup</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span><span class="n">email</span><span class="p">,</span><span class="n">birthdate</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is a very paranoid, but rock solid implementation.  If you screw up calling it, you&#8217;ll know why.  In Mutations, this code
would look like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">UserSignup</span> <span class="o">&lt;</span> <span class="no">Mutations</span><span class="o">::</span><span class="no">Command</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># These inputs are required</span>
</span><span class='line'>  <span class="n">required</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">string</span> <span class="ss">:email</span><span class="p">,</span> <span class="n">matches</span><span class="p">:</span> <span class="no">EMAIL_REGEX</span>
</span><span class='line'>    <span class="n">string</span> <span class="ss">:name</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># These inputs are optional</span>
</span><span class='line'>  <span class="n">optional</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">boolean</span> <span class="ss">:newsletter_subscribe</span>
</span><span class='line'>    <span class="n">date</span> <span class="ss">:birthdate</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># The execute method is called only if the inputs validate. It does your business action.</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">execute</span>
</span><span class='line'>    <span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="n">inputs</span><span class="p">)</span>
</span><span class='line'>    <span class="no">NewsletterSubscriptions</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">email</span><span class="p">:</span> <span class="n">email</span><span class="p">,</span> <span class="n">user_id</span><span class="p">:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span> <span class="k">if</span> <span class="n">newsletter_subscribe</span>
</span><span class='line'>    <span class="no">UserMailer</span><span class="o">.</span><span class="n">async</span><span class="p">(</span><span class="ss">:deliver_welcome</span><span class="p">,</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
</span><span class='line'>    <span class="n">user</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="c1"># ...</span>
</span><span class='line'><span class="n">outcome</span> <span class="o">=</span> <span class="no">UserSignup</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
</span><span class='line'><span class="k">if</span> <span class="n">outcome</span><span class="o">.</span><span class="n">success?</span>
</span><span class='line'>  <span class="c1">#</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'>  <span class="c1">#</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is fairly interesting, as the &#8220;business logic&#8221; (the code in <code>execute</code>) is clean - it&#8217;s just the bare logic.  The sanity
checking and other paranoia is handled by the framework.  Likely that tests of this are simpler as well - you don&#8217;t need to
test the validations.  While this is great, I can&#8217;t help thinking that <a href="http://c2.com/cgi/wiki?GreenspunsTenthRuleOfProgramming">&#8220;every implementation
of parameter validation in Ruby contains an ad-hoc, informally-specified, bug-ridden, slow implementation of a real type system&#8221;</a>.</p>

<p>To demonstrate, here&#8217;s what this class would look like in Scala:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">object</span> <span class="nc">UserSignup</span> <span class="o">{</span>
</span><span class='line'>  <span class="k">def</span> <span class="n">signup</span><span class="o">(</span><span class="n">name</span>                <span class="k">:</span> <span class="kt">String</span><span class="o">,</span>
</span><span class='line'>             <span class="n">email</span>               <span class="k">:</span> <span class="kt">Email</span><span class="o">,</span>
</span><span class='line'>             <span class="n">birthDate</span>           <span class="k">:</span> <span class="kt">Option</span><span class="o">[</span><span class="kt">Date</span><span class="o">],</span>
</span><span class='line'>             <span class="n">newsletterSubscribe</span> <span class="k">:</span> <span class="kt">Boolean</span><span class="o">=</span><span class="kc">false</span><span class="o">)</span> <span class="k">:</span> <span class="kt">User</span> <span class="o">=</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">var</span> <span class="n">user</span> <span class="k">=</span> <span class="nc">User</span><span class="o">.</span><span class="n">create_!</span><span class="o">(</span><span class="n">name</span><span class="o">,</span><span class="n">email</span><span class="o">,</span><span class="n">birthDate</span><span class="o">,</span><span class="n">newsletterSubscribe</span><span class="o">)</span>
</span><span class='line'>    <span class="k">if</span> <span class="o">(</span><span class="n">newsletterSubscribe</span><span class="o">)</span>
</span><span class='line'>      <span class="nc">NewsletterSubscriptions</span><span class="o">.</span><span class="n">create</span><span class="o">(</span><span class="n">email</span><span class="o">,</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="o">)</span>
</span><span class='line'>    <span class="nc">UserMailer</span><span class="o">.</span><span class="n">async</span><span class="o">(</span><span class="-Symbol">&#39;deliver_welcome</span><span class="o">,</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="o">)</span>
</span><span class='line'>    <span class="n">user</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'><span class="k">var</span> <span class="n">user</span><span class="k">:</span><span class="kt">User</span> <span class="o">=</span> <span class="nc">UserSignup</span><span class="o">.</span><span class="n">signup</span><span class="o">(</span><span class="n">name</span><span class="o">,</span><span class="n">email</span><span class="o">,</span><span class="nc">Some</span><span class="o">(</span><span class="n">birthDate</span><span class="o">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&#8217;s it.  No special DSL, no custom framework, nothing. Just the programming language.  Why?</p>

<p>First, we assume that <code>null</code> (Scala&#8217;s analog of <code>nil</code>) is always a bug.  Good Scala programs are designed this way, and it&#8217;s
not that bad to <a href="http://www.naildrivin5.com/blog/2012/07/25/a-world-without-nil.html">program without null</a>, so a declaration like <code>name:String</code> in Scala means &#8220;name is a required
parameter&#8221;.</p>

<p>Second, optional parameters use the <code>Option</code> type to indicate their optionality.</p>

<p>Next, for validating our email, we use the type system.  Instead of using a <code>String</code> for storing email addresses (the
hallmark of every <a href="http://www.globalnerdy.com/2010/05/09/new-programming-jargon/">stringly typed</a> application), we require an instance of <code>Email</code>.  We might imagine it looks like
this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">class</span> <span class="nc">Email</span><span class="o">(</span><span class="k">var</span> <span class="n">emailAddress</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="o">(!</span><span class="nc">EMAIL_REGEX</span><span class="o">.</span><span class="n">matches</span><span class="o">(</span><span class="n">emailAddress</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">throw</span> <span class="k">new</span> <span class="nc">InvalidInputError</span><span class="o">(</span><span class="s">&quot;Email address isn&#39;t valid&quot;</span><span class="o">)</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">var</span> <span class="n">goodemail</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Email</span><span class="o">(</span><span class="s">&quot;dave@foo.com&quot;</span><span class="o">)</span> <span class="c1">// all good</span>
</span><span class='line'><span class="k">var</span> <span class="n">badEmail</span>  <span class="k">=</span> <span class="k">new</span> <span class="nc">Email</span><span class="o">(</span><span class="s">&quot;dave.foo.com&quot;</span><span class="o">)</span> <span class="c1">// exception thrown</span>
</span></code></pre></td></tr></table></div></figure>


<p>So, our <code>UserSignup</code> code can be <strong>absolutely sure</strong> that it gets a valid email.  Validating that email happens elsewhere, as
it should.</p>

<p>Finally, our callsite uses the same method that our class defines.  Under mutations, you define a method called <code>execute</code>, but
you call a method called <code>run</code>.  Both just take a hash, making the callsite somewhat opaque as to what&#8217;s being passed in and
requiring you to know how the framework works in order to piece together what&#8217;s being called. In Scala, you just call the
method that you defined.</p>

<p>There&#8217;s no magic here, no framework, nothing other than idiomatic Scala code.  I like the way it encourages us to create a rich
set of types as opposed to strings and hashtables everywhere.  Types allow us to encode our understanding of the system,
domain, and logic - that&#8217;s what they are for.  Statically checking that those types are used properly is a sanity check that
we&#8217;ve correctly encoded our understanding.</p>

<p>Also note how not-that-verbose the Scala code is, compared to the Ruby code.  The Java equivalent could not make that claim.</p>

<p>Anyway, I think Mutations looks like a cool library, and I plan on checking it out for writing Rails apps.  I did think it was worth pointing out that the problem of separating argument validation from method logic is largely a solved problem - by statically typed languages.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Stitch Fix!]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/02/19/stitch-fix-slash.html"/>
    <updated>2013-02-19T08:18:00-05:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/02/19/stitch-fix-slash</id>
    <content type="html"><![CDATA[<p>Today is my first official day<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup> a <a href="http://www.stitchfix.com">Stitch Fix</a>, where I&#8217;ll be joining the small but awesome engineering team!  What I love about Stitch Fix is that it&#8217;s such a simple business: buy clothes for one price, sell them at a higher price.  It&#8217;s the very essence of what a business does.  &#8220;All&#8221; we have to do is find more people to buy things from us, figure out what they want to buy, and sell to them as efficiently as possible.</p>

<!-- more -->


<p>I got an overview of their operations, and it&#8217;s amazing how well the business seems to work with almost no real automation (which is where the engineering team comes in).  This sort of thing - writing software to make other people&#8217;s jobs vastly simpler - is what I love about programming.  It reminds me of a particular user of the software I was writing for the <a href="http://en.wikipedia.org/wiki/United_States_Marshals_Service">US Marshals Service</a></p>

<p>I&#8217;d been there well over a year, and our requirements came from the government&#8217;s technical project manager.  He was an old timer,
knew the business, and gathered new requirements and features from the users (who were a wide variety of cops, administrators,
and other IT people).  But now, we were getting to meet some real users in a real district office.  We talked to a lot of
different people about how they used the software and, more importantly, what they did at their jobs.</p>

<p>Learning about Stitch Fix&#8217;s operations made me think of one person in particular from this trip.  Her team handled transporting
prisoners from jail to court - when a prisoner&#8217;s court date arrived, the Marshals had to make sure he was in a bus that morning
headed to the courthouse from jail.  Part of this woman&#8217;s job was to assemble that list on paper and hand it to the deputies in
charge of prisoner transport.</p>

<p>Unfortunately, there was no specific feature in the software to generate and print this list.  What she did instead was to call
up the list on screen via a custom query.  This was a VT100 app, so only about 20 prisoners were viewable on the screen at a
time.  She would then <em>print the screen</em>, and scroll to the next page, repeating this printing until she got the entire list.
The result was a stack of papers with 80x24 screenshots printed on them.</p>

<p><em>And then</em> she would <em>cut out the list</em> from each piece of paper before finally <em>taping the lists together</em> and copying it to
hand off to the deputy.  It took her well over an hour each day.  And she wasn&#8217;t the only one doing this - it was the simplest
way to assemble this list.</p>

<p>My colleagues and I were <em>begging</em> to fix this.  It would&#8217;ve taken one of us <em>maybe</em> a day to fully implement, fully test, and
deploy a report that would simply print out the list.  But, it wasn&#8217;t a priority on the project plan, so it never got done. This
was <em>years</em> ago, so I hope this poor woman has retired or that <em>someone</em> has fixed this.</p>

<p>At Stitch Fix, I&#8217;m looking forward to actually <em>solving</em> these sorts of problems.  These really are the reason software is so great - minimal effort &#8220;typing shit into the computer&#8221; can save <em>days</em> for someone else.  Sometimes I think my job is to eliminate the need for tape and scissors.</p>

<hr />

<div class="footnotes">
    <ol>
        <li id='fn:1'>I actually started yesterday, but it was a holiday, so almost no one was in the office (though I <em>did</em> get productive things done :) <a href='#fnref:1' rev='footnote'>↩</a></li>
    </ol>
</div>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What I mean by 'calling out' bad code]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/01/30/what-i-mean-by-calling-out-bad-code.html"/>
    <updated>2013-01-30T16:44:00-05:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/01/30/what-i-mean-by-calling-out-bad-code</id>
    <content type="html"><![CDATA[<p>In my <a href="http://www.naildrivin5.com/blog/2013/01/24/if-you-call-out-bad-code.html">earlier post</a> on how the <a href="https://github.com/harthur/replace">replace</a> command-line app isn&#8217;t &#8220;bad code&#8221;, I said:</p>

<blockquote><p>Now, I&#8217;m all for bad projects and bad code being called out.  Our industry produces some shitty code, and a general lack of
craftsmanship can kill business, or even people.  So bad code needs to be pointed out.</p></blockquote>

<p>The words I&#8217;m using here are a bit loaded, and actually distract from my real meaning.  I don&#8217;t want to change the post, but
thought it was worth explaining what I meant and why it&#8217;s important.</p>

<!-- more -->


<p>Uncle Bob actually weighed in on this debate <a href="http://blog.8thlight.com/uncle-bob/2013/01/30/The-Craftsman-And-The-Laborer.html">in a recent post</a>, and puts it much better:</p>

<blockquote><p>(BTW, There is nothing wrong with politely pointing out what you believe to be deficiencies in someone else&#8217;s code. You don&#8217;t want to be rude or condescending when you do this; but you do want to do it. How else can we learn unless someone points out where we&#8217;ve gone wrong? So please don&#8217;t let this event stop you from valid review and critique.)</p></blockquote>

<p>This is much more precisely what I mean, although this, too, carries the air of &#8220;I, the craftsman, am <em>correct</em>, and you, the
mere code monkey, have <em>much to learn</em>&#8221;.  What I&#8217;m really talking about is code review.</p>

<p>Code review (or code inspection) is one of the few software development techniques whose effectiveness at defect removal has been
remotely proven scientifically.  Anecdotally, it always improves the code under review, and frequently improves the way the code
author and reviewers understand the problem.  There is really no downside to code review.</p>

<p>Although a seasoned veteran developer is going to more quickly and easily identify issues with code under review than a developer
with little or no experience, it <em>doesn&#8217;t</em> mean that the reviewer/reviewee relationship has to go this way - I&#8217;ve had
developers more junior than me identify real issues with code I&#8217;ve written.  No one writes perfectly clean or correct code the
first time andk although tests help, tests only test that the code matches <em>your understanding</em> of the problem.  It doesn&#8217;t take a
20-year senior developer to point out bad naming, poor API design, confusing structure, or a missed item from the requirements.</p>

<p>As to the phrases &#8220;called out&#8221; and &#8220;our industry produces some shitty code&#8221;, I mean this more as a call to arms to experienced
developers.  <em>Make</em> the time to review code - apply your experience to the work done by those who haven&#8217;t walked in your shoes.
Everyone will be better for it.  I wrote a post last year on <a href="http://www.naildrivin5.com/blog/2012/04/02/a-protocol-for-code-reviews.html">how to do a code review</a> that should provide you a
good place to start.</p>

<p>As for unsolicited reviews of open-source software, I don&#8217;t know that trolling Github for &#8220;bad&#8221; code is the best idea, but if you
put your code out there, expect (and embrace) commentary.  Hopefully, it will be in the form of a pull request, but if not, you
can still learn something and improve your code just via conversation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[&#10106;&#10144; What I learned working at LivingSocial]]></title>
    <link href="http://www.naildrivin5.com/blog/2013/01/27/what-i-learned-working-at-livingsocial.html"/>
    <updated>2013-01-27T16:43:00-05:00</updated>
    <id>http://www.naildrivin5.com/blog/2013/01/27/what-i-learned-working-at-livingsocial</id>
    <content type="html"><![CDATA[<p>Friday was my last day at LivingSocial.  I&#8217;m moving on to a small team at a small startup that I hope will eventually be big.  My tenure at LivingSocial was short - it would&#8217;ve certainly been longer had this opportunity not come along - but I learned a lot in the 14 months I worked there.</p>

<p>Sure, I learned stuff about Rails, Ruby, service-oriented architectures, asynchronous processing, and credit card payments.  But
that&#8217;s not what I&#8217;m talking about.  Those skills are great experience and will look just fine on my resume, but I learned two
things that made me a better developer:</p>

<ul>
<li>Deliver results, not promises</li>
<li>Assume <em>everyone</em> knows what they&#8217;re doing</li>
</ul>


<!-- more -->


<h2>Deliver Results, not promises</h2>

<p>At a previous job, we had a well-tuned agile process.  We worked in sprints, with a backlog, stories, and regular releases.  After a while, however, it felt like the team was just producing story points.  Products in development were allowed to be &#8220;in progress&#8221; for weeks or months.  It got to the point where progress itself was a deliverable.</p>

<p>The problem is that promises, plans, and progress reports aren&#8217;t valuable in and of themselves.  Promises don&#8217;t solve business
problems.  Customers don&#8217;t purchase progress reports, and developers can&#8217;t ship code based on un-executed plans.  For whatever
reason, LivingSocial gets this, and gets it deeply.  When Mark Zuckerburg <a href="http://www.ft.com/cms/s/2/a2109a54-4d88-11e1-b96c-00144feabdc0.html#axzz2JHe6EgfY">told Facebook&#8217;s shareholders</a> that &#8220;code wins arguments&#8221;,
I believe this is what he meant.</p>

<p><span class='pullquote-right' data-pullquote='You&#8217;re going to ship smaller features and you&#8217;re going to ship them more quickly. '>
This cultural value leads to what I believe is the true essence of the &#8220;agile&#8221; movement.  Consider for a moment if <em>all</em> you are
as a developer is what you&#8217;ve shipped<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>.  This is going to fundamentally change how you work. You&#8217;re going to ship smaller features and you&#8217;re going to ship them more quickly.  You&#8217;re going to deliver value to the business as quickly and frequently as possible.
</span></p>

<p>Because of this, big projects - which is a project that might take more than a couple months - are unusual at LivingSocial and cause a fair amount of trepidation.  In general, I believe this is a good thing.  Big projects have a way of not shipping.  Big projects have a way of getting bigger and more expensive.</p>

<p>I&#8217;m not saying that big projects are bad or that LivingSocial is incapable of taking on big projects, but we always ask ourselves
if there&#8217;s a way to do a lot less a lot more quickly, and if we can start showing results sooner.  This has not been a common attitude in anywhere else I&#8217;ve worked.</p>

<p>If your organization is truly &#8220;results-oriented&#8221;, then it means that promises, planning, and status reports are treated as
fundamentally different things than shipped product.  This isn&#8217;t to say that planning and progress are bad - LivingSocial
certainly uses a wide variety of project management tools and techniques - but they are only valuable to the extent that they
enable the delivery of results.</p>

<p>This attitude quickly affected everything I did, even down to the way I&#8217;d write email. I stopped immediately responding with &#8220;I&#8217;ll look into it&#8221;, or &#8220;I&#8217;m not sure&#8221;, or &#8220;That should be correct&#8221;.  I started to respect the inboxes of others and responded only when I had a real result (or the name of the person who could get such a result if I could not).  I stopped delivering promises and started delivering results.</p>

<p>Which brings us to the second thing I learned.</p>

<h2>Assume <em>everyone</em> knows what they&#8217;re doing</h2>

<p>Programmers have well-earned reputations as prima-donnas.  While certainly not <em>every</em> programmer develops this arrogance and lack of respect for those who &#8220;can&#8217;t code&#8221;, it&#8217;s unfortunately common.</p>

<p>As a young developer, I was rude and disrespectful to anyone who didn&#8217;t code.  I was allowed to get away with it because I did my job<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup> well and my skills were in high demand.  In my eyes, everyone was guilty until proven innocent.  I even began to apply this to other developers after seeing what government contractors are allowed to get away with.</p>

<p>During my job <em>prior</em> to LivingSocial, I matured a lot in this area.  I was fortunate to work with a great team of developers and with a largely effective, helpful, and talented team of sales and product people.  As the company grew, however, my old attitude crept back - there were just too many people to know intimately, and I started to treat everyone new as incompetent until proven otherwise.</p>

<p>When I started at LivingSocial, this attitude persisted.  The company is huge and, as one of only two developers working on a key
piece of infrastructure, I had to interact with a wide variety of people.  I knew enough to be nice and to be polite, but this
was initially a mask of my lingering bad attitude.</p>

<p>After a while, I started to notice that the stereotypical programmer attitude was not <em>nearly</em> as strong at LivingSocial as anywhere else I had worked.  The relationships between the developers and every other part of the company - customer service, IT, product, accounting - were far healthier than I&#8217;d ever experienced.</p>

<p><span class='pullquote-right' data-pullquote='everyone&#8217;s default assumption was that every member of the team was skilled, useful, and dependable '>
The result was a high functioning team that made decisions rationally, not politically.  Developers were not rude nor disrespectful to their &#8220;non-techie&#8221; counterparts, and, surprise surprise, were treated with respect themselves.  I don&#8217;t know how this evolved, but it seemed that everyone&#8217;s default assumption was that every member of the team was skilled, useful, and dependable - innocent until proven guilty.
</span></p>

<p>As I realized this, and began to adjust my attitude, it became easier and easier to work with other people.  Where in the past I would&#8217;ve dreaded having a meeting with the &#8220;idiot business guy&#8221;, I now went into these situations with an open mind, and a positive attitude - what business problem can I help solve?  Turns out that people who don&#8217;t code are pretty smart and know stuff I don&#8217;t. The end result was that, in any discussion, the best idea would almost always win (and it wasn&#8217;t necessarily the developer&#8217;s :)</p>

<p>I have no idea how to cultivate or maintain this cultural value, but when a team of developers starts with the assumption that
everyone&#8217;s working toward the same goal, and that everyone knows what they&#8217;re doing, the team functions well (and it&#8217;s a lot more
pleasant to work on such a team).</p>

<h2>Bonus third thing</h2>

<p>In July, I wrote about the <a href="http://www.naildrivin5.com/blog/2012/07/30/hungry-academy-graduates.html">Hungry Academy graduates</a>, who were all starting their new lives as LivingSocial developers:</p>

<blockquote><p>The first half of this grand experiment is over and it was a rousing success. The second half - how well they succeed in the actual work environment - begins now. I’m optimistic.</p></blockquote>

<p>It&#8217;s been about six months since I posted that and, in that time, I&#8217;ve had the pleasure of working with many of the graduates.  I don&#8217;t know the ultimate cost of turning these 24 men and women into developers, but they are the most consistently skilled group of junior developers I&#8217;ve worked with.  Hiring someone with little or no experience is always a crapshoot, and you hope that even 50% of your new hires will be this good. LivingSocial was able to produce <em>twenty four</em> of them in five months.</p>

<p>And they aren&#8217;t just code monkeys.  Yes, they can make the computer do things, but their <em>attitudes</em> are amazing.  They don&#8217;t seem saddled with
any of the baggage that myself and my peers seemed to have at that point in our careers.  It gives me hope that the &#8220;prima-donna
developer&#8221; can someday be a thing of the past.  Getting to experience their growth as developers is something I won&#8217;t soon forget, and likely something that very few professional programmers will get to experience.</p>

<h2>Moving on</h2>

<p>So that&#8217;s it.  I&#8217;m moving on to work with some ex co-workers on a small team (more on that later), building a small, successful business into a larger, more successful one.  My paycheck is going to depend on delivering results, and my ability to deliver those results is going to depend on good working relationships with all sorts of people working to make this business succeed.  I honestly don&#8217;t know if this would be possible without my 14+ months at LivingSocial.</p>

<hr />

<div class="footnotes">
    <ol>
        <li id='fn:1'>To be clear, this is certainly not <em>all</em> you are as a developer at LivingSocial.  This is more of a value than a mandate <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>Turns out that a developer&#8217;s job is more than just typing shit into a computer. <a href='#fnref:2' rev='footnote'>↩</a></li>
    </ol>
</div>



]]></content>
  </entry>
  
</feed>
