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

  <title><![CDATA[McPherson Industries]]></title>
  <link href="http://mcphersonindustries.com/atom.xml" rel="self"/>
  <link href="http://mcphersonindustries.com/"/>
  <updated>2011-10-29T18:40:13-06:00</updated>
  <id>http://mcphersonindustries.com/</id>
  <author>
    <name><![CDATA[Alex McPherson]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[.vimrc Sharing]]></title>
    <link href="http://mcphersonindustries.com/dot-vimrc-sharing"/>
    <updated>2011-10-11T19:26:00-06:00</updated>
    <id>http://mcphersonindustries.com/dot-vimrc-sharing</id>
    <content type="html"><![CDATA[<p>My <a href="https://github.com/alexmcpherson/vimrc">.vimrc</a> is posted on Github. A lot of people post theirs for others, but mine is just there for me. New jobs, new computers, friend&#8217;s house&#8230; you never know when you might need to vim a little.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello World]]></title>
    <link href="http://mcphersonindustries.com/hello-world"/>
    <updated>2011-10-10T22:52:00-06:00</updated>
    <id>http://mcphersonindustries.com/hello-world</id>
    <content type="html"><![CDATA[<p>Yeah, who doesn&#8217;t do a Hello World to test their new system out&#8230; Guilty as charged.</p>

<!--more-->


<p>Nerd like futzing, and real nerds hate Wordpress. That&#8217;s really the only reason my site is now hosted with <a href="http://octopress.org/">Octopress</a>. It&#8217;s a slick setup using a static site generator from the Ruby/Github community (<a href="http://jekyllrb.com/">Jekyll</a>), with some solid congifuration done for you.</p>

<p>It comes &#8216;blog aware&#8217;, which is more than many static site generators can say, and also boasts solid markup and a good theme to boot. Add to this that it requires the command line and white text on a black screen to run, and well, that put me over the edge.</p>

<p>The main reasoning behind generating your static files ahead of time is that you&#8217;re basically memoizing the work that something like Wordpress needs to do every pageload: parse the URL segments, route the request, query the data base (and join up to 40+ other queries in Wordpress&#8217; case), template that into HTML&#8230; all that work for something that doesn&#8217;t change much.</p>

<p>What (could) change on this site are the comments and my Twitter feed, and both of those are loaded on the delay with javascript. This means that in a 10 pageload test my old WP blog averaged about a second and a half to fire the .ready() event, where the new one is ~120ms. My page doesn&#8217;t -need- to load fast, but now it does, which makes any nerd happy.</p>

<p>It also supports some great code insertion with the Solarized color scheme by default:</p>

<figure class='code'><figcaption><span>A test of syntaxt highlighting</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='javascript'><span class='line'>   <span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){}</span>
</span><span class='line'>  <span class="nx">foo</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">init</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">//init code here please</span>
</span><span class='line'>      <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;initializing a foo object&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pretty dang slick.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Moustache Fu]]></title>
    <link href="http://mcphersonindustries.com/moustache-fu"/>
    <updated>2011-06-19T16:40:00-06:00</updated>
    <id>http://mcphersonindustries.com/moustache-fu</id>
    <content type="html"><![CDATA[<p>A brief Google shows a high probability that I coined this phrase: “Moustache Fu”. Though it might conjure up notions of a hipster learning martial arts, it’s intended to describe a certain circle of coding hell, as in “I’ve just encountered code written by a 9th degree blackbelt in Moustache Fu”.</p>

<!-- more -->


<p>Basically it means that you’ve over-used and abused a templating system like ASP or PHP, and have to break in and out of ‘code’ tags constantly, leaving you with lines like this:</p>

<figure class='code'><figcaption><span>Closing Fu</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='erb'><span class='line'><span class="cp">&lt;%</span> <span class="p">}</span> <span class="cp">%&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>or</p>

<figure class='code'><figcaption><span>Opening Fu</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='erb'><span class='line'><span class="cp">&lt;%</span> <span class="p">{</span> <span class="cp">%&gt;</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>That code smells, and means that the rest of the file that poor sap wrote is probably garbage as well. Hiyaaaa!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Performance in Javascript: Caching]]></title>
    <link href="http://mcphersonindustries.com/performance-in-javascript-caching"/>
    <updated>2010-10-27T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/performance-in-javascript-caching</id>
    <content type="html"><![CDATA[<p>Typically caching is something done on a web server to reduce expensive calls to databases or filesystems. It will keep commonly used resources in memory, allowing them to be accessed quickly. You can apply similar ideas to Javsascript.</p>

<!-- more -->


<p>When used in programs, this idea is called <strong><em>memoization</em></strong>. Essentially what you are doing is trading computational complexity for memory; that is, if something is hard to compute, you only do it once and save the result in memory. This is not always a good option, however, because memory is always finite. Here are two examples of when to use it:</p>

<p><strong><em>jQuery Selectors</em></strong> &#8211; jQuery offers amazingly powerful selectors that let you pull precisely the node you want out of the DOM using CSS-like notation. For example</p>

<figure class='code'><figcaption><span>A complex selector</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='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#wrapper ul#nav &gt; li.first&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>lets you select all list-items with a class of &#8216;first&#8217;, whom are children of a ul with an id &#8216;nav&#8217;, which is itself contained inside a div of id &#8216;wrapper&#8217;. Phew!</p>

<p>This is super handy, but few realize the complexity that jQuery is wrapping up for them: with only a few shortcuts from native DOM methods (getElementById, notably) it is basically traversing the entire DOM and applying various rules to each node it encounters. On a page of any size, this computational effort adds up quickly and show up as a laggy UI.</p>

<p>A way to cache this function is to write code that avoids making that jQuery selection every time the collection of nodes is required:</p>

<figure class='code'><figcaption><span>Value memoization</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='javascript'><span class='line'><span class="c1">//Bad!</span>
</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">$</span><span class="p">(</span><span class="s1">&#39;#myPage a&#39;</span><span class="p">).</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">//do something with each anchor element</span>
</span><span class='line'>    <span class="c1">//this traverses the DOM each loop</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//Good!</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">elements</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#myPage a&#39;</span><span class="p">);</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">elements</span><span class="p">.</span><span class="nx">length</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">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="nx">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">//do something with each anchor</span>
</span><span class='line'>    <span class="c1">//this only compares two integers each loop that you&#39;ve saved off</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong><em>AJAX call caching</em></strong> &#8211; AJAX is sweet. Using a an XHR object to call a server for data is now the bread and butter of the web. Each call though has a transaction cost associated with it: XHR overhead, network latency, remote server CPU time&#8230; Yuck. Let&#8217;s see how to cache a buffer call:</p>

<figure class='code'><figcaption><span>Ajax Memoization</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">//a function that makes an AJAX call to a remote source</span>
</span><span class='line'><span class="c1">//NOTE: This isn&#39;t practical for rapidly changing data</span>
</span><span class='line'>
</span><span class='line'><span class="nx">myObject</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">callTwitterAPI</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span> <span class="c1">//to preserve context</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">Data</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;twitter.com/RESTfulAPI.html?query...&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">Data</span> <span class="o">=</span> <span class="nx">data</span><span class="p">;</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">callTwitterAPI</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">Data</span><span class="p">;</span>  <span class="c1">//this is the key. This overwrites the current function with one skipping the AJAX call</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="nx">Data</span><span class="p">;</span> <span class="c1">//Finally return the data</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>So this is a function that when first called with make an AJAX request, but then it redefines itself to simply return the already retrieved data for future calls. Powerful stuff that can start to make your client side code fly.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Some Javascript Idioms]]></title>
    <link href="http://mcphersonindustries.com/some-javascript-idioms"/>
    <updated>2010-09-03T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/some-javascript-idioms</id>
    <content type="html"><![CDATA[<p>It&#8217;s common to find yourself assigning a value to a variable that might or might not be <code>null</code>. A first-pass approach to coalescing a value into that variable might look like this:</p>

<figure class='code'><figcaption><span>Ensuring a value is set</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="c1">//userInput may or may not be valid</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">Foo</span><span class="p">;</span>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="nx">userInput</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">Foo</span> <span class="o">=</span> <span class="nx">userInput</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">Foo</span> <span class="o">=</span> <span class="nx">defaultValue</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>A more concise way would be to use the ternary operator:</p>

<figure class='code'><figcaption><span>Ternary to set a value</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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Foo</span> <span class="o">=</span> <span class="p">(</span><span class="nx">userInput</span><span class="p">)</span> <span class="o">?</span> <span class="nx">userInput</span> <span class="o">:</span> <span class="nx">defaultValue</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is much better. It&#8217;s still readable, but is also more expressive. The third way (that I prefer) is to use a Boolean operator in assignment, like so:</p>

<figure class='code'><figcaption><span>OR for a default value</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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Foo</span> <span class="o">=</span> <span class="nx">userInput</span> <span class="o">||</span> <span class="nx">defaultValue</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works exactly like the ternary: if <code>userInput</code> is &#8216;falsey&#8217;, the evaluation of the Boolean continues, and Foo is assigned the value of <code>defaultValue</code>. If <code>userInput</code> is &#8220;truthy&#8221;, however, evaluation of the OR statement stops, because one side of the Boolean has evaluated to true.</p>

<p>A similar trick can be done using the AND operator:</p>

<figure class='code'><figcaption><span>Protecting a reference call on an object with AND</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Bar</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">Baz</span> <span class="o">=</span> <span class="s1">&#39;string&#39;</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">Foo</span> <span class="o">=</span> <span class="nx">Bar</span> <span class="o">&amp;&amp;</span> <span class="nx">Baz</span><span class="p">;</span> <span class="c1">//results in false</span>
</span></code></pre></td></tr></table></div></figure>


<p>but</p>

<figure class='code'><figcaption><span>AND Behavior</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Bar</span> <span class="o">=</span> <span class="s1">&#39;prerequisite&#39;</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">Baz</span> <span class="o">=</span> <span class="s1">&#39;string&#39;</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">Foo</span> <span class="o">=</span> <span class="nx">Bar</span> <span class="o">&amp;&amp;</span> <span class="nx">Baz</span><span class="p">;</span> <span class="c1">//results in Foo == &#39;string&#39;;</span>
</span></code></pre></td></tr></table></div></figure>


<p>These two way of conditionally assigning value to a variable can be used to make javascript quite expressive while maintaining readability. Go forth and code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A Website in the Wild]]></title>
    <link href="http://mcphersonindustries.com/a-website-in-the-wild"/>
    <updated>2010-08-28T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/a-website-in-the-wild</id>
    <content type="html"><![CDATA[<p>I wanted to do a quick post to mark the launching of my wife&#8217;s venture: <a href="http://www.therootsellers.com">The Root Sellers</a>, and their first event, Taste of Place.</p>

<p>It&#8217;s a great idea in the spirit of Joel Salatin, the renegade farmer from &#8220;The Omnivore&#8217;s Dilemma&#8221;. Food is generally over regulated by the government, often to no effect (see the recent egg recalls). People who are passionate about food deserve to meet each other and build a vibrant community. This is what the Root Sellers promises: old-timey methods and techniques of preparing and preserving food, available to learn and purchase at various events.</p>

<p>Taste of Place is a &#8220;pop up market&#8221;, limited to club members. On September 26th a warehouse in the Boulder, CO area will be hosting this market and allowing culinary traditions from the area to continue unfettered by needless oversight. Trust your farmers, trust your nose, and get the best food from the peak of the season.</p>

<p>Quick gloating: I had a blast making their website. I took the concept and developed the aesthetic and made it live in HTML/PHP/JavaScript. The mini-JavaScript functions are also a welcome change from the behemoths that frequently get instantiated at work. Fun!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Various Stages of Button Design]]></title>
    <link href="http://mcphersonindustries.com/various-stages-of-button-design"/>
    <updated>2010-07-14T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/various-stages-of-button-design</id>
    <content type="html"><![CDATA[<p>I&#8217;m not sure if this is a tour through web design history or of a budding designer&#8217;s development. Either way I took 15 minutes tonight to make some examples of buttons you might find around the internet:</p>

<p><img title="Various Stages of Button Design" src="../assets/buttons.png" /></p>

<ul>
<li>a) Web 2.0 &#8211; Our first experience with gradients and buttons that can look like physical objects. Best avoided today except out of irony.</li>
<li>b) Grunge/Worn &#8211; As soon as everyone began to see the glossy web 2.0 buttons as tired, the resulting movement was to go as far the other way as possible: destroy the website a bit around the edges. Like Abercrombie and Fitch.</li>
<li>c) Letter Press &#8211; Many designers come from a traditional arts background and have somewhat of a fetish for letterpress (and rightly so). This is an attempt to add subtle realism and a bit of an &#8220;ooh&#8221; factor. I&#8217;d say we&#8217;re in the midst of this here in 2010.</li>
<li>d) Startup Page &#8211; A startup&#8217;s success is often determined by early user acquisition and user retention, with a good product a distant third. In an effort to get visitors to convert to users, sites are A|B tested and examined by psychologists until you can&#8217;t <em>help</em> but give them your email address and contact information. This huge bright button can be seen on almost any site featured in Hacker News.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Apple Keeps a White List of OS X Apps to Keep Happy]]></title>
    <link href="http://mcphersonindustries.com/apple-keeps-a-white-list-of-os-x-apps-to-keep-happy"/>
    <updated>2010-06-18T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/apple-keeps-a-white-list-of-os-x-apps-to-keep-happy</id>
    <content type="html"><![CDATA[<p><a href="http://www.rogueamoeba.com/">Rogue Amoeba</a> is one of the better software developers for the Mac platform. They make excellent, though admittedly niche, audio software. Their co-founder Paul Kafasis recently gave an <a href="http://www.macobserver.com/tmo/article/tmo_interviews_rogue_amoeba_its_all_about_the_audio/">interview</a> with Mac Observer. It&#8217;s always interesting to hear other programmers hold forth, but he hinted at something I had never heard of before:</p>

<blockquote><p>I believe that a couple of our apps are on an Apple internal white list  so that they say: &#8220;…if our OS update breaks any of these 100 apps, then  we need to figure out what’s up and maybe contact the developer. Or  maybe figure out if we’re doing something wrong.&#8221;</p><footer><strong>Paul Kafasis</strong><cite><a href='http://www.macobserver.com/tmo/article/tmo_interviews_rogue_amoeba_its_all_about_the_audio/'>Mac Observer Interview</a></cite></footer></blockquote>


<p>This is interesting. A more sensationalist write would attack the &#8220;Secret Policy&#8221; angle and demand equality for all purveyors on the platform. I&#8217;m more interested in how responsive this could (read: probably does) make them. By recognizing quality, know-how, and investment in the platform the OS X team at Apple can in a subtle, non-communicated way, grow <strong><em>with</em></strong> their developers. This is a very different mindset from a typical platform manager&#8217;s take of &#8220;Let&#8217;s update, push far in our own direction, and I hope you can keep up&#8221;.</p>

<p>This isn&#8217;t to say that Rogue Amoeba (or Panic or MacroMates or MacRabbit) are secretly steering the direction of Cocoa and Objective-C. It&#8217;s an observation that by having a list of applications that Apple believes is important to the ecosystem that is OS X, they stand to gain more loyal developers and users by maintaining their typical high standards than they could by forging ahead towards the future of their platform without external influence.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to sort an object in Javascript]]></title>
    <link href="http://mcphersonindustries.com/how-to-sort-an-object-in-javascript"/>
    <updated>2010-05-13T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/how-to-sort-an-object-in-javascript</id>
    <content type="html"><![CDATA[<p>Javascript is nice. It has a tiny set of native functions, and allows you to do <strong><em>almost</em></strong> anything you&#8217;d like. A quick trick I learned today is how to sort an array of objects rather painlessly. Let&#8217;s say you have the following array full of objects:</p>

<figure class='code'><figcaption><span>Array Sample</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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">friendsArray</span> <span class="o">=</span> <span class="p">[</span>
</span><span class='line'>    <span class="p">{</span> <span class="nx">name</span> <span class="o">:</span> <span class="s1">&#39;Alex&#39;</span><span class="p">,</span> <span class="nx">age</span> <span class="o">:</span> <span class="mi">26</span> <span class="p">},</span>
</span><span class='line'>    <span class="p">{</span> <span class="nx">name</span> <span class="o">:</span> <span class="s1">&#39;Andrew&#39;</span><span class="p">,</span> <span class="nx">age</span> <span class="o">:</span> <span class="mi">23</span> <span class="p">},</span>
</span><span class='line'>    <span class="p">{</span> <span class="nx">name</span> <span class="o">:</span> <span class="s1">&#39;Alice&#39;</span><span class="p">,</span> <span class="nx">age</span> <span class="o">:</span> <span class="mi">31</span> <span class="p">},</span>
</span><span class='line'>    <span class="p">{</span> <span class="nx">name</span> <span class="o">:</span> <span class="s1">&#39;Amy&#39;</span><span class="p">,</span> <span class="nx">age</span> <span class="o">:</span> <span class="mi">15</span> <span class="p">}</span>
</span><span class='line'><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>You want to sort these people by their age. Javascript has a native <code>.sort</code> function for arrays, that either lexically or numerically sorts an array&#8230; but that won&#8217;t quite cut it for our use case. Luckily, this method <strong><em>also</em></strong> accepts a function as a parameter to help you sort. <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort">Documentation at Mozilla Developer Center</a>. Suitable code would look something more like this:</p>

<figure class='code'><figcaption><span>Custom Sort Function</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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">sortByAge</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">first</span><span class="p">,</span> <span class="nx">second</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">first</span><span class="p">.</span><span class="nx">age</span> <span class="o">&lt;</span> <span class="nx">second</span><span class="p">.</span><span class="nx">age</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">first</span><span class="p">.</span><span class="nx">age</span> <span class="o">&gt;</span> <span class="nx">second</span><span class="p">.</span><span class="nx">age</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">friendsArray</span><span class="p">.</span><span class="nx">sort</span><span class="p">(</span><span class="nx">sortByAge</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>So you can literally put in any method of comparing <code>first</code> and <code>second</code> that you want and set the return value to be <code>1</code> or <code>-1</code> and the function will sort it appropriately. You could ping two servers and sort by latency, or request a series of recent tweets from an array of Twitter username and sort by most recently posted&#8230; The list goes on, but it&#8217;s a pretty powerful function beyond face value.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Geolocated tweets on a map]]></title>
    <link href="http://mcphersonindustries.com/geolocated-tweets-on-a-map"/>
    <updated>2010-05-06T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/geolocated-tweets-on-a-map</id>
    <content type="html"><![CDATA[<p>I&#8217;ve always wondered what Twitter-user distribution looks like across the world. I assume it falls off at night in the US, but who knows? Can I be sure of myself in assuming that most users are in the Mission in San Francisco? Like any fiddler with a penchant for coding and some free time, I decided to figure out myself.</p>

<p><strong>Step 1:</strong> Get some tweets.</p>

<p>Twitter offers a <a href="http://apiwiki.twitter.com/Streaming-API-Documentation#Sampling">sampling API</a> that gives you random tweets from up to 3 years or so ago. I set up an instance of <a href="http://code.google.com/p/phirehose/">Phirehose</a> and let it run for about half an hour, saving the <code>geo</code> and <code>created_at</code> fields in a MySQL table. It swiftly dumped <code>~1.3 million tweets</code>, consuming 58 MB. Awesome!</p>

<p><strong>Step 2:</strong> Normalize the data.</p>

<p>So Tweets are a <a href="http://mehack.com/map-of-a-twitter-status-object">complex chunk of data</a> delivered in either JSON or XML. It&#8217;s fidelity to the official standard is at the mercy of the client used to create the tweet as well as the user. Needless to say, the data was ugly. Basic steps taken (along with tweets left after the filter):</p>

<ul>
<li>Remove empty geo fields (~200,000 left)</li>
<li>Regex for XX.XXXX, XX.XXXX (35,462 left)</li>
<li>Removed &#8220;true&#8221; lat/long with North/N/n values to leave just <a href="http://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system">UTM coordinates</a>(~29,000 left)</li>
<li>Remove identical UTM pairs (21,750 left)</li>
<li>Check for out-of-bounds values for date/time (~19,000 left)</li>
</ul>


<p>So now I was left with 19,000 rows in a DB similar to: <code>40.4410, -105.25529, "Feb 2 2009 4:41:44 EST"</code></p>

<p><strong>Step 3:</strong> Put it on to <code>&lt;canvas&gt;</code>, a drawing element supported by browsers that play nicely with HTML5 (newish Firefox, Safari, and Chrome builds).</p>

<p>The earth isn&#8217;t flat, so a grid is hard to project onto it. Luckily someone else has solved this problem by creating <a href="http://en.wikipedia.org/wiki/Equirectangular_projection">Equirectangular Projections</a>. This family of maps are equal-distance projections that maintains distances between spaces (with sacrificing with more distortion around the poles). I used the Creative Commons map available on Wikipedia.</p>

<p>I built the page up with this image placed behind the <code>&lt;canvas&gt;</code> element with CSS, and started in on the Javascript. For a quick project where page heaviness is fine, jQuery is a good choice. I made some basic functions and threw in a jQueryUI slider (all of which is visible in <a href="http://www.mcphersonindustries.com/assets/geotweets/js.js">the source</a>).</p>

<p><strong>Results: </strong>
So I wound up with a pretty cool way of showing who tweets where, and when. The different hemispheres do indeed encounter lower usage in their night-time, which matches my assumptions. Neat!</p>

<p><strong>Caveats: </strong> A few words of warning:</p>

<ul>
<li>There is a sampling bias in how I &#8220;cleaned&#8221; my data. No Kanji characters, for example, possibly explaining the lower-appearing usage in Japan.</li>
<li>I also opted against geocoding locations such as &#8220;Boulder, CO&#8221; due to computational overhead. Google does offer this API through Google Maps, but I deemed it outside the scope of the project.</li>
<li>This will not render on older browsers or <em>any</em> version of Internet Explorer. There is a project called <a href="http://excanvas.sourceforge.net/">Excanvas</a> to get <code>&lt;canvas&gt;</code> rolling on IE, but this was also outside of my scope.</li>
</ul>


<p><a href="http://www.mcphersonindustries.com/assets/geotweets/canvas.php">Check it out</a></p>

<p>Thanks for looking!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Bridge Scoring App in HTML5]]></title>
    <link href="http://mcphersonindustries.com/bridge-scoring-app-in-html5"/>
    <updated>2010-04-18T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/bridge-scoring-app-in-html5</id>
    <content type="html"><![CDATA[<p>My <a href="http://twitter.com/bopreneur">father-in-law</a> and I play a lot of bridge together and as the evening wends along we often forget exactly how to keep score. Enter in the <a href="http://twitter.com/johnallsopp/status/12188643943">current</a> <a href="http://farukat.es/journal/2010/04/424-screw-the-web">debate</a> regarding HTML5 versus native Obj-C iPhone applications, and I saw a weekend project. I present my first iPhone web-app: <strong>Bridge Score</strong>.</p>

<!--more-->


<p>It is entirely composed of Javascript, HTML, and CSS. It uses the <a href="http://www.w3.org/TR/html5/offline.html">HTML5 cache-manifest</a> to store all of the files locally so it can be used perfectly well offline (a boon to iPod Touch owners). It also uses a few proprietary Apple <code>&lt;meta&gt;</code> tags to hide the Mobile Safari chrome (search bar, back buttons etc) and show a custom application icon so it really feels and acts like a native iPhone app.</p>

<p>Bridge-wise, it is not a &#8220;complete&#8221; scoring app. It has hard-coded player names and only follows our house rules for rubber bridge scoring (which unbeknownst to be are a bit different from the <a href="http://en.wikipedia.org/wiki/Rubber_bridge">Wikipedia entry</a>). Overtricks are 20/30 for minor/major suits, whereas getting set awards 50/100 over-the-line points to the opposing team. Vulnerability is tracked and accounted for, and play can stretch beyond a classic rubber.</p>

<p>To use the app:</p>

<ul>
<li>Direct your iPhones, iPods Touch, or WebKit-enabled browser (Safari) to the starting page:
<a href="http://www.mcphersonindustries.com/assets/bridge/nodashcode.html">Bridge Score</a></li>
<li>On an Apple mobile device, press the &#8220;+&#8221; below the screen</li>
<li>Select &#8220;Add to Home Screen&#8221;</li>
<li>Give it your own title:</li>
<li>It is now at home on the Home Screen and can be accessed off-line with all capabilities, just like a native Obj-C iPhone application, but this is in HTML5, CSS, and Javascript. Awesome!</li>
</ul>


<p>This is a great option for a lot of developers who don&#8217;t want to pick up a whole new language and set of API calls (Objective-C and Cocoa-Touch) to get their ideas mobile. Developing in HTML5 has the added benefit of &#8220;develop once, deploy everywhere&#8221; (at least on A-Grade mobile devices, such as Android-based phones and iPhones). The source code is there for browsing. Let me know if you have any questions.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Job]]></title>
    <link href="http://mcphersonindustries.com/new-job"/>
    <updated>2010-04-14T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/new-job</id>
    <content type="html"><![CDATA[<p>Kids, listen up: <strong>do what you want to do</strong>. I couldn&#8217;t get enough of computers. They were just so cool! I came out of college, and got a job&#8230; (wait for it)</p>

<p>&#8230; working in the woods. But when I got back to the city, I played on my computer. I went to the library. I stayed interested. I read. I tinkered. I played. I convinced people to let me make their websites. I stayed up late. I launched failed projects. I sold my first webapp. And now, I am officially a full-time web developer for a leading financial services firm. This is awesome.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Digs]]></title>
    <link href="http://mcphersonindustries.com/new-digs"/>
    <updated>2010-03-22T00:00:00-06:00</updated>
    <id>http://mcphersonindustries.com/new-digs</id>
    <content type="html"><![CDATA[<p>My prior online repository (Small Batch Studios) was inadvertently in a name-space occupied by another (more awesome) web company, so now we&#8217;re here. Thanks for tracking us down!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PHP Exercise]]></title>
    <link href="http://mcphersonindustries.com/php-exercise"/>
    <updated>2009-11-18T00:00:00-07:00</updated>
    <id>http://mcphersonindustries.com/php-exercise</id>
    <content type="html"><![CDATA[<p>My wife and I are traveling over the next 2 months, and as an exercise I created a page to let our families and friends know our location (roughly).</p>

<p><strong><a href="http://www.mcphersonindustries.com/assets/where/wherearethemcphersons.php">The result is quite neat</a></strong>, and I&#8217;ll explain a few of the finer points.</p>

<p>The Javascript Flipping Clock is a direct borrow from a <a href="http://net.tutsplus.com/tutorials/html-css-techniques/learn-how-to-create-a-retro-animated-flip-down-clock/">NetTuts tutorial</a>. Javascript wizard I ain&#8217;t, but it looks pretty swell.</p>

<p>The current activities are stored in one array per activity for a total of 6. To determine which array to pull from, I did a fairly straight forward elseif tree:</p>

<figure class='code'><figcaption><span>Activity Chart</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='php'><span class='line'><span class="x">$array_to_use = &#39;&#39;; //always initialize</span>
</span><span class='line'>
</span><span class='line'><span class="x">if ($now &lt; 1258783201) {</span>
</span><span class='line'><span class="x">  $array_to_use = $boston;</span>
</span><span class='line'><span class="x">}</span>
</span><span class='line'><span class="x">elseif (1258783201 &lt; $now &amp;&amp; $now &lt; 1258956001 ) {</span>
</span><span class='line'><span class="x">  $array_to_use = $driving;</span>
</span><span class='line'><span class="x">}</span>
</span><span class='line'><span class="x">//many other elseif</span>
</span><span class='line'><span class="x">else {</span>
</span><span class='line'><span class="x">  $array_to_use = $default; //in case the page is viewed later</span>
</span><span class='line'><span class="x">}</span>
</span><span class='line'>
</span><span class='line'><span class="x">$quote = $array_to_use[rand(0,count($array_to_use)-1)]; //picks one from the array</span>
</span></code></pre></td></tr></table></div></figure>


<p>Some key points are highlighted, and these lead to robust code: Always initialize your variables and provide default values. There&#8217;s a potentially confusing part at the end to choose the actual phrase to display.</p>

<p>To access an array element, you provide it with an index. The first element in <code>$Array</code> is <code>$Array[0]</code>, for example. This uses several nested functions. First it counts the elements in <code>$array_to_use</code> and subtracts 1 (to make the first element = 0). Then it picks a random number from 0-(# of elements in array), and uses that as the index for the array. The result is that each time to access the page, it shows a pseudo-random phrase.</p>

<p>The timeline was all displayed using CSS and HTML. It&#8217;s hard to be semantic for something so abstract, but the code is there for your inspection. There was a fair amount of math to ensure the chart was actually to scale, and I believe I wound up at roughly 8px per day. The final bit was making the yellow indicator move (slowly) as time passed. The HTML is a fairly straightforward</p>

<p>$Diff is calculated by some subtraction and scaling of times, like so:</p>

<figure class='code'><figcaption><span>Diff calculation</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='php'><span class='line'><span class="x">$start = 1258092001; //midnight start date</span>
</span><span class='line'><span class="x">$now = time() + 3600; //plus an hour for EST</span>
</span><span class='line'><span class="x">$diff = round(($now-$start)/10800); //division puts it in the 3hrs/pixel range</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is a good demonstration of some basic PHP scripting. Let me know if you have any questions about it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Facebook Photoshop Template]]></title>
    <link href="http://mcphersonindustries.com/facebook-photoshop-template"/>
    <updated>2009-11-05T17:00:00-07:00</updated>
    <id>http://mcphersonindustries.com/facebook-photoshop-template</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been doing a lot of work on the Facebook platform these days and kept finding myself presenting comps with personal information mistakenly included!</p>

<p>I had taken screenshots of the various &#8220;chromes&#8221; that a designer has to create for a fan page, tabbed view, canvas view, friend pickers, dialogue boxes&#8230; The list of designs was huge and they all had <strong><em>my</em></strong> targeted ads in the rail and <strong><em>my</em></strong> name and message count sprayed about. Nonsense, I say!</p>

<p>So I made an anonymized .PSD that I&#8217;d like to share. Highlights include:</p>

<ul>
<li>Generic names for all application areas</li>
<li>Pleasant image placeholders that are the same size as those on Facebook</li>
<li>Text treatments that are as close as possible to what Facebook shows you (it appears that Safari and Photoshop don&#8217;t quite render Tahoma identically)</li>
</ul>


<p>This will be updated as I take on more projects that necessitate generic templates; let me know what you&#8217;d like to use most.</p>

<p><a href="http://www.mcphersonindustries.com/assets/facebook.zip">Download</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Twitter Lists Launched]]></title>
    <link href="http://mcphersonindustries.com/twitter-lists-launched"/>
    <updated>2009-10-30T16:55:00-06:00</updated>
    <id>http://mcphersonindustries.com/twitter-lists-launched</id>
    <content type="html"><![CDATA[<p>Twitter launched a new aggregation tool called &#8220;Lists&#8221; today. You follow a list, which has been curated (hopefully) by someone you know or respect, and you follow all of the people on that list.</p>

<!-- more -->


<p>Robert Scoble, for example, made <a href="http://twitter.com/Scobleizer/lists">this list</a> of important tech folk to listen to. Indexing information is a <a href="http://j.mp/3LvvwA">documented problem</a>, but I believe Lists force the user into a decision to use Twitter for information gathering and ground-listening, or to use it socially. Twitter doesn&#8217;t excel as a tool for both simultaneously. You can use it to replace your feed reader and listen to Scoble&#8217;s 38 new folks, or use it to make snarky comments into the listening void of your friends, but I don&#8217;t think it&#8217;s a both/and decision.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Inaugural Post]]></title>
    <link href="http://mcphersonindustries.com/inaugural-post"/>
    <updated>2009-10-27T16:54:00-06:00</updated>
    <id>http://mcphersonindustries.com/inaugural-post</id>
    <content type="html"><![CDATA[<p>As they say (with only a little irony) in the software world, shipping is a feature. The verb for web developers would be &#8220;launching&#8221;, but the sentiment is the same: Nip, tuck, tweak, and nudge only until you can get something out the door. In front of people. That&#8217;s the idea behind this early iteration of McPherson Industries&#8217; very own website.</p>

<!-- more -->


<p>You&#8217;ll see the different pages fill in as I roll through my templates, and the copy with undoubtedly change as the method of the studio is refined a bit, but that&#8217;s that. A website. Built. Out there. Waving at the googlebot as it whizzes by. Hello!</p>
]]></content>
  </entry>
  
</feed>

