<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Dynamic Constants and their Pitfalls</title>
    <link>http://blog.lathi.net/articles/2008/02/05/dynamic-constants-and-their-pitfalls</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Dynamic Constants and their Pitfalls</title>
      <description>&lt;p&gt;I&amp;#8217;ve just fixed a bug in production that took me more than eight hours to find.  When I show you the code, you&amp;#8217;ll wonder why it took me so long.  I have lots of excuses, but it&amp;#8217;s a fairly interesting bug to think about.  It shows some of the weaknesses in my usual modus operandi.  The code that looks something like this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Article
  CURRENT=&amp;quot;start_publish_on &amp;lt;= #{Date.today} AND stop_publish_on &amp;gt; #{Date.today}&amp;quot;

  def self.find_latest
    find(:all, :conditions =&amp;gt; CURRENT)
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In retrospect the bug is obvious and probably is obvious to you as well.  The Article::CURRENT constant is dynamically generated using the date at the time the class is evaluated.  With rails in production mode, that could be a long time; certainly more than a day.  The conclusion to draw here is to be very, very careful about dynamically generating constant strings.  As a rule, I might suggest not doing it.&lt;/p&gt;


	&lt;p&gt;The most interesting thing about this is you can&amp;#8217;t write a test to catch this error.  I think that&amp;#8217;s the the biggest thing that took me so long to find the error.  I tend to be over confident in our test suite.  As the new guy to the project, I&amp;#8217;m proud of them for how conscientious they are about testing their code.  I&amp;#8217;m trying to fix this bug by triggering it in a test.  Well, it can&amp;#8217;t be done.  The difference is how the production environment cache classes versus how testing and development does it.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s another tidbit that threw me off the trail for a long time.  Our copy editor that is responsible for publishing articles says to fix it she simply back dates the articles by a day.  So I spent a lot of time looking for off-by-one errors.  I had recently fixed a problem with comparing times to dates and causing off-by-one, so I thought that might be it.  As it turns out, this was a red herring.  There&amp;#8217;s such a tight loop for feature request, implement, deploy that the production environment gets restarted fairly regularly (like nearly every day).&lt;/p&gt;


	&lt;p&gt;I guess what prompted me to write about this particular bug was what it said about our testing.  Clearly automated testing can&amp;#8217;t find all the bugs.  It also says something about our rapid development.  As long as we&amp;#8217;re really busy, this bug didn&amp;#8217;t bite us.  It&amp;#8217;s not until our deployment slows down (like a weekend) that it showed up.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Feb 2008 15:35:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:d67c52df-4bbb-44f6-b6ec-f0bb1ca407b2</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2008/02/05/dynamic-constants-and-their-pitfalls</link>
      <category>Programming</category>
      <category>Ruby on Rails</category>
    </item>
    <item>
      <title>"Dynamic Constants and their Pitfalls" by Ben</title>
      <description>&lt;p&gt;I &lt;a href="http://www.culann.com/2008/01/a-bad-idea" rel="nofollow"&gt;ran into a similar situation back in January&lt;/a&gt;... Mine was even trickier, though, since the production environment could stay up for a month before the bug would be triggered. Congrats on tracking it down.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Feb 2008 21:22:47 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:a36ded7f-419d-4229-a645-b131483a9753</guid>
      <link>http://blog.lathi.net/articles/2008/02/05/dynamic-constants-and-their-pitfalls#comment-423</link>
    </item>
  </channel>
</rss>
