{ |one, step, back| } http://onestepback.org/index.cgi Jim Weirich's Blog en-us { |one, step, back| } http://onestepback.org http://onestepback.org/images/jwface.gif On my wall ... http://onestepback.org/index.cgi/Tech/Programming/DarthTest.red <p><img src="http://onestepback.org/images/rublog/DarthTest.jpg" alt="" /></p> <p>(from <a href="http://www.flickr.com/photos/sebastian_bergmann/2282734669/sizes/o/">here</a>)</p> Color Blind http://onestepback.org/index.cgi/Tech/Programming/ColorBlind.red <p style="padding-left:3em;"><em>Presented a talk on Test First Design today at work.</em></p> <p>I did a Lunch &#38; Learn presentation today at work on the topic of unit testing and Test First Design, included a discussion of the <span style="color:red;">Red</span> / <span style="color:green;">Green</span> / <span style="color:blue;">Refactor</span> technique. Unfortunately the red gun on the video projector was bad, ruining all the careful color coding I put in to my slides. So the technique will now be officially called <span style="color:black;">Black</span> / <span style="color:yellow;">Yellow</span> / <span style="color:blue;">Refactor</span>.</p> Tuning Performance http://onestepback.org/index.cgi/Tech/Programming/Performance.red <p><em>Uncle Bob has a couple examples of why performance tuning almost always has surprises in store for you.</em></p> <h2>Not What You Expect!</h2> <p>Bob Martin (Uncle Bob) <a href="http://www.butunclebob.com/ArticleS.UncleBob.PerformanceTuning">relates</a> a couple of stories on performance tuning and why your expectations are nearly always wrong.</p> <p>The moral of the story &#8230; measure, measure, measure whenever you have a performance problem. The root cause is prabably not where you expect it to be.</p> Dependency Injection Gone Bad http://onestepback.org/index.cgi/Tech/Programming/DependencyInjectionGoneBad.red <p style="padding-left:3em;"><em>It always bothers me to see mock objects creating and returning other mock objects.</em></p> <h2>David Chelimsky on Dependencies</h2> <p><a href="http://www.butunclebob.com/ArticleS.DavidChelimsky">David Chelimsky</a> of <a href="http://www.objectmentor.com">Object Mentor</a> <a href="http://www.butunclebob.com/ArticleS.DavidChelimsky.DependencyInjectionIsOnlyMostlyGood">notes</a> that dependency injection eases the pain of dependencies so much that we sometimes don&#8217;t bother to root out the <em>bad</em> dependencies. Read the <a href="http://www.butunclebob.com/ArticleS.DavidChelimsky.DependencyInjectionIsOnlyMostlyGood">article</a> and pay close attention to the dependencies in the original and final versions (I drew a couple <span class="caps">UML</span> diagrams to keep it clear in my head).</p> <p>Plus its got a great <a href="http://en.wikiquote.org/wiki/The_Princess_Bride#Miracle_Max">Princess Bride</a> reference!</p> Coding Standards -- Keep It Simple http://onestepback.org/index.cgi/Tech/Programming/ShortCodingStandards.red <p>The last several clients I have worked for loved to publish huge coding standards documents. A recent one was only 20 pages long (and that was one of the shorter ones).</p> <p>While browsing the new <a href="http://rubyonrails.com">Rails 1.0 web site<sup><a href="#fn1">1</a></sup></a>, I stumbled upon the Rails coding standard. Here it is in full (<a href="http://dev.rubyonrails.org/">link</a>):</p> <ul> <li>Two spaces, no tabs</li> <li>MyClass.my_method(my_arg)&#8212;not my_method( my_arg ) or my_method my_arg</li> <li>Follow the conventions you see used in the source already</li> </ul> <hr /> <p id="fn1"><sup>1</sup> You <em>did</em> know that Rails 1.0 was <a href="http://weblog.rubyonrails.org/articles/2005/12/13/rails-1-0-party-like-its-one-oh-oh">released</a> today, right?</p> Thinking of no one in particular ... http://onestepback.org/index.cgi/Tech/Programming/AgileEvangelist.red <h2>Via <a href="http://www.livejournal.com/users/sirenian/27195.html">Elizabeth Keogh</a> ...</h2> <p><em>If an Agile Evangelist falls in the forest and there&#8217;s no one to hear him, does he still go on, and on, and on about it?</em></p> <p>(<a href="http://www.livejournal.com/users/sirenian/27195.html">link</a>)</p> Test VS Behavior http://onestepback.org/index.cgi/Tech/Programming/TestVSBehavior.red <p><em>This article was sparked by a some conversations and discussions at RubyConf 2005.</em></p> <h2>Test VS Behavior</h2> <p>It seems fashionable these days to say that <b>Test Driven Development</b> is not about <em>testing</em> at all, but rather about (<em>fill in the blank</em>), where the blank can be filled with &#8220;design&#8221;, &#8220;behavior&#8221; or &#8220;specification&#8221;. At RubyConf this year, one of the &#8220;informal&#8221; discussion sessions around the pool area drew a large crowd of people to discuss this very topic.</p> <p>The RSpec ruby library (from Steven Baker, Dave Astels and Aslak Hellesoy) is a replacement for Test::Unit. Its main focus is to change the terminology surrounding the whole <span class="caps">TDD</span> approach to software development. I think this is key &#8230; it is not changing anything fundamental about the processes, it is focused upon the words we use in describing the process. But don&#8217;t be fooled into thinking this is a matter of semantics only, for the very words we use to describe something shapes our thought processes about the thing we are describing.</p> <p><span class="caps">BDD</span>, or Behavior Driven Development, is an attempt to improve <span class="caps">TDD</span> (Test Drive Development) by emphasizing the behavior specification nature of traditional <span class="caps">TDD</span>. By freeing <span class="caps">TDD</span> from its test bound terminology, we avoid some of the preconceptions (e.g. &#8220;Tests? Yuck!&#8221;) that developers bring to the table when discussing <span class="caps">TDD</span>.</p> <h3>So What&#8217;s Different</h3> <p>So, what&#8217;s the real different between RSpec and Test::Unit? Compare these two code snippets. First the &#8220;test&#8221; version:</p> <pre> class TestEmptyMovieList &lt; Test::Unit::TestCase def setup @list = MovieList.new end def test_size_is_zero assert_equal 0, @list.size end end </pre> <p>And now the specification version:</p> <pre> class EmptyMovieList &lt; Spec::Context def setup @list = MovieList.new end def should_have_size_of_0 @list.size.should_equal 0 end end </pre> <p style="padding-left:3em;"><strong>Note:</strong> <em>This is from RSpec version 0.2.0 available as a gem from RubyForge. The RSpec guys are currently thinking about using domain specific language for specification. So expect RSpec to change in the future.</em></p> <p>We can see that the structure between the tests and the specifications is very similar. Actually, more than similar, they are virtually identical. We just use <code>Context</code> instead of <code>TestCase</code>, use methods with any name rather than those that start with <code>test_</code>, and use object based assertions rather than specific assert methods.</p> <p>The result is something that reads rather naturally in ruby-accented English. But the functionally is essentially that of Test::Unit, just in different clothing.</p> <h3>Just Semantics!</h3> <p>I hear you saying &#8220;But it&#8217;s just semantics!&#8221; Of course it is. But leave out the word &#8220;just&#8221;. When conveying meaning, semantics is everything. And one of the important features of Agile developement is to convey the meaning of the code, in the code itself.</p> <p>Semantics are important!</p> <h3>Is it worth it?</h3> <p>Ok, semantics are important. But is it worth converting all our unit tests into specifications?</p> <p>Maybe.</p> <p>I do find the terminology attractive. But I have a large investment in Test::Unit style tests at the moment. And RSpec is still a bit rough around the edges (for example, the error reporting is not quite as mature as Test::Unit&#8217;s). And RSpec is going through some major changes at the moment (I mentioned the specification <span class="caps">DSL</span> they are playing with).</p> <p>What would I like to see? One area of specification that current <span class="caps">TDD</span> practices tend to be weak in is the specification of preconditions. Preconditions define under what circumstances it is valid to call a method. For example, it may be invalid to call a square root function with a negative argument. Looking at a list of tests (umm &#8230; specs), I can&#8217;t be sure if negative inputs are not allowed, or if the spec writer just forgot the cover that case.</p> <p>I&#8217;m not ready to switch over to RSpec yet, but it certainly is moving in interesting directions.</p> <h3>What I didn&#8217;t talk about</h3> <p>Those of you who were present at the poolside conversation at RubyConf may wonder why I didn&#8217;t mention mock objects, since mock objects were a major part of that debate. I see the Behavior VS Test debate to be completely orthogonal to the Mock VS not-Mock discussion. I&#8217;ll pick up the mock object debate in another posting, so don&#8217;t worry.</p> When Clocks Go Bad http://onestepback.org/index.cgi/Tech/Programming/WhenClocksGoBad.rdoc Message <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/103140">ruby-talk:103140</a> on the ruby talk mailing list mentioned problems with the sleep command interacting in strange ways with setting the clock. That reminded me of this story. <p> Way back when, I used to work on a data acquisition system that used an IRIG clock reader to timestamp our data samples. An IRIG clock works by reading a signal that sends the time once every second. Between the second ticks, the clock reader would interpolate the time down to the millisecnod level. This allows you to have several clocks on different systems all synchronized to the same clock signal. </p> <p> Our system had to take a sample every 20 milliseconds, so every cycle we would calculate the time of the next cycle. When the current data cycle was done, we would just wait until that next scheduled time arrived to start the next cycle. </p> <p> An IRIG clock reader is an interesting device. Normally it works really well, but occasionally (especially if there is noise on the clock signal line) it will misread the time signal. One particular reader we were using tended to misread a couple of bits in the minutes data with the result that for one second, the clock reader would add 30 minutes to the real time. When the next clock signal arrived a second later, the reader would start reporting the correct time again. </p> <p> Jumping ahead was no problem. Our software just used the reported time and continued to schedule data cycles. However, when the clock jumped backwards, the next data cycle was suddenly scheduled for 30 minutes in the future. The result was that our system mysteriously stopped dead for 30 minutes and then just as mysteriously restarted as if nothing happened. </p> <p> Time is a strange concept in computer programs. It is difficult to know what the exact time is while executing. Consider the following lines of code &#8230; </p> <pre> t = Time.now # Line 1 puts &quot;The Current Time is: #{t}&quot; # Line 2 </pre> <p> One would think that line 2 will print the current time. And most of the time we would be correct. But suppose I pressed the suspend button on my laptop between line 1 and line 2. It might be hours (or days) before line 2 prints out its now &quot;out-of-date&quot; time message. </p> <p> When you are dealing with times in the millisecond range, there are all kinds of things that will pause your program from one line to the next. Interrupts, page faults, and time slicing all interact in interesting ways to make the concept of &quot;time&quot; in a program something of a challenge. </p> <p> It wasn&#8217;t hard to fix our cycle scheduler to be well behaved, even in the face of uncertain time. And I learned a new respect for time in the process. </p> Miller on Mock Objects http://onestepback.org/index.cgi/Tech/Programming/MillerOnMockObjects.rdoc Charles Miller reminds us that there are <a href="http://fishbowl.pastiche.org/2004/05/07/the_two_uses_of_mock_objects">two reasons</a> for using mock objects. <ol> <li>To create an environment where the object under test can live. </li> <li>To test that the object under test correctly modifies the environment. </li> </ol> <p> Each use requires a slightly different API (#1 sytle APIs tend to be more flexible than style #2). I&#8217;ve used both styles of APIs in the past (and had been frustrated trying to use a style #2 API in a style #1 situation), but I hadn&#8217;t connected the differences with the reason for the differences before. </p> Some Things are Hard to Test ... ? http://onestepback.org/index.cgi/Tech/Programming/HardToTest.rdoc Perryn Fowler has a few <a href="http://jroller.com/page/perryn/20040325#some_things_you_just_can">words of wisdom</a> to share on what can and cannot be tested. I especially like his closing remark. <p> His remarks are short, so I have reproduced them here in full. </p> <dl> <dt></dt><dd><b><em>some things you just can&#8217;t test</em></b> </dd> <dt></dt><dd><em>Rubbish. Think harder.</em> </dd> <dt></dt><dd><em>Of course some things are very difficult to test, and in some cases the effort is not worth it when compared to the amount of risk it would mitigate - but that is a business decision rather than a purely technical one.</em> </dd> <dt></dt><dd><em>You can make this decision a lot easier by not putting important behaviour in things that are hard to test</em> </dd> </dl>