<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ScottRu &#187; Programming</title>
	<atom:link href="http://scottru.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottru.com</link>
	<description></description>
	<lastBuildDate>Thu, 19 May 2011 03:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Lazy Testing your URLs</title>
		<link>http://scottru.com/2010/07/23/lazy-testing-your-urls/</link>
		<comments>http://scottru.com/2010/07/23/lazy-testing-your-urls/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 19:49:13 +0000</pubDate>
		<dc:creator>scottru</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://scottru.com/?p=80</guid>
		<description><![CDATA[So I have a Rails app that&#8217;s broad but not very deep: there are a lot of different types of URLs and thousands of pages, generated dynamically or built through a CMS. No one page is that complicated, but there&#8217;s a fair amount of shared code and thus some tentacles throughout. Yes, I should have [...]]]></description>
			<content:encoded><![CDATA[<p>So I have a Rails app that&#8217;s broad but not very deep: there are a lot of different types of URLs and thousands of pages, generated dynamically or built through a CMS. No one page is that complicated, but there&#8217;s a fair amount of shared code and thus some tentacles throughout. Yes, I should have written behavior-level and unit-level tests from the beginning, but I didn&#8217;t, don&#8217;t judge me.</p>
<p>Anyway, after I broke the site yet again, I realized what I really wanted was a post-release test suite &#8211; I wanted to test a whole bunch of pages in production and make sure they&#8217;re still rendering.</p>
<p>Stealing from the <a href="http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html">Net::HTTP canonical examples</a>, then, I wrote a standalone script:</p>
<div class="code">
require &#8216;net/http&#8217;</p>
<p>URIs = ['http://example.com/', 'http://example.com/page/2']</p>
<p>puts &#8220;Running URL Test Suite&#8230;&#8221;<br />
URIs.each do |uri|<br />
&nbsp;&nbsp;url = URI.parse(uri)<br />
&nbsp;&nbsp;req = Net::HTTP::Get.new(url.path)<br />
&nbsp;&nbsp;res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }<br />
&nbsp;&nbsp;unless res.code == &#8217;200&#8242;<br />
&nbsp;&nbsp;&nbsp;&nbsp;puts &#8220;ERROR: #{uri} #{res.code}&#8221;<br />
&nbsp;&nbsp;end<br />
end<br />
puts &#8220;Test Suite Complete!&#8221;
</p></div>
<p>Then, since I <a href="http://philsturgeon.co.uk/news/2010/02/Deploying-websites-with-Git">deploy directly with git</a>, I added a call to this script in my post-receive hook. As long as I watch the deployment (which I do anyway), I&#8217;ll see if there&#8217;s a problem and fix it immediately. (I can handle a few minutes of downtime or just roll back.)</p>
<p>There are obvious improvements to this (like not storing the URLs in the file), and this doesn&#8217;t handle redirect, workflow, or authentication scenarios, but it&#8217;s a perfectly acceptable 99% case for a content site.</p>
<p>Note that if you are doing Rails development and you&#8217;ve built a custom 404 page, you&#8217;ll want to make sure that you&#8217;re actually returning a 404 status code, or this won&#8217;t catch errors: you can <a href="http://web-sniffer.net/">test your status codes</a>, and if you aren&#8217;t, you can <a href="http://stackoverflow.com/questions/943138/how-does-one-implement-dynamic-404-500-etc-error-pages-in-rails/943762#943762">add code in your application controller</a> to do the right thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottru.com/2010/07/23/lazy-testing-your-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming: write it down (again)</title>
		<link>http://scottru.com/2008/09/30/programming-write-it-down-again/</link>
		<comments>http://scottru.com/2008/09/30/programming-write-it-down-again/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 04:36:10 +0000</pubDate>
		<dc:creator>scottru</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[protip]]></category>

		<guid isPermaLink="false">http://scottru.com/?p=21</guid>
		<description><![CDATA[Last day of last month, I talked about the importance of writing down your questions when you&#8217;re programming. Last day of this month, similar post &#8211; now, it&#8217;s about writing down your algorithms when you&#8217;re programming. Since college (when I would &#8220;program&#8221; by the light of a desk lamp at the top of a bunk [...]]]></description>
			<content:encoded><![CDATA[<p>Last day of last month, I talked about the importance of <a href="http://scottru.com/2008/08/31/programming-when-youre-stuck-write-it-down/">writing down your questions when you&#8217;re programming</a>. Last day of this month, similar post &#8211; now, it&#8217;s about writing down your <strong>algorithms </strong>when you&#8217;re programming.</p>
<p>Since college (when I would &#8220;program&#8221; by the light of a desk lamp at the top of a bunk bed &#8211; pre-laptop), whenever I have a reasonably complex piece of logic to work through, I always start with a piece of paper and pencil, and I sketch out the entire algorithm in pseudo-pseudocode &#8211; basic structures, algorithms, control flow. (It&#8217;s like being at the board for an interview, but nobody&#8217;s watching.)</p>
<p>Why do this? In order of importance:</p>
<ul>
<li><strong>You can focus on the problem. </strong>When you&#8217;re writing real code, you&#8217;re focusing on a lot of things &#8211; the logic, but also the syntax and markup, the object-method structures, the tabs, the comments, the format. Each of those things takes some thought, and each moment causes a mental task switch from solving the real problem. (Maybe you&#8217;re both so talented and so experienced in a particular language and IDE that none of those things actually require any thought &#8211; you&#8217;re as fluent as you are in your native tongue. But I&#8217;m not, and you&#8217;re probably not either.)</li>
<li><strong>It&#8217;s much faster. </strong>You might type faster than you write when taking notes, but nothing&#8217;s faster than writing fake code on a piece of paper, drawing arrows and braces to mark sections, using carets to insert content, etc. You aren&#8217;t worrying about any of the things above: you will find the problems in your thinking much, much more quickly.</li>
<li><strong>You can do it anywhere. </strong>On the bus, by the bed lamp, when you&#8217;re pretending to make art with your three-year-old. You can think about the algorithm in between things and record your notes, rather than waiting for the moment you&#8217;re sitting down. Paper &amp; pen are still more accessible and portable than the MacBook Pro.</li>
</ul>
<p>Highly recommended approach. Let me know if this or similar things work for you!</p>
]]></content:encoded>
			<wfw:commentRss>http://scottru.com/2008/09/30/programming-write-it-down-again/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Vaporbase micro-CMS: Performance Improvement</title>
		<link>http://scottru.com/2008/09/26/vaporbase-micro-cms-performance-improvement/</link>
		<comments>http://scottru.com/2008/09/26/vaporbase-micro-cms-performance-improvement/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 15:02:05 +0000</pubDate>
		<dc:creator>scottru</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://scottru.com/?p=20</guid>
		<description><![CDATA[(Warning: code talk ahead.) When I started building the app I&#8217;m working on, I needed a quick CMS system that didn&#8217;t impose its own parsing language, and I was just learning RoR, so I implemented the Vaporbase micro-CMS based on this great tutorial. While you wouldn&#8217;t confuse this with a pro-grade CMS system, it works [...]]]></description>
			<content:encoded><![CDATA[<p>(Warning: code talk ahead.)</p>
<p>When I started building the app I&#8217;m working on, I needed a quick CMS system that didn&#8217;t impose its own parsing language, and I was just learning RoR, so I implemented the Vaporbase micro-CMS based on <a href="http://www.vaporbase.com/postings/A_Micro-CMS_in_Rails">this great tutorial</a>. While you wouldn&#8217;t confuse this with a pro-grade CMS system, it works just fine for the most part, and doesn&#8217;t require you to learn a number of new things.</p>
<p>Since implementing it, I&#8217;d made a few minor cleanups, but nothing significant, until I had 60 pages in the CMS, and saw a problem: the list of pages was taking a very long time to load (21sec on avg on my dev machine).</p>
<p>Sure enough, there&#8217;s a problem: in the <strong>Edit and Show Tree Hierarchy </strong>section, here&#8217;s a line that says</p>
<div class="code">
<% unless index_item.children.nil? %>
</div>
<p>Looks innocuous, right? Not exactly: for every single page in the tree, this does a separate SQL query (which is a full table scan, though likely on a tiny table) to find out if that item has any children. So creating the page is O(# pages in table), and while the queries itself aren&#8217;t that expensive, building the page just takes a while. If your tree is mostly flat &#8211; almost certainly the case for this kind of CMS &#8211; you&#8217;re wasting a lot of time.</p>
<p>Fortunately, there&#8217;s an easy solution: when you get the list of pages before creating the tree, figure out which pages have children, and then store that list to the side. It adds a bit of code, but not a lot.</p>
<div class="code">
# Original: from the index action in the controller<br />
# This gets just the root nodes and then recurs down the tree<br />
@pages = Page.find( :all, :conditions => ['parent_id IS NULL'], <img src='http://scottru.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => :position)
</div>
<div class="code">
# Replacement:<br />
# build two arrays &#8211;<br />
# @pages consists of all of the root nodes (since the view walks down the tree)<br />
# @pageIDs_with_children consists of all pageIDs which are the parent for >=1 page </p>
<p>@pages = []<br />
@pageIDs_with_children = []</p>
<p>@all_pages = Page.find(:all)<br />
@all_pages.each do |page|<br />
if (page.parent_id.nil?)<br />
   @pages << page<br />
else<br />
   @pageIDs_with_children << page.parent_id<br />
end
</div>
<p>(Note that the list might have duplicates, but that doesn&#8217;t matter. If that makes you unhappy, check for existence in the else clause.)<br />
Then replace the view line above with</p>
<div class="code">
<% if @pageIDs_with_children.index(index_item.id) %>
</div>
<p>I saw a >80X performance improvement on this page with this change in development (from >20sec to ~0.25sec with 60 pages), and you&#8217;ve just replaced an expensive linear scaling step with a very inexpensive one.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottru.com/2008/09/26/vaporbase-micro-cms-performance-improvement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search Evernote, not Google</title>
		<link>http://scottru.com/2008/09/22/search-evernote-not-google/</link>
		<comments>http://scottru.com/2008/09/22/search-evernote-not-google/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 21:15:33 +0000</pubDate>
		<dc:creator>scottru</dc:creator>
				<category><![CDATA[Evernote]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://scottru.com/?p=19</guid>
		<description><![CDATA[I&#8217;ve played with 100 note-taking/history-recording tools over the years, and none of them have ever really taken off for me. I started playing with Evernote soon after the beta began, and while I loved the interface and the story, my uses were very occasional. Now, however, I&#8217;m constantly in Evernote &#8211; it&#8217;s third only to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve played with 100 note-taking/history-recording tools over the years, and none of them have ever really taken off for me. I started playing with <a href="http://www.evernote.com/">Evernote</a> soon after the beta began, and while I loved the interface and the story, my uses were very occasional.</p>
<p>Now, however, I&#8217;m constantly in Evernote &#8211; it&#8217;s third only to my browser and Textmate &#8211; as I&#8217;m coding. </p>
<p>Why? I&#8217;m using it as a <strong>programming information repository</strong>. Every day that I program in Ruby on Rails, I learn a few new things or change the way I do things &#8211; today, for example, I learned that i<a href="http://blog.cbciweb.com/articles/2008/06/10/ruby-performance-use-double-quotes-vs-single-quotes">nterpolation is faster than concatenation</a>, and yesterday I finally found a <a href="http://www.tutorialspoint.com/ruby/ruby_blocks.htm">clear explanation on how yields work</a>. What I do now is every time I find an interesting piece of information, I clip the whole page to Evernote, add a few tags, and then keep on going. Occasionally I&#8217;ll paste screenshots from the PDFs of some books I own as well, which then (usually) get magically transcribed for me.</p>
<p>I&#8217;ve realized that I was using Google to search for the same information over and over again, and then trying to remember which link I liked the best. With Evernote, I&#8217;ve already recorded that memory, so I just have one source. Every day a greater percentage of my programming-related searching is happening in Evernote.</p>
<p>I&#8217;ve thought about making a separate notebook for my Rails/Ruby content and making it public &#8211; if I do so, I&#8217;ll post it here &#8211; though obviously it&#8217;s not a replacement for an actual text, it just has pointers to things I&#8217;ve cared about. However, if a dozen people did this, I&#8217;d love to have links to all of their notebooks.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottru.com/2008/09/22/search-evernote-not-google/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming: When you&#8217;re stuck, write it down</title>
		<link>http://scottru.com/2008/08/31/programming-when-youre-stuck-write-it-down/</link>
		<comments>http://scottru.com/2008/08/31/programming-when-youre-stuck-write-it-down/#comments</comments>
		<pubDate>Sun, 31 Aug 2008 16:00:30 +0000</pubDate>
		<dc:creator>scottru</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[protip]]></category>

		<guid isPermaLink="false">http://scottru.com/?p=17</guid>
		<description><![CDATA[Hey. Long time, no blog. Been busy. Thought I&#8217;d share a tip that I&#8217;ve used for many years when programming (or learning almost any new thing) and I&#8217;ve been using recently: when you&#8217;re stuck, write it down. Say you&#8217;re trying to figure out how to do something in [pick a framework], and you&#8217;ve Googled the [...]]]></description>
			<content:encoded><![CDATA[<p>Hey. Long time, no blog. <a href="http://blog.seattlepi.nwsource.com/venture/archives/144807.asp">Been busy</a>.</p>
<p>Thought I&#8217;d share a tip that I&#8217;ve used for many years when programming (or learning almost any new thing) and I&#8217;ve been using recently: <strong>when you&#8217;re stuck, write it down.</strong></p>
<p><strong></strong>Say you&#8217;re trying to figure out how to do something in [pick a framework], and you&#8217;ve Googled the heck out of the most-likely search terms, and nothing&#8217;s coming up.</p>
<p>Then write down your question as if you were going to ask a teacher/email it to a friend/post to a Google group/etc. Write down all the details: explain the thing you&#8217;re trying to do, the problem you have, and the number of things you&#8217;ve tried. Be as clear as you can, but don&#8217;t worry about being concise.</p>
<p>Literally every single time I&#8217;ve ever done this &#8211; and my rule-of-thumb is to do it after ~1.5 days worth of trying to figure it out myself &#8211; I find a number of new avenues to try, and almost always solve the problem on my own.</p>
<p>Writing it down forces you to take the jumbled thoughts in your head (they probably weren&#8217;t jumbled when you started, but you&#8217;ve changed paths so many times now) and turn them into a narrative. The process makes visible paths that your random walk opened up but that you didn&#8217;t see.</p>
<p>(This has been a procrastination for completing a writeup of my own.)</p>
]]></content:encoded>
			<wfw:commentRss>http://scottru.com/2008/08/31/programming-when-youre-stuck-write-it-down/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

