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

  <title><![CDATA[Samuel Mullen]]></title>
  <link href="http://samuelmullen.com/atom.xml" rel="self"/>
  <link href="http://samuelmullen.com/"/>
  <updated>2013-05-10T22:15:35-05:00</updated>
  <id>http://samuelmullen.com/</id>
  <author>
    <name><![CDATA[Samuel Mullen]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[The Problem with Rails Callbacks]]></title>
    <link href="http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/"/>
    <updated>2013-05-07T10:48:00-05:00</updated>
    <id>http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks</id>
    <content type="html"><![CDATA[<p>If you search <a href="http://stackoverflow.com">StackOverflow</a> for &#8220;Rails callbacks&#8221;,
a large number of the results pertain to seeking means to avoid issuing the
callback in certain contexts. It almost seems as though Rails developers
discover a need to avoid callbacks as soon as they discover their existence.</p>

<p>Normally, this would be a cause for concern, that perhaps the feature should be
avoided altogether or even removed, but callbacks are still part of Rails. Maybe
the problem goes deeper.</p>

<h2>What is a Callback?</h2>

<p>As you likely already know, callbacks are just hooks into an <code>ActiveRecord</code>
object&#8217;s life cycle. Actions can be performed &#8220;before&#8221;, &#8220;after&#8221;, or even &#8220;around&#8221;
<code>ActiveRecord</code> events, such as <code>save</code>, <code>validate</code>, or <code>create</code>. Also, callbacks
are cumulative, so you can have two actions which occur <code>before_update</code>, and
those callbacks will be executed in the order they are occur.</p>

<h2>Where Trouble Begins</h2>

<p><img class="right" src="http://samuelmullen.com/images/pandora.jpg"></p>

<p>Developers usually start noticing callback pain during testing. If you&#8217;re not
testing your <code>ActiveRecord</code> models, you&#8217;ll begin noticing pain later as your
application grows and as more logic is required to call or avoid the callback.</p>

<p>I say, &#8220;developers usually start noticing callback pain during testing&#8221; because
in order to speed up tests or to get them to pass, it becomes necessary to &#8220;stub
out&#8221; the callback actions. If you don&#8217;t stub out the action, then you must add
the supporting data structure, class, and/or logic to each test in order for it
to pass.</p>

<p>Here&#8217;s an example of what I mean:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Post</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:followers</span>
</span><span class='line'>  <span class="n">after_save</span> <span class="ss">:notify_followers</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">publish!</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">published_at</span> <span class="o">=</span> <span class="no">Time</span><span class="o">.</span><span class="n">now</span>
</span><span class='line'>    <span class="n">save</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">notify_followers</span>
</span><span class='line'>    <span class="no">Notifier</span><span class="o">.</span><span class="n">post_mailer</span><span class="o">.</span><span class="n">deliver</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">describe</span> <span class="s2">&quot;publishing the article&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;saves the object with a defined published_at value&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="no">Post</span><span class="o">.</span><span class="n">any_instance</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:notify_followers</span><span class="p">)</span> <span class="c1"># Codey McSmellsalot</span>
</span><span class='line'>    <span class="n">post</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:title</span> <span class="o">=&gt;</span> <span class="s2">&quot;The Problem with Callbacks in Rails&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="n">post</span><span class="o">.</span><span class="n">publish!</span>
</span><span class='line'>    <span class="n">expect</span><span class="p">(</span><span class="n">post</span><span class="o">.</span><span class="n">published_at</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">be_an_kind_of</span><span class="p">(</span><span class="no">Time</span><span class="p">)</span>
</span><span class='line'>    <span class="n">expect</span><span class="p">(</span><span class="n">post</span><span class="p">)</span><span class="o">.</span><span class="n">to_not</span> <span class="n">be_a_new_record</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>In order to get that example code to pass, <code>notify_followers</code> must be &#8220;stubbed
out&#8221;. If it isn&#8217;t, and if <code>follower</code>s are used within the mailer, the test will fail because it&#8217;s not able to execute the delivery (i.e. it&#8217;ll error out due to <code>nil</code> values).</p>

<h2>What About Observers?</h2>

<p>Rails developers who&#8217;ve begun moving into a more Object Oriented mindset might
ask, &#8220;What about using observers instead of callbacks?&#8221; It&#8217;s the right
direction: by creating an observer, you move responsibilities which don&#8217;t belong
in the object being observed to the observer.</p>

<p><img class="right" src="http://samuelmullen.com/images/observers.jpg"></p>

<p>The problem is that observers in Rails are kind of like ninja callbacks: they
perform the same function as callbacks, they just work in the shadows. Unless
you look at the file system, you are very likely to forget Observers even exist
in your application.</p>

<p>Furthermore, observers are assigned to their appropriate class when Rails starts
up, and Rails starts up when you run your tests. Once again, you&#8217;ll start
feeling pain in your tests first, because in order to avoid observer calls in
your tests, you will need to either create all the dependent objects or install
a gem such as <a href="https://github.com/patmaddox/no-peeping-toms">no_peeping_toms</a>.
Just like callbacks, observers run every time their condition is met.</p>

<p><em>Aside:</em> Herman Moreno wrote a good post on undocumented observer usage: <a href="http://blog.crowdint.com/2013/04/23/fun-with-activerecord-observer.html">Fun with
ActiveRecord::Observer</a>.</p>

<h2>Why Are Callbacks So Problematic?</h2>

<p>In his post on <a href="http://robots.thoughtbot.com/post/9123160250/activerecord-caching-and-the-single-responsibility">ActiveRecord, Caching, and the Single Responsibility Principle</a>, Joshua Clayton noticed &#8220;after_* callbacks on Rails models seem to have some of the most tightly-coupled code, especially when it comes to models with associations.&#8221;</p>

<p>It&#8217;s no coincidence. &#8220;before_&#8221; callbacks are generally used to prepare an
object to be saved. Updating timestamps or incrementing counters on the object
are the sort of things we do  &#8220;before&#8221; the object is saved. On the other hand,
&#8220;after_*&#8221; callbacks are primarily used in relation to saving or persisting the
object. Once the object is saved, the purpose (i.e. responsibility) of the
object has been fulfilled, and so what we usually see are callbacks reaching
outside of its area of responsibility, and that&#8217;s when we run into problems.</p>

<h2>Solving the Problem</h2>

<p>Jonathan Wallace, over at <a href="http://bignerdranch.com">the Big Nerd Ranch</a>, ran into to same problems and came up with one simple rule: &#8220;Use a callback only when the logic refers to state internal to the object.&#8221; (<a href="http://blog.bignerdranch.com/1658-the-only-acceptable-use-for-callbacks-in-rails-ever/">The only acceptable use for callbacks in Rails ever</a>)</p>

<p>If we can&#8217;t use callbacks which extend responsibility outside their class, what
do we do? We make an object whose responsibility is to handle that callback.</p>

<h2>Before Example</h2>

<p>Let&#8217;s look at a hypothetical example. This is what we might originally have:</p>

<figure class='code'><figcaption><span>Order model</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='ruby'><span class='line'><span class="k">class</span> <span class="nc">Order</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">belongs_to</span> <span class="ss">:user</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:line_items</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:products</span><span class="p">,</span> <span class="ss">:through</span> <span class="o">=&gt;</span> <span class="ss">:line_items</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">after_create</span> <span class="ss">:purchase_completion_notification</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">purchase_completion_notification</span>
</span><span class='line'>    <span class="no">Notifier</span><span class="o">.</span><span class="n">purchase_notifier</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span><span class="o">.</span><span class="n">deliver</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>




<figure class='code'><figcaption><span>Notifier ActionMailer</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Notifier</span> <span class="o">&lt;</span> <span class="no">ActionMailer</span><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">purchase_notifier</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@order</span> <span class="o">=</span> <span class="n">order</span>
</span><span class='line'>    <span class="vi">@user</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">user</span>
</span><span class='line'>    <span class="vi">@products</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">products</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">rest</span> <span class="n">of</span> <span class="n">the</span> <span class="n">action</span> <span class="n">mailer</span> <span class="n">logic</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>In the above example we can see that when an order is saved, it&#8217;s going to shoot
off an email to the customer. That Mailer is going to use the <code>order</code> object to
retrieve the ordering <code>user</code> and the products which were purchased and likely
use them in the email. Pretty simple, right?</p>

<p>In a test, however, <em>any</em> time an order is saved to the database, <code>user</code>,
<code>line_items</code>, and <code>products</code> will need to be created, or the <code>purchase_notifier</code>
method will need to be stubbed out –
<code>Order.any_instance.stub(:purchase_notifier)</code> (Ugh).</p>

<h2>After Example</h2>

<p>Here&#8217;s what happens when we move some responsibilities:</p>

<p>Our <code>Order</code> model is much simpler.</p>

<figure class='code'><figcaption><span>Order model</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="k">class</span> <span class="nc">Order</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">belongs_to</span> <span class="ss">:user</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:line_items</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:products</span><span class="p">,</span> <span class="ss">:through</span> <span class="o">=&gt;</span> <span class="ss">:line_items</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here&#8217;s our new class:</p>

<figure class='code'><figcaption><span>OrderCompletion</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">OrderCompletion</span>
</span><span class='line'>  <span class="kp">attr_accessor</span> <span class="ss">:order</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@order</span> <span class="o">=</span> <span class="n">order</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">create</span>
</span><span class='line'>    <span class="k">if</span> <span class="nb">self</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">save</span>
</span><span class='line'>      <span class="nb">self</span><span class="o">.</span><span class="n">purchase_completion_notification</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="k">def</span> <span class="nf">purchase_completion_notification</span>
</span><span class='line'>    <span class="no">Notifier</span><span class="o">.</span><span class="n">purchase_notifier</span><span class="o">.</span><span class="n">deliver</span><span class="p">(</span><span class="nb">self</span><span class="o">.</span><span class="n">order</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>What we&#8217;ve done above is moved the process of saving the order and sending the
notification out of the <code>Order</code> model and into a PORO (Plain Old Ruby Object)
class. This class is responsible for completing an order. Y&#8217;know, saving the
order and letting the customer know it worked.</p>

<p>By doing this, we no longer have to stub out the notification method in our
tests, we&#8217;ve made it a simple matter to create an order without requiring an
email to be sent, and we&#8217;re following good Object Oriented design by making sure
our classes have a single responsibility
(<a href="http://butunclebob.com/ArticleS.UncleBob.SrpInRuby">SRP</a>).</p>

<p>It&#8217;s a simple matter to use this in our controller too:</p>

<figure class='code'><figcaption><span>OrdersController.create</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">create</span>
</span><span class='line'>  <span class="vi">@order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:order</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>  <span class="vi">@order_completion</span> <span class="o">=</span> <span class="no">OrderCompletion</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="vi">@order</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="vi">@order_completion</span><span class="o">.</span><span class="n">create</span>
</span><span class='line'>    <span class="n">redirect_to</span> <span class="n">root_path</span><span class="p">,</span> <span class="n">notice</span><span class="p">:</span> <span class="s1">&#39;Your order is being processed.&#39;</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="vi">@order</span> <span class="o">=</span> <span class="vi">@order_completion</span><span class="o">.</span><span class="n">order</span>
</span><span class='line'>    <span class="n">render</span> <span class="n">action</span><span class="p">:</span> <span class="s2">&quot;new&quot;</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>


<h2>Wrapping up</h2>

<p>As much as I complain about callbacks, they&#8217;re really not bad as long as you
remember the rule: &#8220;Use a callback only when the logic refers to state internal to the object.&#8221; And really, that can be applied to any method.</p>

<p>When you start to feel those first twinges of pain from your tests, whether from
callbacks or otherwise, consider if what you are trying to do exceeds your
class&#8217;s responsibility. Creating a new class is a simple matter, especially
compared to the pain and frustration caused by not doing it.</p>

<p><em>Many thanks to <a href="http://patshaughnessy.net/">Pat Shaughnessy</a> for proof-reading and providing feedback.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Determining and Encouraging Developer Growth]]></title>
    <link href="http://samuelmullen.com/2013/01/determining-and-encouraging-developer-growth/"/>
    <updated>2013-01-13T06:39:00-06:00</updated>
    <id>http://samuelmullen.com/2013/01/determining-and-encouraging-developer-growth</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://samuelmullen.com/images/developer_growth/growthchart.jpg"></p>

<p>Excitement, frustration, discovery, passion, elation, disappointment, hunger,
failure, conquest, defeat, insight, freedom, dominance. These are not subtle
words, and I&#8217;ve chosen them because they are descriptive of programming, and
more specifically, growth as a programmer. Not one word standing above the
others, but all of them, in any order, and of varying durations. Do you know
these words? Do you know them now, or are they a memory?</p>

<p>I&#8217;ve been thinking a lot lately about how to determine if you&#8217;re growing as a
programmer. It&#8217;s easy to see growth when you are first beginning; you see the
progress of acquiring knowledge. But as your mastery increases, quantifying
growth moves from that based on knowledge, toward a growth based on
understanding and application. What are the metrics for determining growth then?</p>

<h2>An Experiment</h2>

<p>To give us an idea if we are growing, let&#8217;s try an experiment. Answer the
following questions:</p>

<ul>
<li>List three new things you&#8217;ve learned in the last week about your current
technology of preference.</li>
<li>What new idea, concept, pattern, or technique have you played with or tried to
use in the past week.</li>
<li>What was the last technical book you read for fun? When did you read it?</li>
<li>Do you program outside of work? (For fun, not freelancing)</li>
<li>When was the last time you had an exciting conversation with a peer about
programming?</li>
<li>Do you look forward to going to work?</li>
</ul>


<p>Did your responses to those questions give you an idea about your current state
of growth?  Simply put, the best metric for determining if you are learning and
growing as a programmer - probably as anything - is this: &#8220;Are you having fun?&#8221;</p>

<p>So, are you?</p>

<p>Go back to the first sentence of this post. When in your programming career have
you felt those words most intensely? When you were learning, right? But weren&#8217;t
you also enjoying yourself? Weren&#8217;t you having fun?</p>

<h2>Being Stuck</h2>

<p>If you had trouble providing positive answers for the questions above, maybe
you&#8217;re in a bad spot. I know what that&#8217;s like. I know what it&#8217;s like to have
every idea shot down because that&#8217;s &#8220;not how we do things.&#8221; I know what it&#8217;s
like to have your creativity constrained because &#8220;failure&#8217;s not an option.&#8221; I
know what it&#8217;s like to want to do better, but be told &#8220;it&#8217;s good enough.&#8221; I know
what it&#8217;s like to be stuck.</p>

<p>Being stuck is a horrible state to be in. Maybe you&#8217;re stuck because of the
technology you&#8217;re using, or maybe it&#8217;s your boss or your organization, maybe
it&#8217;s the system, but whatever the case, there you are, unchanging, unmoving, not
growing, stuck.</p>

<p>Needless to say, being stuck is not fun.</p>

<p><img class="center" src="http://samuelmullen.com/images/developer_growth/stuck_cat.jpg"></p>

<h2>Encouraging Growth</h2>

<p>The whole premise of this post is that if you&#8217;re having fun, you&#8217;re most likely
growing as a developer; if you&#8217;re not having fun, then you&#8217;re not growing as
much as you could. If you&#8217;re not growing as much as you would like, then
something needs to change, and maybe several &#8220;somethings&#8221;.</p>

<p>What follows is a list of some ideas you should try in order to rekindle your
love and excitement for the art. This is not an exhaustive list, and I would
love to hear from you what things you&#8217;ve tried to get the development juices
flowing.</p>

<ul>
<li>Keep lists of researchables: I have a list - it sounds more organized than it
is - I use to keep track of words, concepts, and ideas that I want to
research. When I get a few minutes (between test runs) I go and look things up.</li>
<li>Develop a low pain threshold: We create applications to solve problems. Pay
attention to those little annoyances and find a programmatic way of solving
them. e.g. Be annoyed by the way your editor behaves and find a way to fix it.</li>
<li>Play more:

<ul>
<li>Take some time to tweak your configuration files; especially
<a href="http://www.gnu.org/software/emacs/">emacs</a>, <a href="http://www.vim.org/">VIM</a>,
<a href="http://git-scm.com/">Git</a>, or shell configs.</li>
<li>Experiment with a new library or language</li>
<li>Force a limitation on yourself: examples, try to use recursion instead of
loops; use methods instead of constants; read source code in place of
documentation; etc. See also <a href="http://samuelmullen.com/2011/04/creative-limitations/">Creative Limitations</a></li>
<li>Participate in a <a href="http://en.wikipedia.org/wiki/Hackathon">Hackathon</a></li>
</ul>
</li>
<li>Find other developers who are passionate and start hanging out with them,
especially if they&#8217;re better than you.

<ul>
<li>Check out your local user groups</li>
<li>Go to conferences</li>
<li>Find a mentor</li>
<li>Find a way, but make those connections</li>
</ul>
</li>
<li>Start contributing to a
<a href="http://en.wikipedia.org/wiki/Free_and_open_source_software">FOSS</a> project</li>
<li>Try playing with some <a href="http://www.arduino.cc/">Arduino</a> or <a href="http://www.raspberrypi.org/">Raspberry Pi</a> projects?</li>
</ul>


<p><img class="right" src="http://samuelmullen.com/images/developer_growth/nice_game_of_chess.jpg"></p>

<p>The point is to play and to have fun, and to remind yourself how much fun
programming is. Yeah, at times programming is difficult and sometimes it&#8217;s even
a chore, but you know from your past that it doesn&#8217;t have to be that way, and
you know from your experience that it shouldn&#8217;t be that way. If you are at a
point where you find yourself incapable of growth, maybe it&#8217;s time for a change;
maybe a drastic change.</p>

<p>Because, let&#8217;s face it, &#8220;If you&#8217;re not having fun, you&#8217;re doing it wrong.&#8221;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Workload Based Marketing]]></title>
    <link href="http://samuelmullen.com/2012/12/workload-based-marketing/"/>
    <updated>2012-12-24T22:03:00-06:00</updated>
    <id>http://samuelmullen.com/2012/12/workload-based-marketing</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been working through the book,<a href="http://www.amazon.com/gp/product/B001O0EGQY?ie=UTF8&amp;keywords=get%20clients%20now&amp;qid=1356149801&amp;ref_=sr_1_1&amp;sr=8-1">&#8220;Get Clients Now,&#8221;</a>
as part of my never ending quest to attract and develop new clients. In the
book, the author suggests you choose ten activities you can complete on a daily
or weekly basis over a period of twenty-eight days. As I was discussing with my
wife some of the activities I was considering (blogging, answering
<a href="http://stackoverflow.com">StackOverflow</a> questions, attending networking
events, reaching out to contacts, etc.), she stated rather bluntly, &#8220;You&#8217;re
setting yourself up for failure.&#8221;</p>

<p>It sounds harsh, but it was really what I needed to hear. My wife worked in
sales for a number of years and she recognized immediately the effort required
to complete all the activities I had on my list. More importantly, she
understands me and the effect it would have on me.</p>

<p>You see, as of this writing I am finishing up two projects and I&#8217;m gearing up to
start three new ones - one which includes an unfamiliar technology. My wife
knows that if I meet all my clients&#8217; needs AND attempt to meet all ten of my
marketing goals, something will have to give; probably a lot of somethings. And
because I&#8217;m highly goal oriented, she knows I would beat myself up for not
meeting all my expectations, and waste a lot of energy and time trying to figure
out why I couldn&#8217;t get it all done.</p>

<p><img class="right" src="http://samuelmullen.com/images/workload_based_marketing/Balance.jpg"></p>

<p>My wife&#8217;s years of sales experience helped her recognize the obvious: I was
trying to do too much. Part of what kept me from recognizing the same was my own
inexperience with the demands of marketing, the rest was me blindly following
recipes out of a book.</p>

<p>But from her insight and fresh perspective, I&#8217;ve come up with a new marketing
strategy for myself which I&#8217;ve dubbed&#8230;</p>

<h2>Workload Based Marketing</h2>

<p>It&#8217;s not rocket science, and it&#8217;s certainly not an earth-shattering discovery,
but it was eye-opening for me and where I was at. It starts with these three
rules:</p>

<h3>1. Focus on Current Clients First</h3>

<p>Here&#8217;s the deal, you already have actual clients. Marketing represents clients
you <em>might</em> get, and who <em>might</em> have money. If you have to choose between
marketing and investing in relationships with your current clients, choose the
latter; it&#8217;s the only decision guaranteed to pay dividends every time.</p>

<h3>2. No Marketing Effort is Too Small</h3>

<p>Like I said, clients always come first, but I also know none of us are
heads-down 24/7. There&#8217;s always time in a week for at least something. Maybe
it&#8217;s making a couple phone calls one day, eating lunch with a contact another
day, or just getting a paragraph or two on that next blog post.</p>

<p>The point is to keep up the momentum. Momentum from small efforts can lead to
big opportunities.</p>

<h3>3. Waiting is Stupid</h3>

<p>In &#8220;Get Clients Now&#8221;, the author instructs you to start your system on a
Saturday. I had planned on starting mine on the first Saturday of the new year,
after the holidays were over.</p>

<p>My wife asked, &#8220;Why do you want to wait to find clients?&#8221;</p>

<p>Ummmm&#8230;.</p>

<h3>The Plan(s)</h3>

<p><img class="right" src="http://samuelmullen.com/images/workload_based_marketing/planz.jpg"></p>

<p>With those rules in mind, let&#8217;s look at the plan itself.</p>

<p>Based on the name alone, you should already get the basic idea of &#8220;Workload
Based Marketing&#8221;: if you have a lot of work on your plate, don&#8217;t push yourself
to market so much; if your workload is light, increase your marketing efforts
accordingly.</p>

<p>More than that, though, and before you start anything, devise plans for yourself
which you can execute according to the intensity of your workload. To keep it
simple, create three plans: one for when your workload is light; one for when
it&#8217;s moderate, and then one for those times when you&#8217;re swamped. For example,
I&#8217;ve created plans for myself based on possible workload, each with a variety of
activities suggested from &#8220;Get Clients Now&#8221; or from my own marketing
preferences.</p>

<p>Having these plans established ahead of time allows you to &#8220;just go on
autopilot&#8221; rather than burning a lot of cycles trying to figure out what to do.
You won&#8217;t need to waste time thinking about things, you&#8217;ll just be able to go.
Organizing your plans around a weekly schedule is probably the simplest
solution.</p>

<p>&#8220;Get Clients Now&#8221; is a great book, and every freelancer should read it at least
once and consider the author&#8217;s suggestions. In it, she gives some great advice
for getting started, setting goals, and strategies for the different stages of
the marketing cycle. But remember also, no system is perfect. Adjust whatever
system you settle on to match your own needs, and prepare your plans ahead of
time so that when storms hit, you can be ready with something to do, rather than
wasting precious time and energy.</p>

<p>One last thought, review your plans in light of the end goal: right clients,
right projects, and right price. Make certain your strategies and tactics are
aimed at the right audience. Use your time wisely on existing clients first, and
then pick the plan matching your workload to find the prospects who fit the
profile you desire.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why Your Company Should Sponsor Tech Conferences]]></title>
    <link href="http://samuelmullen.com/2012/12/why-your-company-should-sponsor-tech-conferences/"/>
    <updated>2012-12-16T18:47:00-06:00</updated>
    <id>http://samuelmullen.com/2012/12/why-your-company-should-sponsor-tech-conferences</id>
    <content type="html"><![CDATA[<p><strong>Note:</strong> <em>In this post, when I refer to &#8220;developers&#8221;, I&#8217;m generally referring
to programmers, DevOps, and digitally creative types.</em></p>

<p><img class="right" src="http://samuelmullen.com/images/conference_sponsorship/ruby-midwest-logo-2013.png"></p>

<p>For the past couple of months, I&#8217;ve been reaching out to companies, asking them
to sponsor <a href="http://rubymidwest.com">Ruby Midwest 2013</a>, a technology conference
I&#8217;m helping to organize. Every company I&#8217;ve had contact with has been super nice
and many have chosen to support us either financially or with products or
services. Some companies have chosen not to sponsor for a variety of reasons,
and that&#8217;s cool, I get that. There are definitely reasons not to become
sponsors. But in this post, I want to look at some of the results
decision-makers are expecting, and some which they might not consider, and
which, if they do, might change their outlook on sponsorship.</p>

<p>Just looking at sponsorship from a general perspective, most companies are
seeking to advertise, increase brand awareness, make sales, and maybe find new
employees. The hope is that the amount of money invested will be offset by what
is returned. And of course this works, which is why you see some of the same
companies sponsoring so many conferences.</p>

<p>Take, for example, publishing companies <a href="http://oreilly.com">O&#8217;Reilly</a> and
<a href="http://addisonwesley.com">Addison Wesley</a>. I can&#8217;t imagine a conference which
didn&#8217;t have books from either of these companies to give away. The results of
these minor investments are advertising and increased brand awareness (people
get to see the books handed out), and increased sales by way of the book
recipients buying more products from the same publisher, and also from them
telling their friends or writing reviews. It&#8217;s nothing but &#8220;win&#8221; for these guys.</p>

<p>Since I am currently seeking sponsorship for Ruby Midwest, I am a little
hesitant to say this, but investing in a conference may not be the best choice
for a company if it is only looking for sales and advertising. Part of the
problem is that those running the conference are usually developers (like
myself), and, well, we&#8217;re not really known for our ability to sell and market
companies and products (or even have &#8220;normal&#8221; conversations.)</p>

<p>The other part of the problem is the target audience. Most people attending tech
conferences are company-employed developers (i.e. not the decision-makers). So
even though a company might be selling the new whiz-bang product, we may not be
in a position to do anything about it other than make the case to our
management, (and again, we&#8217;re not really known for our ability to sell.)</p>

<p>Selling books or developer tools is one thing; developers are usually the
decision-makers. Selling other types of products, especially those focused on
companies, are a different matter; developers, even if they&#8217;re the target
audience, may not be in a position to purchase.</p>

<p>And so at this point you may be asking yourself, &#8220;If we&#8217;re unlikely to make
sales, get a good advertising push, or increase our brand awareness, why should
my company sponsor a regional conference?&#8221; In short, by sponsoring conferences,
you are telling your development staff that you support them and their passion,
you inform the local development community that you &#8220;get it&#8221;, and you add to
your reputation as a company who understands technology. Let&#8217;s break this down a
bit.</p>

<p>I&#8217;ve written about this before in my post on &#8221;<a href="http://samuelmullen.com/2012/02/advice-on-attracting-good-developers/">Attracting Good Developers</a>&#8221;,
but in short, developers want to know they&#8217;re supported and needed. You can do
that in a number of ways (and you should) but one way is to invest, not just in
them (which you should), but also their interest: i.e. conferences they want to
attend. By doing so, it&#8217;s understood that your company is taking an interest in
their growth and improvement. That matters.</p>

<p>When you sponsor conferences, you not only tell your own development staff you
support them, but that you support the local community and you&#8217;re a company who
understands technology. Believe me, we notice.</p>

<p>Don&#8217;t underestimate the value of this. Good developers want to work for &#8220;cool&#8221;
companies, because, well, to be honest, it makes us &#8220;cool&#8221;. Not &#8220;cool&#8221; like
James Dean, but &#8220;cool&#8221; as in, we are good enough to work there. This can be a
huge confidence boost, and can unleash hidden potential. Developers who once
just thought of themselves as the line-workers of the 21st century, might now -
with the added confidence - begin to think of themselves as something more, and
increase their contribution to your organization.</p>

<p><img src="http://samuelmullen.com/images/conference_sponsorship/hackathon.jpg"></p>

<p><em>Used with permission from <a href="http://www.kcitp.com/">KCITP&#8217;s</a> post: <a href="http://www.kcitp.com/2012/05/21/kansas-city-hackathon/">3 Reasons Why Developers Need To Attend Hack The Midwest</a></em></p>

<p>Alright, let&#8217;s pretend my arguments have convinced you to sponsor a conference,
which ones to you sponsor? Here are three things to look at:</p>

<ol>
<li>If you&#8217;re local, stay local: If you are a company localized to a certain city or area, sponsor those conferences and developer communities resident in that area; in your market</li>
<li>If you&#8217;re big, go BIG: If you&#8217;ve got offices all over the nation or world, you should probably be sponsoring the major events: <a href="http://oscon.com">OSCon</a>, <a href="http://pycon.org">PyCon</a>, <a href="http://rubyconf.org">RubyConf</a>, <a href="http://velocityconf.com">Velocity</a>, etc. But hey, don&#8217;t forget the to sponsor the regional conferences your offices are located.</li>
<li>Stay with what you know: Unless you are trying to recruit talent from another technology area, don&#8217;t feel like you have to sponsor everything; just sponsor the conferences relevant to your company.</li>
</ol>


<p>Tech conferences occur in every semi-major city around the globe and every
company with a good development staff or who wants a good development staff
should be sponsoring at least one a year. Sure, sponsorships can bring in new
sales extend your company&#8217;s brand, but more than that, it can improve your
company&#8217;s reputation among your own staff and in the development community.</p>

<p>In the end, sponsoring conferences isn&#8217;t just about investing in your company&#8217;s
representation of itself, it&#8217;s really about investing in your development staff
and in your company&#8217;s culture. It&#8217;s worth it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Step By Step: Setting Up minitest and Cucumber with Rails]]></title>
    <link href="http://samuelmullen.com/2012/11/step-by-step-setting-up-minitest-and-cucumber-with-rails/"/>
    <updated>2012-11-10T13:50:00-06:00</updated>
    <id>http://samuelmullen.com/2012/11/step-by-step-setting-up-minitest-and-cucumber-with-rails</id>
    <content type="html"><![CDATA[<p>I&#8217;ve tried a couple times in the past to successfully set up a
<a href="http://rubyonrails.org/">Rails</a> application with both
<a href="https://github.com/seattlerb/minitest">minitest</a> and
<a href="http://cukes.info/">Cucumber</a>; each time, I ran in to issues. I&#8217;m not entirely
sure why I ran into the issues I did, but I just couldn&#8217;t seem to get things
working. Recently, I managed to get everything going with almost no effort.
Here&#8217;s what I did.</p>

<h2>Step 1: Creating and Configuring a New Rails App with minitest</h2>

<p>For the sake of simplicity, we&#8217;ll assume that we&#8217;re creating a new Rails application. To do so, we&#8217;ll want to skip the standard usage of Test::Unit</p>

<figure class='code'><figcaption><span>Initializing a New App</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='bash'><span class='line'>rails new &lt;app_name&gt; --skip-test-unit
</span></code></pre></td></tr></table></div></figure>


<p>Immediately after this, we&#8217;ll need to install and initialize the <code>minitest-rails</code> gem</p>

<figure class='code'><figcaption><span>Gemfile</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="n">group</span> <span class="ss">:test</span><span class="p">,</span> <span class="ss">:development</span> <span class="k">do</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>  <span class="n">gem</span> <span class="s1">&#39;minitest-rails&#39;</span><span class="p">,</span> <span class="err">&#39;</span><span class="o">~&gt;</span> <span class="mi">0</span><span class="o">.</span><span class="mi">3</span><span class="o">.</span><span class="mi">0</span><span class="sb">` # as of this writing</span>
</span><span class='line'><span class="sb">  ...</span>
</span><span class='line'><span class="sb">end</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>bundle install</code> and then initialize the gem in the app.</p>

<figure class='code'><figcaption><span>minitest installation</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='bash'><span class='line'>rails g mini_test:install
</span></code></pre></td></tr></table></div></figure>


<p>The last thing we&#8217;ll do is make sure our generators behave accordingly. To do that, add the following logic to the <code>config/application.rb</code> <code>config</code> block:</p>

<figure class='code'><figcaption><span>config/application.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">generators</span> <span class="k">do</span> <span class="o">|</span><span class="n">g</span><span class="o">|</span>
</span><span class='line'>  <span class="n">g</span><span class="o">.</span><span class="n">test_framework</span> <span class="ss">:mini_test</span><span class="p">,</span> <span class="ss">:spec</span> <span class="o">=&gt;</span> <span class="kp">true</span><span class="p">,</span> <span class="ss">:fixture</span> <span class="o">=&gt;</span> <span class="kp">false</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This turns on <code>minitest</code>&#8217;s <code>spec</code> functionality and turns off fixture generation. If you don&#8217;t want to use <code>minispec</code> or if you do want to use fixtures, just alter the values accordingly.</p>

<h2>Step 2: Setting up Cucumber</h2>

<p>Okay, at this point we have our foundational test suite set up. Now we need to add cucumber to the mix.</p>

<p>Add <code>cucumber-rails</code> to your <code>Gemfile</code> as you normally would.</p>

<figure class='code'><figcaption><span>Gemfile</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='ruby'><span class='line'><span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">gem</span> <span class="s1">&#39;cucumber-rails&#39;</span><span class="p">,</span> <span class="s2">&quot;~&gt; 1.3.0&quot;</span>
</span><span class='line'>  <span class="n">gem</span> <span class="s2">&quot;database_cleaner&quot;</span><span class="p">,</span> <span class="s2">&quot;~&gt; 0.9.1&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Call <code>bundle install</code> and then call the cucumber Rails generator:</p>

<figure class='code'><figcaption><span>cucumber:install</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='bash'><span class='line'>rails g cucumber:install
</span></code></pre></td></tr></table></div></figure>


<p>Now, add the following lines of code to <code>features/support/env.rb</code>. (I placed mine near the top.)</p>

<figure class='code'><figcaption><span>features/support/env.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;minitest/spec&#39;</span>
</span><span class='line'><span class="no">World</span><span class="p">(</span><span class="no">MiniTest</span><span class="o">::</span><span class="no">Assertions</span><span class="p">)</span>
</span><span class='line'><span class="no">MiniTest</span><span class="o">::</span><span class="no">Spec</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="kp">nil</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>For greater separation, the previous lines of code above can be added to a different support file.</p>

<h2>Step 3: Running the Tests</h2>

<p>At this point, everything should work. Let&#8217;s find out with a couple simple tests.</p>

<h3>minispec Test</h3>

<p>Create the following test file and model:</p>

<figure class='code'><figcaption><span>test/models/foo_test.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">require_relative</span> <span class="s2">&quot;../minitest_helper&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">describe</span> <span class="no">Foo</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;must be valid&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="no">Foo</span><span class="o">.</span><span class="n">must_respond_to</span> <span class="ss">:new</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>And the corresponding model file:</p>

<figure class='code'><figcaption><span>app/models/foo.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice in the first line of <code>foo_test.rb</code> that we&#8217;re using <code>require_relative</code> instead of a normal <code>require</code> call. We do this to avoid the need to use rake. Now, rather than running <code>bundle exec rake minitest:models</code> and running all the models, we can execute <code>bundle exec ruby test/models/foo_test.rb</code>.</p>

<h3>Cucumber Test</h3>

<p>Create the following Cucumber &#8220;feature&#8221; file:</p>

<figure class='code'><figcaption><span>test.feature</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='cucumber'><span class='line'><span class="k">Feature:</span><span class="nf"> test</span>
</span><span class='line'><span class="nf">  In order to test cucumber</span>
</span><span class='line'><span class="nf">  a user</span>
</span><span class='line'><span class="nf">  wants to successfully land on the home page</span>
</span><span class='line'>
</span><span class='line'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> foo</span>
</span><span class='line'><span class="k">    Given </span><span class="nf">I am on the home page</span>
</span><span class='line'><span class="nf">     </span><span class="k">Then </span><span class="nf">I should see &quot;</span><span class="s">Ruby on Rails: Welcome aboard</span><span class="nf">&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>And an associated step file:</p>

<figure class='code'><figcaption><span>test_steps.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Given</span><span class="sr"> /^I am on the home page$/</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">visit</span> <span class="s2">&quot;/&quot;</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">Then</span><span class="sr"> /^I should see \&quot;([^&quot;]+)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">string</span><span class="o">|</span>
</span><span class='line'>  <span class="n">page</span><span class="o">.</span><span class="n">has_content?</span><span class="p">(</span><span class="n">string</span><span class="p">)</span><span class="o">.</span><span class="n">must_equal</span> <span class="kp">true</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Run the test and everything should be green.</p>

<figure class='code'><figcaption><span>Cucumber execution</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='bash'><span class='line'>bundle <span class="nb">exec </span>cucumber features/test.feature
</span></code></pre></td></tr></table></div></figure>


<h2>Final Thoughts</h2>

<p>I like minitest a lot, mainly because it&#8217;s fast, it&#8217;s simple, and it&#8217;s included in Ruby 1.9+, but there are drawbacks; the two main ones for me are its readability and the lack of integration with the <a href="https://github.com/tpope/vim-rails">vim-rails</a> plugin. Neither of these &#8220;drawbacks&#8221; are deal breakers, however: the <code>vim-rails</code> plugin will eventually support it, and there are gems to make the suite more &#8220;civilized&#8221; (listed below).</p>

<p>So if you like the idea of a really simple and fast test suite, and you don&#8217;t mind the syntax, give <code>minitest</code> a go on your next project. Oh, and let me know how it works out for you.</p>

<h2>Further Reading</h2>

<ul>
<li>minitest on GitHub: <a href="https://github.com/seattlerb/minitest">https://github.com/seattlerb/minitest</a></li>
<li>minitest RDoc: <a href="http://docs.seattlerb.org/minitest">http://docs.seattlerb.org/minitest/</a></li>
<li>minitest-rails: <a href="https://github.com/blowmage/minitest-rails">https://github.com/blowmage/minitest-rails</a></li>
<li>including minitest in Cucumber: <a href="https://github.com/cucumber/cucumber/wiki/Using-MiniTest">https://github.com/cucumber/cucumber/wiki/Using-MiniTest</a></li>
<li>Capybara integration for minitest-rails: <a href="https://github.com/blowmage/minitest-rails-capybara">https://github.com/blowmage/minitest-rails-capybara</a></li>
<li>TURN - minitest Reporters: <a href="https://github.com/TwP/turn">https://github.com/TwP/turn</a></li>
<li>PurdyTest from tenderlove: <a href="https://github.com/tenderlove/purdytest">https://github.com/tenderlove/purdytest</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Introvert's Networking Survival Guide: Large Events]]></title>
    <link href="http://samuelmullen.com/2012/08/the-introverts-networking-survival-guide/"/>
    <updated>2012-08-25T17:20:00-05:00</updated>
    <id>http://samuelmullen.com/2012/08/the-introverts-networking-survival-guide</id>
    <content type="html"><![CDATA[<p>I am an introvert. As a freelance developer, that works out well for me (mostly). It allows me to work from my home office with little to no contact with the majority of mankind for days on end. But as a freelancer, I also recognize that I have to get out and meet people in order to find new work.</p>

<p>A little over a year ago, I was invited to a <a href="http://bni.com">BNI</a> networking event. I thought it would be the same thing I&#8217;ve seen at any large gathering of people: people gathering to those they already know, new people getting ignored, uninteresting small talk, etc.</p>

<p>I was wrong.</p>

<p>From the moment I stepped through the door, I was greeted with a dozen hands thrust out to shake mine, people asking me my name, what I did, where I was located, then telling me their name, telling me what they did, wanting to know my pitch, telling me theirs, asking me if I was a member, if I wanted to be a member.</p>

<p>It was too much.</p>

<p>As soon as I could, I got out of the room and into a hallway, and called my wife. Yes, I wanted to talk to her, but at that moment, I also needed an excuse to escape and focus on one person. I slipped back into the room once the meeting had officially started, and counted the minutes until I could leave.</p>

<p>Since then, I&#8217;ve been to a number of other networking events. Each event had its own characteristics and quirks, and I&#8217;ve learned more about networking from each event I&#8217;ve attended.</p>

<h2>Tip #1 :: No One Expects the Spanish Inquisition</h2>

<p><img class="center" src="http://samuelmullen.com/images/networking/spanish_inquisition.jpg"></p>

<p>Expectations are everything, and we enter in to every situation with at least some sort of preconceived idea of what is going to happen. Under normal circumstances, being a little off about what to expect isn&#8217;t a problem, but when your expectations are completely wrong, as mine were at the BNI event (and my 10th grade Geometry class), it can be difficult to recover from.</p>

<p>Knowing this about myself, I find it helpful to do a little pre-networking research. Not a lot, but just enough to have a better grasp of what to expect. Here are some of the things I look for:</p>

<ul>
<li>Setting :: Where is the event taking place?Is it a bar or restaurant? A business? On a college campus?</li>
<li>Size :: How many people are expected to attend? This is an important one for me.</li>
<li>Format ::

<ul>
<li>Is this a presentation plus networking event?</li>
<li>Is it just networking?</li>
<li>Is there a structure to the event?</li>
</ul>
</li>
<li>Topic :: Is there an overriding theme to the event?</li>
<li>People-group :: Is the event specific to a particular profession or more generalized?</li>
</ul>


<p><a href="http://www.youtube.com/watch?v=vt0Y39eMvpI">The Spanish Inquisition Sketch</a></p>

<h2>Tip #2 :: What is Your Quest?</h2>

<p><img class="center" src="http://samuelmullen.com/images/networking/what_is_your_quest.jpg"></p>

<p>&#8220;Why are you going to this event?&#8221; Have you ever asked yourself that question? Obviously you&#8217;re going there to increase your network and meet people, but really, to what end?</p>

<p>Are you looking to make a sale? To find someone to partner with? Are you looking for investors, or employees? Are you going just to practice networking (don&#8217;t laugh)?</p>

<p>Having a purpose in going to an event can 1) help determine if you should even go; 2) help determine if the event in question is going to help meet that goal; and 3) keep you on track while you are at the event.</p>

<p>If you don&#8217;t have a purpose in going to a networking event, you probably shouldn&#8217;t be going.</p>

<p><a href="http://www.youtube.com/watch?v=pWS8Mg-JWSg">Three Questions</a></p>

<h2>Tip #3 :: Are you the Judean People&#8217;s Front?</h2>

<p><img class="center" src="http://samuelmullen.com/images/networking/judean_peoples_front.jpg"></p>

<p>Okay, so you know where the event is going to be and what it&#8217;s going to be like, and you have a purpose in going, the last thing you need to know is who to talk to. If you&#8217;ve figured out <em>why</em> you are going to the event, this part should direct you to the people you should look for.</p>

<p>As an example, if I go to a networking event and my goal is to find a new client, I&#8217;m not going to spend a lot of time speaking with other freelancers, except to find out if they are looking to offload a client. Instead, I&#8217;m going to target IT decision makers in the markets I&#8217;m looking to get into.</p>

<p>Simple, right? But as introverts, it&#8217;s a lot easier to gravitate to those people we know, and to corners we feel safe in. Sometimes, you just have to embrace the suck and talk to people.</p>

<p>So based on your purpose for going to the event, who are the people who would best help you meet your goals?</p>

<p><a href="http://www.youtube.com/watch?v=gb_qHP7VaZE">The People&#8217;s Front</a></p>

<h2>Bonus Tip :: Why Did You Say &#8220;Good Morning&#8221;?</h2>

<p><img class="center" src="http://samuelmullen.com/images/networking/job_interview.jpg"></p>

<p>Okay, last point. Most networking events - thankfully - last less than three hours. That means you have that much time to acclimate yourself to the environment, figure out how to meet your goals, and then find the people you need to meet. The last thing you need to do is waste the time with small talk.</p>

<p>I don&#8217;t care what the <a href="http://en.wikipedia.org/wiki/Bene_gesserit">Bene Gesserit</a> say, small talk, not fear, is the mind-killer.We fall in to the trap of small talk because it&#8217;s safe and it&#8217;s comfortable, but it&#8217;s also completely forgettable. The point of networking is to connect with people. If your conversation is forgettable, what value is the connection?</p>

<p>So what do you talk about? I like to use some of the following questions and roll on from there:</p>

<ul>
<li>Why are you here and what kinds of people are you looking to connect with?

<ul>
<li>You will probably want to ask that less directly. The point is just to find out if there is potential value in the connection.</li>
</ul>
</li>
<li>What do you do? It&#8217;s cliche and small talky, but it has to be asked.</li>
<li>What is your favorite part about what you do?

<ul>
<li>I ask this solely to ensure there is a positive experience to the time. Well, that, and it&#8217;s usually fun to see people get excited about their work/family/etc.</li>
<li>Note: I don&#8217;t ask what their least favorite part is</li>
</ul>
</li>
<li>What are some of the struggles you are currently facing in your work?

<ul>
<li>This isn&#8217;t asking what they don&#8217;t like.</li>
<li>This is discovering if you can fix their problems.</li>
</ul>
</li>
<li>and so on&#8230;</li>
</ul>


<p><a href="http://www.youtube.com/watch?v=1dWMIuipn_c">Silly Job Interview</a></p>

<p>As far as networking goes, I usually prefer meeting people for coffee or lunch. It fits my temperament, it&#8217;s less intense, and it generally makes for a better conversation. That&#8217;s the advice most &#8220;networking for introverts&#8221; articles provide. While more intimate types of networking fits the introverts&#8217; personality, it doesn&#8217;t address the problem of what to do if you&#8217;ve managed to stumble into a larger event, which is to say, find out what to expect, figure out your purpose,  know who you want to meet, and avoid small talk.</p>

<p>If all else fails, you can always hide out in the hallway and talk to your wife on the phone.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Letter to the City of Shawnee, Kansas]]></title>
    <link href="http://samuelmullen.com/2012/08/my-letter-to-the-city-of-shawnee/"/>
    <updated>2012-08-13T15:32:00-05:00</updated>
    <id>http://samuelmullen.com/2012/08/my-letter-to-the-city-of-shawnee</id>
    <content type="html"><![CDATA[<p>As a rule, I don&#8217;t post anything relating to politics, but since this has
nothing to do with the banalities of left vs. right vs. independents, I am
making an exception.</p>

<p>I am so completely excited about <a href="https://fiber.google.com">Google Fiber</a>, I
could burst, and I am terrified that rather than opening our doors to the future
and to future growth, our local governments will instead see this as an
opportunity to make a buck and flex its muscles.</p>

<p>The Internet represents freedom. We need to open our arms to it.</p>

<blockquote><p>Ladies and Gentlemen of the Governing Body of Shawnee,</p>

<p>My name is Samuel Mullen. I&#8217;m a freelance programmer specializing in web and
iPhone development. As such, I am very keen to learn what, if anything, has been
done to encourage Google Fiber to come to Shawnee. Obviously we are some
distance from the initial locations selected by Google, but they will inevitably
spread out, and I am anxious for it&#8217;s arrival. I am convinced my business can
only grow with its arrival: through new companies, technological improvements,
and networking.</p>

<p>I believe the greater Kansas City metro area is in a unique position to usurp
the technology crown which Silicon Valley currently wears. New businesses, new
ideas, new people, and new opportunities are all on the horizon, we must be
prepared to take up the mantle of Silicon Prairie.</p>

<p>Please let me know that you, the current leaders of Shawnee, are as excited
about this technology as I am, and that you are doing everything in your power
to eliminate road blocks which might hinder the arrival of Google Fiber.</p>

<p>Thank you,
Samuel Mullen</p></blockquote>

<p>If you live in the Kansas City metro area and you are not part of the Google
Fiber rollout, I would encourage you to contact your city officials as well.</p>

<h3>Update: August 24, 2012</h3>

<p><a href="http://cityofshawnee.org">The City of Shawnee</a> did end up reaching out to
Google. This is <a href="http://www.cityofshawnee.org/WEB/ShawneeCMS.nsf/vwNews/3FAF13B950ED405086257A6A005F1010?OpenDocument">their letter</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Getting More Out of the Rails Console]]></title>
    <link href="http://samuelmullen.com/2012/07/getting-more-out-of-the-rails-console/"/>
    <updated>2012-07-11T10:35:00-05:00</updated>
    <id>http://samuelmullen.com/2012/07/getting-more-out-of-the-rails-console</id>
    <content type="html"><![CDATA[<p>Even at it&#8217;s most basic functionality, the console is indispensable in the Rails
developer&#8217;s arsenal of tools. Whether it&#8217;s running a query, testing a chain of
methods, or executing small blocks of code, the console may be the most useful
tool available to the Rails developer. With that in mind, doesn&#8217;t it make sense
to do what we can to get the most out of it?</p>

<h2>IRB</h2>

<h3>.irbrc</h3>

<p>Under the hood, the Rails Console is just IRB (Interactive Ruby), so anything
you can do with IRB, you can do in the console. This means you can modify your
IRB environment and .irbrc file to define methods to use  at the console. Here
are three methods I frequently use:</p>

<figure class='code'><figcaption><span>Useful IRB methods</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="p">\</span><span class="c1"># return a sorted list of methods minus those which are inherited from Object</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Object</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">interesting_methods</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">self</span><span class="o">.</span><span class="n">methods</span> <span class="o">-</span> <span class="no">Object</span><span class="o">.</span><span class="n">instance_methods</span><span class="p">)</span><span class="o">.</span><span class="n">sort</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="p">\</span><span class="c1"># return an Array of random numbers</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Array</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">test_list</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</span><span class='line'>    <span class="nb">Array</span><span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.x</span><span class="p">)</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="p">\</span><span class="c1"># return a Hash of symbols to random numbers</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Hash</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">test_list</span>
</span><span class='line'>    <span class="nb">Array</span><span class="p">(</span><span class="ss">:a</span><span class="o">.</span><span class="n">.</span><span class="ss">:z</span><span class="p">)</span><span class="o">.</span><span class="n">each_with_object</span><span class="p">({})</span> <span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="p">,</span><span class="n">h</span><span class="o">|</span> <span class="n">h</span><span class="o">[</span><span class="n">x</span><span class="o">]</span> <span class="o">=</span> <span class="nb">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">)</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>Of course this isn&#8217;t even scratching the surface of what&#8217;s possible. Check out
what other people are doing:</p>

<ul>
<li><a href="https://github.com/search?langOverride=&amp;q=irbrc&amp;repo=&amp;start_value=1&amp;type=Code&amp;utf8=%E2%9C%93">Example .irbrc files on github</a></li>
<li><a href="http://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick">What&#8217;s Your Favourite IRB Trick?  (StackOverflow)</a></li>
</ul>


<h3>The Last Expression</h3>

<p>While working in the console, have you ever typed out a bit of code to return a
value and then realize you forgot to assign the returned value to a variable?
You then have to go back into the console history, move your cursor to the
beginning of the line, add the variable, and then execute the code again.</p>

<p>Ugh, what a pain!</p>

<p>Unbeknownst to most people, IRB places the output of the last command into the
<code>_</code> variable. Here, let me show you:</p>

<figure class='code'><figcaption><span>_ example</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='ruby'><span class='line'> <span class="o">&gt;</span> <span class="nb">Array</span><span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">10</span><span class="p">)</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="o">]</span>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">_</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, as cool as that is, you have to understand that <code>_</code> always contains the
output of the last expression. This means if you try call a method on it, it
will then contain the output of the method executed.</p>

<figure class='code'><figcaption><span>_ detailed example</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='ruby'><span class='line'> <span class="o">&gt;</span> <span class="nb">Array</span><span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">10</span><span class="p">)</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="o">]</span>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">_</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="o">]</span>
</span><span class='line'> <span class="o">&gt;</span>  <span class="n">_</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">}</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">20</span><span class="o">]</span>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">_</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="o">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">20</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Forking IRB</h3>

<p>When working in the console, it&#8217;s sometimes desirable to have another instance
to play with. It may be that you don&#8217;t want to lose what you were working with,
or you just need another scratch area, but whatever the case, you can create a
new console (IRB) session by calling <code>irb</code> at the prompt (note: you&#8217;ll use <code>irb</code>
for the rails console as well).</p>

<p>I typically don&#8217;t use this. If I need another IRB instance, I just open a new
<code>tmux</code> pane or window and work there.</p>

<p>If this sort of method fits your workflow, I highly recommend reading <a href="http://tagaholic.me/2009/05/11/demystifying-irb-commands.html">Gabriel Horner&#8217;s in depth post on IRB commands</a></p>

<h2>The Rails Console</h2>

<h3>Models</h3>

<p>One of the things you will want to make extensive use of in the console are your
app&#8217;s models. The Rails console is a great way to play with your models and an
alternative way of accessing your data.</p>

<figure class='code'><figcaption><span>Model Example</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="o">&gt;</span> <span class="n">u</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span> <span class="mi">1234</span><span class="p">;</span> <span class="kp">nil</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">nil</span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="s2">&quot;Foo Man&quot;</span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">email</span> <span class="o">=</span> <span class="s2">&quot;fooman@example.com&quot;</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="s2">&quot;fooman@example.com&quot;</span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">save</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">true</span>
</span></code></pre></td></tr></table></div></figure>


<h3>The &#8220;app&#8221; object</h3>

<p>The <code>app</code> object is used by test processes to mimic system interactions. Through
this object, we can access routing information and even make requests to our
app.</p>

<figure class='code'><figcaption><span>app Example</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># displaying path information</span>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">app</span><span class="o">.</span><span class="n">users_path</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="s2">&quot;/users/</span>
</span><span class='line'>
</span><span class='line'><span class="s2"> &gt; app.user_path(User.last)</span>
</span><span class='line'><span class="s2">=&gt; &quot;</span><span class="o">/</span><span class="n">users</span><span class="o">/</span><span class="mi">42</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># making app requests</span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span> <span class="n">app</span><span class="o">.</span><span class="n">user_path</span><span class="p">(</span><span class="no">User</span><span class="o">.</span><span class="n">last</span><span class="p">)</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="mi">200</span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">app</span><span class="o">.</span><span class="n">response</span><span class="o">.</span><span class="n">body</span>
</span><span class='line'><span class="o">=&gt;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;&lt;!DOCTYPE html&gt;</span><span class="se">\n</span><span class="s2">&lt;html&gt;</span><span class="se">\n</span><span class="s2">  &lt;head&gt;</span><span class="se">\n</span><span class="s2">    &lt;title&gt;...&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<h3>The &#8220;helper&#8221; Object</h3>

<p>I really don&#8217;t think I can do a better job on this than what Nick Quaranto
already did in his <a href="http://37signals.com/svn/posts/3176-three-quick-rails-console-tips">&#8220;Three Quick Rails console tips&#8221; post</a>.</p>

<h3>Reloading the Environment</h3>

<p>If you make changes to your app while still in the console, you will need to
reload your console session with the <code>reload!</code> command. You will also need to
reinstantiate objects which existed prior to the reload for them to recognize
your changes.</p>

<figure class='code'><figcaption><span>reload! Example</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='ruby'><span class='line'> <span class="o">&gt;</span> <span class="n">u</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span> <span class="mi">1234</span><span class="p">;</span> <span class="kp">nil</span>
</span><span class='line'>
</span><span class='line'><span class="p">\</span><span class="c1"># changes made to User model outside the console </span>
</span><span class='line'>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">reload!</span>
</span><span class='line'><span class="no">Reloading</span><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'> <span class="o">&gt;</span> <span class="n">u</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span> <span class="mi">1234</span><span class="p">;</span> <span class="kp">nil</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Better Output</h3>

<p>At one time it seemed like everyone was writing a gem for improving the IRB
experience, but it appears like that particular endeavor has since been largely
ignored. The one project that appears to be currently active is the
<a href="https://github.com/michaeldv/awesome_print">awesome_print gem</a>.</p>

<p>I&#8217;ve used this gem in the past, and it really does improve the output and
IRB experience. It also supports <a href="https://github.com/pry/pry://github.com/pry/pry/">pry</a>.</p>

<p>In a pinch, you can format the output as YAML with the <code>y</code> command.</p>

<figure class='code'><figcaption><span>y example</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='ruby'><span class='line'> <span class="o">&gt;</span> <span class="n">y</span> <span class="nb">Array</span><span class="o">.</span><span class="n">test_list</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</span><span class='line'><span class="o">---</span>
</span><span class='line'><span class="o">-</span> <span class="mi">1</span>
</span><span class='line'><span class="o">-</span> <span class="mi">2</span>
</span><span class='line'><span class="o">-</span> <span class="mi">3</span>
</span><span class='line'><span class="o">-</span> <span class="mi">4</span>
</span><span class='line'><span class="o">-</span> <span class="mi">5</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">nil</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Avoiding Output</h3>

<p>Running commands in the console can get pretty noisy. Output follows every
command which is run. To get around this, just end your command with a semicolon
and <code>nil</code>.</p>

<figure class='code'><figcaption><span>Avoiding Output</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='ruby'><span class='line'> <span class="o">&gt;</span> <span class="n">u</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">last</span><span class="p">;</span> <span class="kp">nil</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">nil</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the above example, <code>u</code> still contains the &#8220;last&#8221; user record, it just doesn&#8217;t
print out all the output that would normally be produced.</p>

<h3>The Sandbox</h3>

<p>Sometimes it would be nice to open up a console session and mess around with the
data to see what happens. But if you do that, the data&#8217;s messed up. The solution
to that is to lunch the console with the <code>--sandbox</code> flag. When launched, you
can handle the data, tweak it, and destroy it, all without fear of harming any
of your data.</p>

<figure class='code'><figcaption><span>Rails Console Sandbox</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="n">rails</span> <span class="n">console</span> <span class="o">--</span><span class="n">sandbox</span>
</span><span class='line'>
</span><span class='line'><span class="no">Loading</span> <span class="n">development</span> <span class="n">environment</span> <span class="k">in</span> <span class="n">sandbox</span> <span class="p">(</span><span class="no">Rails</span> <span class="mi">3</span><span class="o">.</span><span class="mi">2</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="no">Any</span> <span class="n">modifications</span> <span class="n">you</span> <span class="n">make</span> <span class="n">will</span> <span class="n">be</span> <span class="n">rolled</span> <span class="n">back</span> <span class="n">on</span> <span class="nb">exit</span>
</span><span class='line'> <span class="o">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Conclusion</h2>

<p>In my workflow, the rails console is an indispensible tool. It allows me to test
out ideas, access the database, run minor tasks, and even calculate basic math.
I can&#8217;t imagine developing Rails applications without it, because I know how
painful it is to be deprived of such a tool in other languages and frameworks.</p>

<p>What are your favorite IRB and console tricks?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Validating Booleans]]></title>
    <link href="http://samuelmullen.com/2012/06/validating-booleans/"/>
    <updated>2012-06-14T13:49:00-05:00</updated>
    <id>http://samuelmullen.com/2012/06/validating-booleans</id>
    <content type="html"><![CDATA[<p>I ran into an instance today wherein I needed to validate that a boolean field was either <code>true</code> or <code>false</code> and not <code>null</code>. I tried using <code>validates :fieldname, :presence =&gt; true</code>, but since <code>:presence</code> uses <code>#blank?</code> under the hood, it was reading <code>false</code> as not being present. (Why is <code>false</code> considered blank?)</p>

<p>Anyway, I needed a validator to test whether an attribute was either true or
false and I couldn&#8217;t find anything among the standard validators, so I wrote my
own.</p>

<p>Just plop this file in your app&#8217;s <code>lib/validators</code> directory.</p>

<figure class='code'><figcaption><span>truthiness_validator.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">TruthinessValidator</span> <span class="o">&lt;</span> <span class="no">ActiveModel</span><span class="o">::</span><span class="no">EachValidator</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">validate_each</span><span class="p">(</span><span class="n">record</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="k">unless</span> <span class="n">value</span> <span class="o">==</span> <span class="kp">true</span> <span class="o">||</span> <span class="n">value</span> <span class="o">==</span> <span class="kp">false</span>
</span><span class='line'>      <span class="n">record</span><span class="o">.</span><span class="n">errors</span><span class="o">[</span><span class="n">attribute</span><span class="o">]</span> <span class="o">&lt;&lt;</span> <span class="s2">&quot;must be true or false&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>In your model, add the validation like so:</p>

<figure class='code'><figcaption><span>some_model.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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">SomeModel</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">validates</span> <span class="ss">:field_name</span><span class="p">,</span> <span class="ss">:truthiness</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>For more information on writing validators, see
<a href="http://databasically.com/2010/11/08/gettings-started-with-custom-rails3-validators/">Getting Started with Custom Rails3 Validators</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why It's Okay to Write Crummy Code]]></title>
    <link href="http://samuelmullen.com/2012/05/why-its-okay-to-write-crummy-code/"/>
    <updated>2012-05-25T10:03:00-05:00</updated>
    <id>http://samuelmullen.com/2012/05/why-its-okay-to-write-crummy-code</id>
    <content type="html"><![CDATA[<p>There is an inherent need in good developers to write &#8220;good&#8221; code. We continually look for ways to simplify, enhance, improve, and speed up the code we - or others - have written. We also look back on our older code and shake our head in disgust.</p>

<p>Perhaps it is because of this love of &#8220;good&#8221; code or perhaps it is because of what we know we can do in more familiar languages, or perhaps, even, it&#8217;s because we know what we don&#8217;t know, but it seems there is a sort of barrier between learning and using a new technology (by technology I mean a language, framework, methodology, etc.).</p>

<p>Rather than doing, sometimes it&#8217;s easier to continue reading and researching. &#8220;Just one more section&#8221;, &#8220;just one more chapter&#8221;, &#8220;just one more book&#8221;. It makes sense, of course, we have to have some sort of foundation upon which to build; and so we read a little bit more.</p>

<p>But the argument that we need to learn just a little more before we begin doing is a hollow argument: we learn far more by doing than by reading. Think back upon when you&#8217;ve learned the most: was it when you were in class listening to lectures, or in lab applying what you learned; when you read the chapter, or when you completed the exercises; when you read the API, or tried to implement it?</p>

<p>But the &#8220;doing&#8221; is difficult, we end up hitting speed bumps and roadblocks, we go in circles in roundabouts, and occasionally we even find ourselves in dark alleyways or hit dead ends, but eventually, if we persevere, we find our way onto the right path. It&#8217;s those little success which make all the difference. Finding out what doesn&#8217;t work is a learning experience all to itself, but it is the successes, both large and small, which make all the difference. It is the successes which provide us with the energy and motivation to continue on.</p>

<p>With this in mind, go ahead and write crummy code. Write obtuse, ugly, poorly thought out code. Disregard methodologies and best practices, violate principles, ignore everything which hinders you from learning and playing. Fool around and explore, find the speed bumps, the roadblocks, and even the dark alleyways; it&#8217;s how you learn and how you eventually succeed.</p>

<p>So to Hell with being ready and writing perfect code; go have fun. If you&#8217;re not having fun, you&#8217;re doing it wrong anyway.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Evolution of an Apple Fanboy]]></title>
    <link href="http://samuelmullen.com/2012/05/evolution-of-an-apple-fanboy/"/>
    <updated>2012-05-14T07:03:00-05:00</updated>
    <id>http://samuelmullen.com/2012/05/evolution-of-an-apple-fanboy</id>
    <content type="html"><![CDATA[<ul>
<li>1997 - 2010: I was an anti-apple, Linux fanboy.</li>
<li>18 months ago: I bought an iPad (16GB) instead of a Kindle at my wife&#8217;s behest</li>
<li>17 months ago: I won a second iPad (32GB)</li>
<li>1 year ago: I bought a MacBook Pro so I could be on the same page as my Ruby coworkers</li>
<li>Six months ago: I won a 13&#8221; MacBook Air</li>
<li>10 days ago: I started learning Objective-C and iOS development.</li>
<li>This morning: I searched out iOS developer podcasts</li>
<li>Currently: I&#8217;m planning on getting the next iPhone, the new iPad, and the Apple TV (little black box).</li>
</ul>


<p>Things got really weird somewhere in that mess, and I have no idea how it happened. I still see myself as a Linux guy, but I can no longer deny my appreciation for Apple products. Who says I&#8217;m closed-minded?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling Objective-C Programs at the Command Line]]></title>
    <link href="http://samuelmullen.com/2012/05/compiling-objective-c-programs-at-the-command-line/"/>
    <updated>2012-05-13T22:19:00-05:00</updated>
    <id>http://samuelmullen.com/2012/05/compiling-objective-c-programs-at-the-command-line</id>
    <content type="html"><![CDATA[<p>I&#8217;ve switched my learning efforts from JavaScript frameworks to iOS development. I&#8217;m using my morning 30 minute period of time to study the &#8221;<a href="http://www.amazon.com/gp/product/0321706285/ref=as_li_ss_tl?ie=UTF8&amp;tag=themulfam-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321706285">Objective-C Programming</a>&#8221; book from &#8221;<a href="https://www.bignerdranch.com/">Big Nerd Ranch</a>&#8221;. I&#8217;m also taking an hour out of my normal billable hours to study Big Nerd Ranch&#8217;s &#8221;<a href="http://www.amazon.com/gp/product/0321821521/ref=as_li_ss_tl?ie=UTF8&amp;tag=themulfam-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321821521">iOS Programming</a>&#8221;.</p>

<p>Many of the chapters of the &#8220;Objective-C Programming&#8221; book have exercises at the end to help the student cement what he or she has learned. As I&#8217;ve been going through them, I&#8217;ve become very frustrated with needing to open a new XCode project for each exercise. It would be a lot easier for me just to code up the solutions in Vim and compile them at the command line.</p>

<p>I wasn&#8217;t sure it was possible, but after a quick Googling, I ran across a <a href="http://www.informit.com/articles/article.aspx?p=1819492">sample chapter from &#8220;Programming in Objective C&#8221;</a> which had the solution.</p>

<p>In a nutshell:</p>

<ol>
<li>Create an objective-c implimentation file (*.m)</li>
<li>Add the necessary code</li>
<li>Compile it using the following command</li>
</ol>


<figure class='code'><figcaption><span>Compiling obj-c Programs</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='bash'><span class='line'><span class="nv">$ </span>clang -fobjc-arc –framework Foundation filename.m -o output_filename
</span></code></pre></td></tr></table></div></figure>


<p>For those who are familiar with GCC or other command-line compilers, that should be enough to get you started. For the rest of you, you may want to stick with XCode. Seriously, I&#8217;m not sure how long I&#8217;ll stick with this method.</p>

<blockquote><p>You should note that writing and debugging Objective-C programs from the terminal is a valid approach. However, it’s not a good long-term strategy. If you want to build Mac OS X or iOS applications, there’s more to just the executable file that needs to be “packaged” into an application bundle. It’s not easy to do that from the Terminal application, and it’s one of Xcode’s specialties. Therefore, I suggest you start learning to use Xcode to develop your programs. There is a learning curve to do this, but the effort will be well worth it in the end.</p></blockquote>

<p>As stated at the end of the linked article (chapter), this is not something you&#8217;ll want to use to build MacOS or iOS applications. I&#8217;m only doing things this way to simplify completion of exercises.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[thirty minutes a day]]></title>
    <link href="http://samuelmullen.com/2012/04/thirty-minutes-a-day/"/>
    <updated>2012-04-14T07:14:00-05:00</updated>
    <id>http://samuelmullen.com/2012/04/thirty-minutes-a-day</id>
    <content type="html"><![CDATA[<p>Thirty minutes doesn&#8217;t seem like much time, especially for programmers. It takes that much time just to get into the &#8220;zone&#8221;. It takes at least that much time just to research some libraries or problems. You can read a chapter of a book in thirty minutes, but can you imagine working on a project for only thirty minutes a day?</p>

<p>But if thirty minutes a day is the only amount of time you have, the question changes from &#8220;can you?&#8221; to &#8220;do you want to?&#8221;.</p>

<p>I get around thirty minutes to myself in the mornings before my family wakes up and I have to get everyone ready for their day. For a long while I used that precious amount of time to read blog posts, twitter, email, or pitter it away in some other manner. I&#8217;ve not done that this year.</p>

<p>Since January, I&#8217;ve used those thirty minutes to write ten blog posts and read three books. It doesn&#8217;t sound like much, but if I were to keep up that pace, I would have forty blog posts written and 12 books read by the end of the year. That&#8217;s not too shabby&#8230;If I were to keep up the pace.</p>

<p>I&#8217;ve been wanting to learn <a href="http://documentcloud.github.com/backbone/">Backbone.js</a> for a while now, but the free time I have during the rest of my day involves my family. The only time I have is my thirty minute window. So I&#8217;ve decided to use that time to work on a project using backbone.</p>

<p>I won&#8217;t have time to write - although I have found time this Saturday morning - and I won&#8217;t have as much time to read, but I really want to learn backbone.js.</p>

<p>I get thirty minutes a day. It&#8217;s not much, but it&#8217;s mine, and it&#8217;s not wasted.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ActiveRecord::Base.store - Pros, Cons, and Usage]]></title>
    <link href="http://samuelmullen.com/2012/04/activerecord-base-store-pros-cons-usage/"/>
    <updated>2012-04-02T06:20:00-05:00</updated>
    <id>http://samuelmullen.com/2012/04/activerecord-base-store-pros-cons-usage</id>
    <content type="html"><![CDATA[<p>In <a href="http://samuelmullen.com/2012/03/understanding-activerecord-base-dot-explain/">my post on <code>ActiveRecord</code>&#8217;s new <code>explain</code>method</a>, I mentioned there were &#8220;two&#8221;  features in ActiveRecord (introduced in Rails 3.2) I wanted to look at. This post covers the second method: <code>ActiveRecord::Base.store</code>.</p>

<p><code>Store</code> provides a simple means of accessing and storing key/value pairs related to a model. The <a href="http://api.rubyonrails.org/classes/ActiveRecord/Store.html">API documentation</a> uses the example of a <code>User</code> model which has settings. &#8220;Settings&#8221; may not in themselves warrant their own model, but there still needs to be a means of accessing them. This is where <code>Store</code> comes in to play.</p>

<p>Behind the curtain, store is just a <code>Hash</code> which gets serialized and deserialized upon save and load. It has a few accessors added in to make it look less hashy, but the hash is still there nonetheless as we&#8217;ll see later.</p>

<p>Prior to Rails 3.2, if you needed this functionality you had three choices: 1) implement it yourself; 2) Muck up your table with a bunch of extra fields; 3) create another table to store all the fields.</p>

<p>For the sake of laziness, I&#8217;ll use the same project I used in <a href="http://samuelmullen.com/2012/03/understanding-activerecord-base-dot-explain/">my previous post</a>: <a href="https://github.com/samullen/ar_explain">https://github.com/samullen/ar_explain</a></p>

<h2>Setup</h2>

<p>In our example, we&#8217;re going to allow users to toggle the different types of email they want to receive. The first thing we&#8217;ll need to do is add the field in which to store the settings.</p>

<figure class='code'><figcaption><span>Add contact_settings to Users</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="k">class</span> <span class="nc">AddContactSettingsToUsers</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">change</span>
</span><span class='line'>    <span class="n">add_column</span> <span class="ss">:users</span><span class="p">,</span> <span class="ss">:contact_settings</span><span class="p">,</span> <span class="ss">:string</span><span class="p">,</span> <span class="ss">:limit</span> <span class="o">=&gt;</span> <span class="mi">4096</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>You&#8217;ll likely need some space for your settings so set the <code>string</code> limit to something large. Alternatively, you can use <code>text</code> instead of <code>string</code> if you like, but I tend to run more conservative.</p>

<p>Next, we&#8217;ll need to let the user model know we&#8217;ll be using the <code>contact_settings</code> field as a store.</p>

<figure class='code'><figcaption><span>User Model</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="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">store</span> <span class="ss">:contact_settings</span><span class="p">,</span> <span class="n">accessors</span><span class="p">:</span> <span class="o">[</span> <span class="ss">:daily_email</span><span class="p">,</span> <span class="ss">:weekly_email</span><span class="p">,</span> <span class="ss">:account_email</span> <span class="o">]</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:owner_restaurants</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Well that wasn&#8217;t too difficult.</p>

<h3>Usage</h3>

<p>Like I said, the field used as the store is really just a hash. You can see that in the console when I retrieve the first user record:</p>

<figure class='code'><figcaption><span>Retrieving the First User</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">001</span> <span class="o">&gt;</span> <span class="n">u</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">first</span>
</span><span class='line'>  <span class="no">User</span> <span class="no">Load</span> <span class="p">(</span><span class="mi">0</span><span class="o">.</span><span class="mi">3</span><span class="n">ms</span><span class="p">)</span>  <span class="no">SELECT</span> <span class="sb">`users`</span><span class="o">.</span><span class="n">*</span> <span class="no">FROM</span> <span class="sb">`users`</span> <span class="no">LIMIT</span> <span class="mi">1</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="c1">#&lt;User id: 1, name: &quot;John Galt&quot;, email: &quot;john@galtsgulch.com&quot;, created_at: &quot;2012-03-08 01:50:22&quot;, updated_at: &quot;2012-03-08 01:50:22&quot;, contact_settings: {}&gt; </span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">002</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">contact_settings</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="p">{}</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">003</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">contact_settings</span><span class="o">.</span><span class="n">class</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="no">Hash</span>
</span></code></pre></td></tr></table></div></figure>


<p>Instead of accessing the attributes through <code>contact_settings</code> as you would a normal <code>Hash</code>, you access them as if they were attributes on the model itself.</p>

<figure class='code'><figcaption><span>Accessing Store Attributes</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='ruby'><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">005</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">weekly_email</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">006</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">account_email</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">007</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">daily_email</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="kp">false</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">00</span><span class="mi">8</span> <span class="o">&gt;</span> <span class="n">u</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="c1">#&lt;User id: 1, name: &quot;John Galt&quot;, email: &quot;john@galtsgulch.com&quot;, created_at: &quot;2012-03-08 01:50:22&quot;, updated_at: &quot;2012-03-08 01:50:22&quot;, contact_settings: {:weekly_email=&gt;true, :account_email=&gt;true, :daily_email=&gt;false}&gt; </span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">00</span><span class="mi">9</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">contact_settings</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:weekly_email</span><span class="o">=&gt;</span><span class="kp">true</span><span class="p">,</span> <span class="ss">:account_email</span><span class="o">=&gt;</span><span class="kp">true</span><span class="p">,</span> <span class="ss">:daily_email</span><span class="o">=&gt;</span><span class="kp">false</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As mentioned earlier, <code>store</code> fields are just hashes. This means you can access them and use methods on them just like any other hash. You can even add attributes not defined in the store.</p>

<figure class='code'><figcaption><span>Accessing Store as a Hash</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="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">010</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">contact_settings</span><span class="o">[</span><span class="ss">:foo</span><span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;bar&quot;</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p125</span> <span class="p">:</span><span class="mo">012</span> <span class="o">&gt;</span> <span class="n">u</span><span class="o">.</span><span class="n">contact_settings</span>
</span><span class='line'> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:weekly_email</span><span class="o">=&gt;</span><span class="kp">true</span><span class="p">,</span> <span class="ss">:account_email</span><span class="o">=&gt;</span><span class="kp">true</span><span class="p">,</span> <span class="ss">:daily_email</span><span class="o">=&gt;</span><span class="kp">false</span><span class="p">,</span> <span class="ss">:foo</span><span class="o">=&gt;</span><span class="s2">&quot;bar&quot;</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we were to save the record and look at it in the database (without <code>:foo =&gt; "bar"</code>), it would look like this.</p>

<figure class='code'><figcaption><span>User Record</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='sql'><span class='line'><span class="n">mysql</span><span class="o">&gt;</span> <span class="k">select</span> <span class="o">*</span> <span class="k">from</span> <span class="n">users</span><span class="p">;</span>
</span><span class='line'><span class="o">+</span><span class="c1">----+--------------+----------------------+---------------------+---------------------+-------------------------------------------------------------------+</span>
</span><span class='line'><span class="o">|</span> <span class="n">id</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">created_at</span>          <span class="o">|</span> <span class="n">updated_at</span>          <span class="o">|</span> <span class="n">contact_settings</span>                                                  <span class="o">|</span>
</span><span class='line'><span class="o">+</span><span class="c1">----+--------------+----------------------+---------------------+---------------------+-------------------------------------------------------------------+</span>
</span><span class='line'><span class="o">|</span>  <span class="mi">1</span> <span class="o">|</span> <span class="n">John</span> <span class="n">Galt</span>    <span class="o">|</span> <span class="n">john</span><span class="o">@</span><span class="n">galtsgulch</span><span class="p">.</span><span class="n">com</span>  <span class="o">|</span> <span class="mi">2012</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">08</span> <span class="mi">01</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">22</span> <span class="o">|</span> <span class="mi">2012</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">04</span> <span class="mi">11</span><span class="p">:</span><span class="mi">23</span><span class="p">:</span><span class="mi">47</span> <span class="o">|</span> <span class="c1">---</span>
</span><span class='line'><span class="p">:</span><span class="n">weekly_email</span><span class="p">:</span> <span class="k">true</span>
</span><span class='line'><span class="p">:</span><span class="n">account_email</span><span class="p">:</span> <span class="k">true</span>
</span><span class='line'><span class="p">:</span><span class="n">daily_email</span><span class="p">:</span> <span class="k">false</span>
</span><span class='line'> <span class="o">|</span>
</span><span class='line'><span class="o">|</span>  <span class="mi">2</span> <span class="o">|</span> <span class="n">Howard</span> <span class="n">Roark</span> <span class="o">|</span> <span class="n">howard</span><span class="o">@</span><span class="n">architect</span><span class="p">.</span><span class="n">com</span> <span class="o">|</span> <span class="mi">2012</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">08</span> <span class="mi">01</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">22</span> <span class="o">|</span> <span class="mi">2012</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">08</span> <span class="mi">01</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">22</span> <span class="o">|</span> <span class="k">NULL</span>                                                              <span class="o">|</span>
</span><span class='line'><span class="o">+</span><span class="c1">----+--------------+----------------------+---------------------+---------------------+-------------------------------------------------------------------+</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Pros</h2>

<p>I think many of the &#8220;pros&#8221; for ActiveRecord.store are pretty obvious: it eliminates the need for yet another table or extra fields; simplifies adding new attributes; they work just like normal model attributes. Did I mention that validations work on Store attributes? They do, and <a href="https://plus.google.com/117671343455128545584">Rafal Wrzochol</a> has <a href="http://blog.rawonrails.com/2012/02/using-activerecordstore-with-rails-32.html">a great write up on using them</a></p>

<p>So what are the drawbacks?</p>

<h2>Cons</h2>

<p>There are a number of philosophical reasons against using the new Store feature. The main argument against is that the data really isn&#8217;t normalized. But we&#8217;re using Rails, which means we have a tendency to abuse normalization for the sake of the application anyway.</p>

<p>Another minus is that dirty attributes don&#8217;t work quite as well in the Store. For instance, you can&#8217;t call <code>User#weekly_email_changed?</code>. The only thing you can do is check if the Store field has changed (e.g. <code>User#contact_settings_changed?</code>). Again, it&#8217;s not really a huge issue and I imagine this will get resolved in future releases.</p>

<p>Really the main &#8220;con&#8221; with regard to using store - and this really is a big deal - is that you can&#8217;t perform efficient searches on the field. The only way to perform a search is by surrounding the search term with &#8220;%&#8221; characters.</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='sql'><span class='line'><span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="n">users</span> <span class="k">WHERE</span> <span class="n">contact_settings</span> <span class="k">LIKE</span> <span class="s1">&#39;%weekly_email: true%&#39;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>If the percent sign was just on the end, that would be one thing, but with the leading &#8220;%&#8221; it&#8217;s now the slowest possible way of searching the database.</p>

<p>I really think the new Store feature in Rails 3.2 is a nice feature. They&#8217;ve done it well and made its usage fairly seamless (i.e. store attributes look and act like any other attribute). If your application&#8217;s database is fairly large or if you plan on running a lot of queries against the attributes in the Store (e.g. gathering lots of metrics) you may want to use a separate table. For most applications out there, however, this is a very safe and sane solution.</p>

<h2>Further Reader</h2>

<ul>
<li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Store.html">ActiveRecord::Store</a></li>
<li><a href="https://plus.google.com/117671343455128545584/posts">Ruby on Rails 3.2 Release Notes - Section 8</a></li>
<li><a href="http://blog.rawonrails.com/2012/02/using-activerecordstore-with-rails-32.html">Using ActiveRecord::Store with Rails 3.2</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Need to Refactor]]></title>
    <link href="http://samuelmullen.com/2012/03/the-need-to-refactor/"/>
    <updated>2012-03-19T06:28:00-05:00</updated>
    <id>http://samuelmullen.com/2012/03/the-need-to-refactor</id>
    <content type="html"><![CDATA[<p>When people hear the words &#8220;ceremony&#8221; and &#8220;tradition&#8221;, their first thoughts usually involve religion. Of course it makes sense, all religions have some sort of tradition and ceremony, but while some of the traditions are prescribed, many have come about from some need or requirement. Those which have come about often continue on long after the requirement no longer exists. For example, the communion plates in most Christian churches are stacked and covered to keep the flies out. Flies have ceased to be a problem in our air conditioned mega-churches of the 21st century, but we continue to protect our crackers and grape juice from them nonetheless.</p>

<p>Businesses have ceremonies and traditions as well, only we call them &#8220;processes&#8221; and &#8220;systems&#8221;. In the world of programming we call it &#8220;technical debt&#8221;. Regardless of the name used, the only way to reduce what is unnecessary is by refactoring. <a href="http://martinfowler.com/refactoring/">The best definition of refactoring</a> that I am aware of was provided by Martin Fowler:</p>

<blockquote><p>Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a &#8216;refactoring&#8217;) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it&#8217;s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.</p></blockquote>

<p>Businesses refactor all the time, but again the terms are different: &#8220;streamlining processes&#8221;, &#8220;downsizing&#8221;, &#8220;pivoting&#8221;, and &#8220;reorg&#8221; are only a few examples. But in the world of programming, we don&#8217;t always have the luxury of refactoring like we want, and we really want to.</p>

<p>When you refactor in business, there is usually some sort of immediate, &#8220;tangible&#8221; result: Suzy, Johnny, and Sally were axed so now our expenses are <em>x</em> amount less. In programming, if I refactor a library of code, the end result may only be that I increase its readability. How do you quantify that? When it can&#8217;t be quantified, refactoring is only seen as an expense and something to be avoided, and so the technical debt grows.</p>

<p>As alluded, the results of a refactoring are not always measurable. There are certainly instances which result in measurable improvements, but improvements such as increased performance are not always important. So why allow your development staff time to refactor?</p>

<p>Although &#8220;increased performance [is] not <em>always</em> important&#8221;, it oftentimes is. With increased performance and efficiency come reduced hardware requirements, translating into either lower expenses or greater output. If you have a website, it means a better user experience, greater retention, and increased revenues. Look at some of these <a href="http://radar.oreilly.com/2009/07/velocity-making-your-site-fast.html">stats provided from Velocity 2009</a>:</p>

<ul>
<li><a href="http://bing.com">Bing</a> - Bing found that a 2 second slowdown changed queries/user by -1.8% and revenue/user by -4.3%. Google Search found that a 400 millisecond delay resulted in a -0.59% change in searches/user</li>
<li><a href="http://google.com">Google</a> - One experiment increased the number of search results per page from 10 to 30, with a corresponding increase in page load times from 400 milliseconds to 900 milliseconds. This resulted in a 25% drop-off in first result page searches.</li>
<li><a href="http://shopzilla.com">Shopzilla</a> - A year-long performance redesign resulted in a 5 second speed up (from ~7 seconds to ~2 seconds). This resulted in a 25% increase in page views, a 7-12% increase in revenue, and a 50% reduction in hardware.</li>
</ul>


<p>But application performance is only part of the picture, if your application or system has customers - and it does - then those customers are going to want the occasional change made or problems addressed. The time it takes to accommodate the request is proportional to the complexity of the system (i.e. the more complex the code, the longer it&#8217;s going to take).</p>

<p>By providing your development staff time to refactor, you not only shore up an application&#8217;s foundation, you reduce the cost of future development. In other words, decreased complexity equates to increased productivity, resulting in happier customers.</p>

<p>There is one more consideration with regard to encouraging regular refactoring, and if the truth were to be told, it&#8217;s my whole motive for writing this post: working with code that has a high technical debt is miserable and it&#8217;s a morale killer. When you know that each new feature request or bug fix that comes in will require sifting through years of spaghetti code, the sense of dread becomes palpable. Who wants to work in a system where any change, any minor tweak, could result in hours of bug hunts, shoring up crumbling foundations, and frustration? So developers avoid and procrastinate, and sometimes we start looking for a new job and a new system.</p>

<p>I&#8217;ve worked in shops where management allowed the development staff to either completely rewrite or perform a massive overhaul on a system, and the immediate improvement in developer morale was dramatic. We began talking about work again, how to do things right, how to make it faster, we began caring about what the customer wanted again, and we weren&#8217;t dreading coming to work each day.</p>

<p>Of course, I&#8217;ve also worked in shops where refactoring was too &#8220;expensive&#8221; for the company. It&#8217;s one thing to &#8220;overhaul&#8221; a project which has been neglected for too long, it&#8217;s quite another to have to build upon what is already there. It&#8217;s like the difference between cleaning a house owned by a &#8220;hoarder&#8221;, and being forced to live in one.</p>

<p>Technical debt creeps in to every project, and that&#8217;s okay. The point is to manage it, and the only way to manage it is by regularly refactoring your code and your design. If allowances are made for refactoring with each bug fix or new feature, keeping the technical debt low is a small investment. If allowances are not made, well, it&#8217;s kinda like the differences between patching a house&#8217;s foundation and mudjacking it. Either address the problems in the foundation as they arise or watch the house it&#8217;s supporting slowly come crashing down.</p>

<h2>Further Reading</h2>

<ul>
<li>You can&#8217;t refactor without tests. Find out why <a href="http://samuelmullen.com/2011/12/tests-are-pain/">Tests are Pain</a></li>
<li>You should be reading <a href="http://martinfowler.com/">Martin Fowler</a></li>
<li>You should <a href="http://www.extremeprogramming.org/rules/refactor.html">Refactor Mercilessly</a></li>
<li>More than you really wanted to know on <a href="http://www.martinfowler.com/bliki/TechnicalDebt.html">Technical Debt</a> and <a href="http://www.codinghorror.com/blog/2009/02/paying-down-your-technical-debt.html">Paying Down Technical Debt</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Understanding ActiveRecord::Base.explain in Rails 3.2]]></title>
    <link href="http://samuelmullen.com/2012/03/understanding-activerecord-base-dot-explain/"/>
    <updated>2012-03-07T19:23:00-06:00</updated>
    <id>http://samuelmullen.com/2012/03/understanding-activerecord-base-dot-explain</id>
    <content type="html"><![CDATA[<p>With the <a href="http://guides.rubyonrails.org/3_2_release_notes.html">release of Rails 3.2</a>, comes a host of new features. There are two in particular in ActiveRecord which seem to have attracted a lot of attention, so I thought I would look at them each more closely. This post, as you have no doubt realized from the title, is focused on <code>ActiveRecord::Base.explain</code></p>

<p>This may come as a shock, but <code>explain</code> is only <em>new</em> in Ruby on Rails; it - or similar features - has been around in the database world forever. Stated most simple, &#8220;The <code>EXPLAIN</code> statement provides information about the execution plan for a <code>SELECT</code> statement.&#8221; (<a href="http://dev.mysql.com/doc/refman/5.6/en/explain-output.html">MySQL 7.8.2. EXPLAIN Output Format</a>) In other words, when you <code>explain</code> a query, the database returns information about how it is going to go about finding all the data. It&#8217;s a very useful tool which can help developers uncover why queries are running slowly and what might be done to speed them up.</p>

<p>Let&#8217;s look at an example so we can see the value of <code>explain</code>.</p>

<h2>Schema Setup</h2>

<p>We&#8217;ll need a couple tables with data for our example. I&#8217;ll use users and user_restaurants.</p>

<figure class='code'><figcaption><span>The users Table</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">CreateUsers</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">change</span>
</span><span class='line'>    <span class="n">create_table</span> <span class="ss">:users</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:name</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:email</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">timestamps</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>


<figure class='code'><figcaption><span>The user_restaurants Table</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='ruby'><span class='line'><span class="k">class</span> <span class="nc">CreateUserRestaurants</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">change</span>
</span><span class='line'>    <span class="n">create_table</span> <span class="ss">:user_restaurants</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">integer</span> <span class="ss">:user_id</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:name</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">boolean</span> <span class="ss">:like</span>
</span><span class='line'>      <span class="n">t</span><span class="o">.</span><span class="n">timestamps</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>The users table is self-explanatory. user_restaurants, on the other hand, is a table of restaurants a user likes (I guess that&#8217;s self-explanatory as well.)</p>

<p>Well need a couple of users, so let&#8217;s add them here.</p>

<figure class='code'><figcaption><span>Populate the users Table</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='ruby'><span class='line'><span class="k">class</span> <span class="nc">AddUsers</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">up</span>
</span><span class='line'>    <span class="no">User</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s2">&quot;John Galt&quot;</span><span class="p">,</span> <span class="ss">:email</span> <span class="o">=&gt;</span> <span class="s2">&quot;john@galtsgulch.com&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="no">User</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s2">&quot;Howard Roark&quot;</span><span class="p">,</span> <span class="ss">:email</span> <span class="o">=&gt;</span> <span class="s2">&quot;howard@architect.com&quot;</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>And our users will want to &#8220;like&#8221; or &#8220;dislike&#8221; restaurants, so we&#8217;ll need to create the liked restaurant records. I&#8217;ll name the restaurants &#8220;Restaurant n&#8221; to save myself from having to come up with 100,000 restaurant names. We&#8217;ll split those restaurants between the two users and define 20% of them to be restaurants the users did not &#8220;like&#8221;.</p>

<figure class='code'><figcaption><span>Populate the user_restaurants Table</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">AddRestaurants</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">up</span>
</span><span class='line'>    <span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span>
</span><span class='line'>
</span><span class='line'>    <span class="mi">100000</span><span class="o">.</span><span class="n">times</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'>      <span class="no">UserRestaurant</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user_id</span> <span class="o">=&gt;</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">?</span> <span class="n">users</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">id</span> <span class="p">:</span> <span class="n">users</span><span class="o">.</span><span class="n">second</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s2">&quot;Restaurant </span><span class="si">#{</span><span class="n">i</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="ss">:like</span> <span class="o">=&gt;</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">?</span> <span class="kp">false</span> <span class="p">:</span> <span class="kp">true</span><span class="p">)</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>


<h2>Model Associations</h2>

<p>The last thing we have to do is associate the users table to the user_restaurants table.</p>

<figure class='code'><figcaption><span>Model Associations</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:user_restaurants</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">UserRestaurant</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
</span><span class='line'>  <span class="n">belongs_to</span> <span class="ss">:user</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Explain Round 1</h2>

<p>Okay, we&#8217;ve created our tables and populated them with data, and we&#8217;ve got our models and associations. Let&#8217;s run a query to find &#8220;John Galt&#8217;s&#8221; favorite restaurants and see what&#8217;s going on.</p>

<figure class='code'><figcaption><span>users / user_restaurants join</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='ruby'><span class='line'><span class="no">User</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="ss">:email</span> <span class="o">=&gt;</span> <span class="s2">&quot;john@galtsgulch.com&quot;</span><span class="p">)</span><span class="o">.</span>
</span><span class='line'>  <span class="n">joins</span><span class="p">(</span><span class="ss">:user_restaurants</span><span class="p">)</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="s2">&quot;user_restaurants.like = 1&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>When I run this from the Rails Console, <code>explain</code> kicks in automatically and returns the table below (this is output from MySQL, PostgreSQL looks and is worded differently). &#8220;Active Record monitors queries and if they take more than [<code>config.active_record.auto_explain_threshold_in_seconds</code>] their query plan will be logged using warn.&#8221; (<a href="http://weblog.rubyonrails.org/2011/12/6/what-s-new-in-edge-rails-explain">What&#8217;s new in Edge Rails: EXPLAIN</a>)</p>

<p><em>Note:</em> To manually execute explain on a query, just append <code>.explain</code> to it.</p>

<figure class='code'><figcaption><span>Explain Output v1</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='bash'><span class='line'>+----+-------------+------------------+------+---------------+------+---------+------+--------+--------------------------------+
</span><span class='line'>| id | select_type | table            | <span class="nb">type</span> | possible_keys | key  | key_len | ref  | rows   | Extra                          |
</span><span class='line'>+----+-------------+------------------+------+---------------+------+---------+------+--------+--------------------------------+
</span><span class='line'>|  1 | SIMPLE      | users            | ALL  | PRIMARY       | NULL | NULL    | NULL |      2 | Using where                    |
</span><span class='line'>|  1 | SIMPLE      | user_restaurants | ALL  | NULL          | NULL | NULL    | NULL | 100041 | Using where; Using join buffer |
</span><span class='line'>+----+-------------+------------------+------+---------------+------+---------+------+--------+--------------------------------+
</span></code></pre></td></tr></table></div></figure>


<p>The columns you&#8217;re going to be most interested in are &#8220;key&#8221; and &#8220;rows&#8221;. The &#8220;key&#8221; columns lists the indexes, if any, the database uses to find data. &#8220;The rows column indicates the number of rows MySQL believes it must examine to execute the query.&#8221; <a href="http://dev.mysql.com/doc/refman/5.6/en/explain-output.html">7.8.2. EXPLAIN Output Format</a></p>

<p>In the output above, we can see that the query would use no indexes, and would have to search through 100,041 rows (Hey, that&#8217;s more rows than are in the database). &#8220;For InnoDB tables, this number is an estimate, and may not always be exact.&#8221; <a href="http://dev.mysql.com/doc/refman/5.6/en/explain-output.html">7.8.2. EXPLAIN Output Format</a></p>

<h2>Database Refactoring</h2>

<p>Let&#8217;s add some indexes to a couple of the key table columns and see if we can&#8217;t reduce the pain.</p>

<figure class='code'><figcaption><span>AddTableIndexes</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">AddTableIndexes</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">up</span>
</span><span class='line'>    <span class="n">add_index</span> <span class="ss">:users</span><span class="p">,</span> <span class="ss">:email</span>
</span><span class='line'>    <span class="n">add_index</span> <span class="ss">:user_restaurants</span><span class="p">,</span> <span class="ss">:user_id</span>
</span><span class='line'>    <span class="n">add_index</span> <span class="ss">:user_restaurants</span><span class="p">,</span> <span class="ss">:like</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>Now, when we run our query it still issues an explain (because it&#8217;s a really stupid example), but we now see how adding the indexes improved performance.</p>

<figure class='code'><figcaption><span>Explain Output v2</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='bash'><span class='line'>+----+-------------+------------------+------+------------------------------------------------------------------+-----------------------------------+---------+---------------------+-------+-------------+
</span><span class='line'>| id | select_type | table            | <span class="nb">type</span> | possible_keys                                                    | key                               | key_len | ref                 | rows  | Extra       |
</span><span class='line'>+----+-------------+------------------+------+------------------------------------------------------------------+-----------------------------------+---------+---------------------+-------+-------------+
</span><span class='line'>|  1 | SIMPLE      | users            | ref  | PRIMARY,index_users_on_email                                     | index_users_on_email              | 768     | const               |     1 | Using where |
</span><span class='line'>|  1 | SIMPLE      | user_restaurants | ref  | index_user_restaurants_on_user_id,index_user_restaurants_on_like | index_user_restaurants_on_user_id | 5       | ar_explain.users.id | 25010 | Using where |
</span><span class='line'>+----+-------------+------------------+------+------------------------------------------------------------------+-----------------------------------+---------+---------------------+-------+-------------+
</span></code></pre></td></tr></table></div></figure>


<h2>Improvements</h2>

<p>Looking at the new <code>explain</code> output, we see two things: 1) the query is now using the indexes we created; and 2) the number of potential rows that have to be searched through has been quartered.</p>

<p>It may not seem like a big deal that we went from two to one for the search by email, but imagine if you had 10,000 users, and by using the index here it was still able to go directly to that one record. And if the data we were searching for was something other than a boolean, as it is with the &#8220;like&#8221; column, we would be able to further reduce the number of potential records through which were searched.</p>

<p>Of course, if we didn&#8217;t have our handy dandy <code>explain</code> tool we might not even have realized it was a database problem to begin with.</p>

<p>As I alluded to in the beginning, there is nothing new about <code>explain</code>. It&#8217;s an excellent tool which provides insight into what course of action the database will take to find the data you are searching for. I&#8217;ve been using <code>explain</code> for some time directly from the database command line; it&#8217;s nice to finally have it accessible from the Rails Console.</p>

<p>I&#8217;ve purposely ignored the various implementations and configurations of <code>explain</code> in order to focus more on why it&#8217;s useful. If you are interested in configuration and implementation, check out <a href="http://weblog.rubyonrails.org/2011/12/6/what-s-new-in-edge-rails-explain">What&#8217;s new in Edge Rails: EXPLAIN</a>.</p>

<h2>Further Reader</h2>

<ul>
<li><a href="http://weblog.rubyonrails.org/2011/12/6/what-s-new-in-edge-rails-explain">What&#8217;s new in Edge Rails: EXPLAIN</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.6/en/explain-output.html">MySQL: EXPLAIN Output Format</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/using-explain.html">MySQL: Optimizing Queries with EXPLAIN</a></li>
<li><a href="http://www.sqlite.org/eqp.html">SQLite: EXPLAIN Query Plan</a></li>
<li><a href="http://www.postgresql.org/docs/current/static/using-explain.html">PostgreSQL: Using EXPLAIN</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Left Pad Zeroes in JavaScript]]></title>
    <link href="http://samuelmullen.com/2012/03/left-pad-zeroes-in-javascript/"/>
    <updated>2012-03-01T08:44:00-06:00</updated>
    <id>http://samuelmullen.com/2012/03/left-pad-zeroes-in-javascript</id>
    <content type="html"><![CDATA[<p>I was recently working on a project wherein I needed to present a number padded on the left with zeroes. Most languages have some sort of formatter method which would allow you to do that, but not <a href="https://developer.mozilla.org/en/JavaScript">JavaScript</a>. Instead of following the path of other languages, JavaScript opts to give the developer opportunities to be creative.</p>

<p>After a quick search, I ran across <a href="http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/">a very elegant solution</a> from <a href="http://www.codigomanso.com/">Pau Sanchez</a> which I have turned into a the following function and added some smarts to allow the padding to be changed as necessary.</p>

<figure class='code'><figcaption><span>lpad</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">lpad</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">padding</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">zeroes</span> <span class="o">=</span> <span class="s2">&quot;0&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">padding</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="nx">zeroes</span> <span class="o">+=</span> <span class="s2">&quot;0&quot;</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="p">(</span><span class="nx">zeroes</span> <span class="o">+</span> <span class="nx">value</span><span class="p">).</span><span class="nx">slice</span><span class="p">(</span><span class="nx">padding</span> <span class="o">*</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that this doesn&#8217;t account for numbers which are longer than the padding value; they get cut off. Most of the time you deal with left padded numbers, you&#8217;re dealing with a fixed width and this isn&#8217;t going to be a problem. I&#8217;ll leave it as an exercise for the user, if this is not the current case for you.</p>

<p>Here it is again in CoffeeScript:</p>

<figure class='code'><figcaption><span>lpad</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='coffeescript'><span class='line'><span class="nv">lpad = </span><span class="nf">(value, padding) -&gt;</span>
</span><span class='line'>  <span class="nv">zeroes = </span><span class="s2">&quot;0&quot;</span>
</span><span class='line'>  <span class="nx">zeroes</span> <span class="o">+=</span> <span class="s2">&quot;0&quot;</span> <span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="nx">padding</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nx">zeroes</span> <span class="o">+</span> <span class="nx">value</span><span class="p">).</span><span class="nx">slice</span><span class="p">(</span><span class="nx">padding</span> <span class="o">*</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Advice on Attracting Good Developers]]></title>
    <link href="http://samuelmullen.com/2012/02/advice-on-attracting-good-developers/"/>
    <updated>2012-02-24T19:37:00-06:00</updated>
    <id>http://samuelmullen.com/2012/02/advice-on-attracting-good-developers</id>
    <content type="html"><![CDATA[<p>During the dot-com boom of the late 90&#8217;s, much ado was made about the many perks offered by companies used to attract developers: ping pong and pool tables, catered lunches, video game rooms, alcohol, sign-on bonuses, and oh yeah, stock options! Today, some of those perks are just running gags, but others have become standard to the modern tech company.</p>

<p>A lot has changed in the past fifteen years, but not <a href="http://www.forbes.com/sites/venkateshrao/2011/12/05/the-rise-of-developeronomics/">the need for good developers</a>. The big companies still offer all the crazy benefits to attract top talent (just think of every story you&#8217;ve heard about <a href="http://www.google.com/intl/en/jobs/lifeatgoogle/">what it&#8217;s like to work at Google</a>), and sometimes they just buy companies <a href="http://www.nytimes.com/2011/05/18/technology/18talent.html?_r=1&amp;pagewanted=all">to acquire the developers therein</a>.</p>

<p>For the sake of argument, let&#8217;s assume you represent a smallish company which has established itself to some degree and you&#8217;re looking to bring on new developers - and by &#8220;new developers&#8221;, I mean developers who care about the work they produce (i.e. <a href="http://paulgraham.com/gba.html">hackers</a>). With so much demand for these developers, how do you compete?</p>

<p>It should come as no surprise that the first thing you must do is to understand how developers think. We&#8217;re a different lot. We don&#8217;t stop working just because we&#8217;ve left the office; we hack on our own projects, we do side work for more experience, and we attend user groups and contribute back to our technical communities. We live and breathe technology; it&#8217;s who we are. Understand this, and you&#8217;re halfway there. Learn to encourage this in us, and you&#8217;re home free.</p>

<p>Attracting developer <em>interest</em> really isn&#8217;t hard, it just requires putting your name and support behind what we&#8217;re interested in. Here&#8217;s just a sample of things you can do:</p>

<h3>Support Local User Groups</h3>

<p>Although developers are generally thought to be introverts, we do enjoy getting together to share ideas and new technologies. One of the ways we do this is by going to user groups, and companies which support user groups are generally seen in a favorable light.</p>

<p>Supporting user groups is cheap and easy. The simplest, and possibly the most appreciated, thing you can do is provide snacks, food, and/or beverages. It doesn&#8217;t have to be extravagant, snack trays, pizza, sandwiches, or soda is good enough.</p>

<p>Another way you can support local groups is by providing a place to meet. It&#8217;s not easy to find a meeting place once a group begins to grow, and providing that meeting spot will be greatly appreciated. What better way to get your name out there and scout new talent than to open your offices to these groups?</p>

<p>One last thing about user groups, they oftentimes need speakers, so encourage (don&#8217;t coerce) your own developers to speak.</p>

<h3>Coding Competitions and Hackathons</h3>

<p>Not only do developers get together to discuss technologies, we get together to play too. Most languages have some sort of competition (<a href="http://railsrumble.com">Rails Rumble</a>, <a href="http://nodeknockout.com/">Node Knockout</a>, <a href="http://djangodash.com">Django Dash</a>, etc.); major open source projects often have bugmashes leading up to new releases; and new projects or releases may announce <a href="http://en.wikipedia.org/wiki/Hackathon">hackathons</a> to build excitement in a product or release.</p>

<p>Support for these sort of events is similar to supporting user groups. Providing snacks and beverages, a place to meet, and your developers would all be welcomed. Your support and name will be remembered.</p>

<h3>Technology Conferences</h3>

<p>Unlike user groups and hackathons, involvement in technology conferences requires a greater investment in money, time, resources, or a combination therein. With the increased expenses, however, comes an increase in visibility.</p>

<p>Running a conference is expensive, and the organizers are usually really good about promoting sponsoring companies. Blog posts, &#8220;Sponsor&#8221; pages, <a href="http://twitter.com">Twitter</a> and <a href="http://facebook.com">Facebook</a> posts, T-shirts, and in-conference announcements are just some of the ways sponsors get promoted.</p>

<p>Whereas user groups may average ten to twenty developers, conferences often have 150 or more (some of these being &#8220;rock stars&#8221;). By supporting the conference, showing genuine interest in the developers, and by being available to talk, your company should have no problems attracting developer interest.</p>

<p>Like I said, attracting developer interest isn&#8217;t hard, but drawing us in is a little different, because we need to know that who your company appears to be lines up with who your company really is. There are at least three things your organization can do to show that you are developer-friendly: Give back to the community; Encourage a hacker culture; Support your current developers.</p>

<h3>Giving Back to the Community</h3>

<p>Chances are, your organization is using <a href="http://en.wikipedia.org/wiki/Open_source_software">open source software</a>, but chances are also that your organization is not giving back to those same projects. I&#8217;m not necessarily talking about money, although open source projects are always happy to receive contributions, I&#8217;m talking about giving code back to the community.</p>

<p>In his post, &#8221;<a href="http://intridea.com/2011/4/22/why-open-source-company-culture-is-important">Why Open Source Company Culture is Important</a>&#8221;, <a href="https://twitter.com/mbleigh">Michael Bleigh</a> expounds upon why it&#8217;s important for companies and organizations to open source as much as they can. For our purposes, this one quote says it all:</p>

<blockquote><p>Why do you care about happier developers? Well, every company should care about happy employees, but software development is a black magic mix of science, artistry, and craftsmanship. While some work can be done by &#8220;forcing it&#8221;, a good deal of that work requires inspiration and passion, something you won&#8217;t be getting out of developers that feel stifled and isolated from the development community at large.</p></blockquote>

<p>Open source makes developers happy. You want happy developers. When you open source your code, you are showing that you &#8220;get&#8221; the community and that your organization is developer-friendly.</p>

<h3>Encouraging a Hacker Culture</h3>

<p>Creating a culture within a company is no small feat. In many companies, culture arises naturally, but in others, such as <a href="http://apple.com">Apple</a>, culture is actively developed and nurtured. And while there is not enough room in this post to describe all that goes into defining company culture - and I am in no position to speak to that - I would like to provide some tips which can allow a hacker culture to be born and grow.</p>

<h4>Support Failure</h4>

<p>I have worked in companies in which there was no room for failure. It&#8217;s stifling, it&#8217;s demotivating, and it&#8217;s a horrible condition to work in. &#8220;When there is no room for failure, there is no room for creativity.&#8221;</p>

<p>Failure happens a lot in computer programming. It happens because programmers are human, but it also happens because we&#8217;re trying new ideas and we&#8217;re growing in our discipline. Failure is a part of growth, so continue to support your devs when they stumble, and celebrate them when they succeed.</p>

<h4>Support Experimentation</h4>

<p>Technology is changing rapidly, and nowhere is that more clear than in programming. By allowing your developers to experiment with new technologies, they get a feel for what direction the industry is going and how the organization will inevitably need to respond.</p>

<p>Besides allowing your developers to test out new technologies, encourage them to try out new development techniques as well. <a href="http://en.wikipedia.org/wiki/Pair_programming">Pair programming</a>, standing desks, <a href="http://en.wikipedia.org/wiki/Test-driven_development">test driven development</a>, <a href="http://agilemanifesto.org/">Agile software development</a>, and open seating arrangements can all ignite developer excitement.</p>

<p>Let&#8217;s face it, we are talking about getting your developers to try to be more productive, should there ever be a reason not to do that?</p>

<h4>Support Play Time</h4>

<p>You probably already knew this, but <a href="http://google.com">Google</a> has the concept of <a href="http://googleblog.blogspot.com/2006/05/googles-20-percent-time-in-action.html">20% time</a>. The idea is that employees are allowed to use 20% of their work time to work on side projects they find interesting . It is estimated that 50% of all of Google&#8217;s products have come out of &#8220;twenty percent&#8221; projects, including <a href="http://gmail.com">Gmail</a>, <a href="http://orkut.com">Orkut</a>, <a href="http://news.google.com">Google News</a>, and <a href="http://adsense.google.com">AdSense</a>. (<a href="http://www.quora.com/Google/What-are-the-most-innovative-projects-that-have-come-out-of-Googles-20-time">source</a>)</p>

<p>20% is a lot of time, and Google can really only do that because they are overstaffed compared to many companies. But what if you took a couple days a month (10%) to allow your developers time to work on something completely new?</p>

<p><a href="http://rednovalabs.com">Red Nova Labs</a>, a local Kansas City tech company, recently embarked on what they called <a href="http://www.rednovalabs.com/red-nova-labs-embarks-on-operation-launchpad/">Operation Launchpad</a>. The whole company took an entire week off and organized themselves into smaller teams to work on completely new business ideas. The result, as I understand it, is two completely new products which are already close to launching. What could your company do in a week?</p>

<h3>Support Your Current Developers</h3>

<p>Finally, support the developers you currently have on staff. Are we the most business-savvy people? No. Do we know technology and where it is going? Unquestionably. If you really want to support your developers, then listen to them. Yes, we&#8217;re nerds and geeks, but as John Stewart recently noted, <a href="http://www.thedailyshow.com/watch/wed-january-18-2012/ko-computer">&#8220;I believe the term you are looking for is &#8216;expert&#8217;.&#8221;</a></p>

<p>I can&#8217;t stress how important respect is. Most developers I know would rather take a cut in pay and be valued, than be payed well, but whose voice went unheard. If you do not value your developers, one of three things will happen: 1) they&#8217;ll leave (most likely); 2) they will work less on work, and more on their own interests; or 3) they will become the replaceable cogs you imagine them to be.</p>

<p>Unfortunately for your organization, it is a seller&#8217;s market. We know we are in demand and we use that knowledge to leverage what we want. What is fortunate for your organization, however, is that most competing companies don&#8217;t know how to make developers happy - pool tables and free beer just aren&#8217;t enough. Furthermore, developers don&#8217;t always know what kind of environment they be happiest in. But if your organization can show outwardly that it at least tries to support the community developers live in, and internally provide an environment in which developers can thrive, there should be no lack of good developers from which to choose.</p>

<p><em>Update 20120306:</em> So what do you do once you&#8217;ve attracted developer attention?
Jeff Atwood wrote a great post on &#8221;<a href="http://www.codinghorror.com/blog/2012/03/how-to-hire-a-programmer.html">How to Hire a Programmer</a>&#8221;. You should go read it; he&#8217;s wicked-smart.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Power Up Search in Google Chrome]]></title>
    <link href="http://samuelmullen.com/2012/02/power-up-search-in-google-chrome/"/>
    <updated>2012-02-16T13:48:00-06:00</updated>
    <id>http://samuelmullen.com/2012/02/power-up-search-in-google-chrome</id>
    <content type="html"><![CDATA[<p>I always look for ways to simplify my workflow, and one of the best ways I know is to use shortcuts: key bindings, abbreviations, aliases, cutting through neighbors&#8217; yards, etc. The other day, I ran across <a href="http://duckduckgo.com">DuckDuckGo&#8217;s</a> search shortcuts which allow you to use bang operators to localize search to specific sites. So if you know you are looking for widgets from <a href="http://en.wikipedia.org">Wikipedia</a>, you can type <code>!w widgets</code> in DuckDuckGo&#8217;s search box and see results from <a href="http://en.wikipedia.org">Wikipedia</a>.</p>

<p>In discovering DuckDuckGo&#8217;s shortcuts, I stumbled upon a similar ability in <a href="https://www.google.com/chrome">Google Chrome</a>. Normally, if you wanted to localize your search to a site such as <a href="http:github.com">GitHub</a> you would enter <code>site:github.com linux</code> in the location bar. After following the steps below, you will be able to type <code>gh linux</code>  to localize your search to GitHub.</p>

<h3>Step 1 - Preferences</h3>

<p>In Google Chrome, open up the preferences page. You can do that by typing &#8220;about:settings&#8221; in the location bar or by going through the menu.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/chrome_menu.png"></p>

<p>Once there, click the &#8220;Manage Search Engines&#8230;&#8221; button.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/manage_se_button.png"></p>

<h3>Step 2 - Edit an Existing Entry</h3>

<p>Look through the list of available sites and select one which you search frequently. Now, click on the entry in the second column and change the text to something simpler and easy to remember: if it&#8217;s &#8221;<a href="http://github.com">github.com</a>&#8221;, change it to &#8220;gh&#8221;; if it&#8217;s &#8221;<a href="http://catcht.com">catch.com</a>&#8221;, change it to &#8220;c&#8221;; etc.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/search_list.png"></p>

<h3>Step 3 - Search Using the New Shortcut</h3>

<p>Now that you have tweaked your search engine preferences, you can use your shortcuts to localize search. Open up a new tab and type one of your shortcuts followed by a space or tab. Notice that when you type the space/tab, the location bar adds a &#8220;Search&#8221; highlight area to show you what site you are actually searching. Go ahead and add your search query after this and hit enter.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/search.png"></p>

<p>Cool, right?</p>

<h3>Bonus Step - Adding Your Own Sites</h3>

<p>If the site you want to search is not listed, you can add it. Go to the bottom of the list of sites on the preferences page. In the last entry, fill in the three fields: &#8220;Search Engine&#8221;, &#8220;Keyword&#8221;, and the search address.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/new_engine.png"></p>

<p>To determine the search address, you&#8217;ll need to go to that site and perform a search. Highlight the portion of the location bar which contains the URL and the query string, and paste that in to the third column of the new entry. Now, just replace the search term you used on the site with a &#8220;%s&#8221; and you&#8217;re finished.</p>

<p><img src="http://samuelmullen.com/images/chrome_powerup/query_replace.png"></p>

<p>For the image above, I would change &#8220;https://kippt.com/#/search/vim&#8221; to
&#8220;http://kippt.com/#search/%s&#8221; in the &#8220;search address&#8221; field.</p>

<p>I&#8217;m really just tickled to have found this feature in <a href="https://www.google.com/chrome">Google Chrome</a>. I&#8217;ve been using it a lot since finding it, and I&#8217;ve set up several shortcuts which I&#8217;ve been making heavy use of: <a href="http:/github.com">GitHub</a>, <a href="http://kippt.com">kippt</a>, <a href="http://catch.com">catch</a>, and <a href="http://rubydoc.info">rubydoc.info</a> to name a fiew. It reduces clicks, it simplifies my workflow, and it makes the web a nicer place to live.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Testing Geocoder with RSpec and Cucumber]]></title>
    <link href="http://samuelmullen.com/2012/02/testing-geocoder-with-rspec-and-cucumber/"/>
    <updated>2012-02-03T21:40:00-06:00</updated>
    <id>http://samuelmullen.com/2012/02/testing-geocoder-with-rspec-and-cucumber</id>
    <content type="html"><![CDATA[<p>If you are using the <a href="https://github.com/alexreisner/geocoder">Geocoder gem</a> then your tests related to geocoding are most likely hitting one of the gem&#8217;s geolocation providers. That&#8217;s probably not what you want: 1) your tests run slower; 2) you use up your request quota for your specified provider; 3) and most importantly, you are not able to control the data returned. A good solution to this is to use the <a href="https://github.com/chrisk/fakeweb">fakeweb gem</a> - and it&#8217;s really easy to set it up.</p>

<h3>The Steps</h3>

<p>First, add <code>fakeweb</code> to your <code>Gemfile</code> under the <code>:test</code> group and then &#8220;bundle install&#8221;.</p>

<figure class='code'><figcaption><span>Gemfile</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="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>  <span class="n">gem</span> <span class="s1">&#39;fakeweb&#39;</span>
</span><span class='line'>  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, put the following code in a file under <code>spec/support</code> (or <code>test/support</code>). I use <code>geocoding.rb</code>, but you can name it anything you wish.</p>

<figure class='code'><figcaption><span>spec/support/geocoding.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>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">google_json</span> <span class="o">=</span> <span class="o">&lt;&lt;-</span><span class="no">JSON</span>
</span><span class='line'><span class="sh">{</span>
</span><span class='line'><span class="sh">  &quot;status&quot;: &quot;OK&quot;,</span>
</span><span class='line'><span class="sh">  &quot;results&quot;: [ {</span>
</span><span class='line'><span class="sh">    &quot;types&quot;: [ &quot;street_address&quot; ],</span>
</span><span class='line'><span class="sh">    &quot;formatted_address&quot;: &quot;45 Main Street, Long Road, Neverland, England&quot;,</span>
</span><span class='line'><span class="sh">    &quot;address_components&quot;: [ {</span>
</span><span class='line'><span class="sh">      &quot;long_name&quot;: &quot;45 Main Street, Long Road&quot;,</span>
</span><span class='line'><span class="sh">      &quot;short_name&quot;: &quot;45 Main Street, Long Road&quot;,</span>
</span><span class='line'><span class="sh">      &quot;types&quot;: [ &quot;route&quot; ]</span>
</span><span class='line'><span class="sh">    }, {</span>
</span><span class='line'><span class="sh">      &quot;long_name&quot;: &quot;Neverland&quot;,</span>
</span><span class='line'><span class="sh">      &quot;short_name&quot;: &quot;Neverland&quot;,</span>
</span><span class='line'><span class="sh">      &quot;types&quot;: [ &quot;city&quot;, &quot;political&quot; ]</span>
</span><span class='line'><span class="sh">    }, {</span>
</span><span class='line'><span class="sh">      &quot;long_name&quot;: &quot;England&quot;,</span>
</span><span class='line'><span class="sh">      &quot;short_name&quot;: &quot;UK&quot;,</span>
</span><span class='line'><span class="sh">      &quot;types&quot;: [ &quot;country&quot;, &quot;political&quot; ]</span>
</span><span class='line'><span class="sh">    } ],</span>
</span><span class='line'><span class="sh">    &quot;geometry&quot;: {</span>
</span><span class='line'><span class="sh">      &quot;location&quot;: {</span>
</span><span class='line'><span class="sh">        &quot;lat&quot;: 0.0,</span>
</span><span class='line'><span class="sh">        &quot;lng&quot;: 0.0</span>
</span><span class='line'><span class="sh">      }</span>
</span><span class='line'><span class="sh">    }</span>
</span><span class='line'><span class="sh">  } ]</span>
</span><span class='line'><span class="sh">}</span>
</span><span class='line'><span class="no">JSON</span>
</span><span class='line'>
</span><span class='line'><span class="no">FakeWeb</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="ss">:any</span><span class="p">,</span> <span class="sr">%r|http://maps\.googleapis\.com/maps/api/geocode|</span><span class="p">,</span> <span class="ss">:body</span> <span class="o">=&gt;</span> <span class="n">google_json</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>To reference the above file in Cucumber, add the following line to your <code>features/support/env.rb</code> file:</p>

<figure class='code'><figcaption><span>features/support/env.rb</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="nb">require</span> <span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;spec/support/geocoding&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<h3>The Explanation</h3>

<p>Even though there is a lot of text in the <code>geocoding.rb</code> file, there really isn&#8217;t much going on. In the &#8220;here document&#8221; (the parts between <code>google_json = &lt;&lt;-JSON</code> and <code>JSON</code>) a big blob of JSON data gets assigned to the <code>google_json</code> variable. That&#8217;s it.</p>

<p>On the last line, we tell the FakeWeb gem that any call to Google&#8217;s API needs to be captured and then return a blob of JSON data (what&#8217;s stored in the <code>google_json</code> variable) &#8220;successfully&#8221;. Note that the URL used above is a regular expression and is only a portion of the full address in order to capture everything sent to that location.</p>

<p>It is entirely likely that you are using an alternate provider to Google. If this is the case, you will need to retrieve the appropriate JSON data yourself. You can find this data in the <a href="https://github.com/alexreisner/geocoder/tree/master/test/fixtures">Geocoder test fixtures</a>. Look for the files beginning with your provider and ending with &#8220;madison_square_garden.json&#8221;.</p>

<p>Alternate URLs for providers can be found in the <a href="https://github.com/alexreisner/geocoder/tree/master/lib/geocoder/lookups">Geocoder &#8220;lookups&#8221; files</a>. The URLs reside in the <code>query_url</code> method.</p>

<p>If you&#8217;ve done everything correctly, your test should now stay local to your machine and return only the data you&#8217;ve specified. From here, you can set up tests  for failures, alternate geolocation providers, specific latitude/longitude coordinates, and more. You now have all that is necessary to test geolocation calls.</p>
]]></content>
  </entry>
  
</feed>
