<?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: How Old Are You?</title>
    <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>How Old Are You?</title>
      <description>&lt;p&gt;In one of my &lt;a href="http://rubyonrails.org"&gt;Ruby on Rails&lt;/a&gt; apps, I have a &lt;code&gt;User&lt;/code&gt; class with a &lt;code&gt;dob&lt;/code&gt; date of birth field.  My client wants me to display the user&amp;#8217;s age.&lt;/p&gt;


	&lt;p&gt;I had added this code a very long time ago:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;age&lt;/span&gt;
  &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Date&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;today&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;dob&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;to_i&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt; &lt;span class="number"&gt;365.25&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;I just went back and looked at that and was surprised I had let that code stand.  It was just so smelly.  I poked around and didn&amp;#8217;t find any good helpers to clean it up.  So, I started asking around to the &lt;a href="http://ruby-lang.org"&gt;Ruby&lt;/a&gt; smart-guys I know.  Not to name drop, but &lt;a href="http://onestepback.org/"&gt;Jim Weirich&lt;/a&gt; is a member of &lt;a href="http://www.xpcincinnati.org/"&gt;XP Cincinnati&lt;/a&gt;.  When I asked him about it he responded with this &lt;a href="http://rafb.net/paste/results/rL4Dt798.html"&gt;paste&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;That demonstrates a lot of things I like about Jim.  First, he&amp;#8217;s a super friendly guy and very accessible.  I don&amp;#8217;t remember if he gave me his &lt;span class="caps"&gt;AIM&lt;/span&gt; nick or if it&amp;#8217;s listed on his web site.  Either way, he never acts bothered by my somewhat periodic interruptions about ruby related stuff.  Second, he responded with code; not just code, but code with unit tests; not just simple unit tests, but fairly exhaustive unit tests.  He was able to demonstrate with those tests why my algorithm wasn&amp;#8217;t good enough; several of his tests failed with my code.&lt;/p&gt;


	&lt;p&gt;Jim practices what he preaches.  He&amp;#8217;s a big advocate for both Ruby and Test Driven Development.  Even with this simple exercise he started off with good tests.  Thanks Jim!  I appreciate your help.&lt;/p&gt;</description>
      <pubDate>Thu, 05 Jan 2006 12:49:22 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:6de005ec-906b-4955-a1bf-da37a5822ea4</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you</link>
      <category>Test Driven Development</category>
      <category>Ruby on Rails</category>
      <category>dates</category>
      <category>testing</category>
    </item>
    <item>
      <title>"How Old Are You?" by Christopher Jones</title>
      <description>&lt;p&gt;Anyone have the code from the paste. I thought I had found the answer to my problem but the paste is gone. Even archive.org does not have it.&lt;/p&gt;


	&lt;p&gt;You can email me at the given address. Thanks.&lt;/p&gt;</description>
      <pubDate>Mon, 10 Apr 2006 14:35:55 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:91c8e351-4028-4af1-84d7-5eaf4d9b57de</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-194</link>
    </item>
    <item>
      <title>"How Old Are You?" by Sean Smith</title>
      <description>&lt;p&gt;The paste is gone! That&amp;#8217;s what I get for leaving readings for later&amp;#8230;.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Jan 2006 20:31:32 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:81a02d5d-a788-41ef-b670-46f5a59893d9</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-134</link>
    </item>
    <item>
      <title>"How Old Are You?" by Lewis</title>
      <description>&lt;p&gt;Sorry, you guys are right, it&amp;#8217;s not really a bug just undefined. The function totally works as written, it just when I ran it with my birthday, one of the tests failed.&lt;/p&gt;


	&lt;p&gt;Maybe a test_lewis_birthday should be required on all date classes :)&lt;/p&gt;


	&lt;p&gt;(or maybe not)&lt;/p&gt;</description>
      <pubDate>Fri, 06 Jan 2006 08:46:56 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:014882d0-8e31-4f09-a171-43e670e87d6a</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-131</link>
    </item>
    <item>
      <title>"How Old Are You?" by Jim Weirich</title>
      <description>&lt;p&gt;Hi Lewis,&lt;/p&gt;


	&lt;p&gt;I don&amp;#8217;t see a bug involving the first of January.  In fact, I added this test case to make sure:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def test_lewis_birthday
  born = Date.parse("Jan 1, 2005")
  assert_equal 0, age_in_years(born, born)
  assert_equal 0, age_in_years(born, Date.parse("Jan 2, 2005"))
  assert_equal 0, age_in_years(born, Date.parse("Dec 31, 2005"))
  assert_equal 1, age_in_years(born, Date.parse("Jan 1, 2006"))
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;If you are pointing out that asking for the age of someone who is not born yet (i.e. the today date is prior to the birth date) produces nonsensical output, then indeed it does.&lt;/p&gt;


	&lt;p&gt;If defining age_in_years for that particular condition is important, one needs to decide what the proper outcome is (zero?, negative years?, exception?) and add the appropriate test cases.&lt;/p&gt;


	&lt;p&gt;For myself, I should have stated that the precondition for age_in_years was that today &amp;gt;= born rather than leave it implicit in the tests.&lt;/p&gt;


	&lt;p&gt;&amp;#8212;Jim&lt;/p&gt;</description>
      <pubDate>Fri, 06 Jan 2006 08:27:39 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:207b9685-2d8e-4663-b635-84fca8fcbfa0</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-130</link>
    </item>
    <item>
      <title>"How Old Are You?" by Doug</title>
      <description>&lt;p&gt;Good catch!  Although I don&amp;#8217;t know that it&amp;#8217;s a bug as much as just undefined.  When I posed the question I never said what I wanted to happen with birthdays in the future.  It could be argued that your age (given your birthday is in the future) is negative. :-) Ether way, I&amp;#8217;ved added a &lt;code&gt;result = 0 if result &amp;amp;lt; 0&lt;/code&gt; statement in &lt;code&gt;age_in_years&lt;/code&gt; and these test cases:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;    @born.dob=Date.parse(&amp;quot;Jan 1, 2005&amp;quot;)
    assert_equal 0, @born.age(Date.parse(&amp;quot;Jan 2, 2005&amp;quot;))
    assert_equal 0, @born.age(Date.parse(&amp;quot;Dec 31, 2005&amp;quot;))
    assert_equal 1, @born.age(Date.parse(&amp;quot;Jan 2, 2006&amp;quot;))
    assert_equal 0, @born.age(Date.parse(&amp;quot;Dec 31, 2004&amp;quot;))&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The format of these tests are a little different than Jim&amp;#8217;s because of my environment.&lt;/p&gt;</description>
      <pubDate>Fri, 06 Jan 2006 08:24:33 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:297cd8e7-9943-4ab7-b0de-b3387859b2ff</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-129</link>
    </item>
    <item>
      <title>"How Old Are You?" by Lewis</title>
      <description>&lt;p&gt;There is a bug in the code.&lt;/p&gt;


	&lt;p&gt;For example: dob = 1/1/2005
the day before is 12/31/2004&lt;/p&gt;


	&lt;p&gt;so 2004 &amp;#8211; 2005 = -1&lt;/p&gt;


	&lt;p&gt;My birthday happens to be on the 1st of January :)&lt;/p&gt;</description>
      <pubDate>Fri, 06 Jan 2006 06:45:50 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:520f0032-3a3a-46be-a690-7f43d490ac19</guid>
      <link>http://blog.lathi.net/articles/2006/01/05/how-old-are-you#comment-128</link>
    </item>
  </channel>
</rss>
