<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Talk Like A Duck: Are you aiming at the right bug?</title>
    <link>http://talklikeaduck.denhaven2.com/articles/2007/03/12/are-you-aiming-at-the-right-bug</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Are you aiming at the right bug?</title>
      <description>&lt;p&gt;How many times has this happened to you?&lt;/p&gt;


	&lt;p&gt;You have a really tough bug which defies all attacks.  Finally after pondering it for hours, or days or maybe more, you realize that the symptoms you are seeing aren&amp;#8217;t due to the posited bug at all, but are really something else.&lt;/p&gt;


	&lt;p&gt;Some examples.&lt;/p&gt;


On ruby-talk, I&amp;#8217;ve been trying to help someone who was having problems making Net::SMTP work.  He had some code which included:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;smtp.open_message_stream('sender@mail.com', [&amp;quot;dummy@dummy.com&amp;quot;]) do&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
And which was working, except that he really wanted to send an email to an address held in a variable rather than &amp;#8220;dummy@dummy.com&amp;#8221;  He was getting an error if he used &amp;#8216;a&amp;#8217; variable for the recipient address, and wondered why the array couldn&amp;#8217;t take a variable.

	&lt;p&gt;At first it looked to me, based on the examples he was giving that he was trying to pass the name of the variable as a string, and I suggested that he just change that parameter to [email].  But he said that that still didn&amp;#8217;t work (actually he&amp;#8217;d already tried it.&lt;/p&gt;


	&lt;p&gt;I was getting frustrated, until he rephrased something that he had briefly mentioned in his intial post, that the error he was getting was a &amp;#8216;tainted sender&amp;#8217; error.  The problem was really that he was getting the object referenced in the variable from a web form, and the framework code he was using was, rightly, tainting these values as a protection against security exposures.  There was no problem with passing values. The solution was to untaint the object after sniff-testing it.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve done similar things in the past.  My theory of what&amp;#8217;s wrong becomes an idee fixee which blocks me from seeing the truth.&lt;/p&gt;


Two more (colorful) examples:
&lt;ol&gt;
&lt;li&gt;Some years ago I was building an application in Smalltalk which allowed for boxes to be drawn with connecting lines which were anchored to the boxes so that they moved when a box was moved. The kind of thing you need for a pert chart or other network diagram.  Everything was working until I changed the lines into separate objects, when the lines just disappeared.  I kept trying to figure out why the line objects weren&amp;#8217;t drawing, until it finally dawned on me that they actually &lt;b&gt;were&lt;/b&gt; drawing. I was using xor to draw them so that I could easily erase them in order to animate them when the boxes moved.  The only way I figured this out was because in desperation, I changed from using a black line to a red one, which allowed me to see the line being drawn, then erased as the color changed from red to cyan.  The problem wasn&amp;#8217;t in the line object at all but in the animation code which was drawing then erasing the line.&lt;/li&gt;
&lt;li&gt;More recently, I&amp;#8217;ve been playing with a Ruby application which is a Sudoku puzzle assistant, as an exercise in learning RubyTk among other things.  It displays the values of the initial squares given in the puzzle in black, and uses blue for squares which you have set yourself.  The model object for the square has a boolean instance variable named initial which is set when the puzzle is initially entered so as to show this difference.  Once the application is set in &amp;#8216;play&amp;#8217; mode no other squares should have this variable change.
&lt;p&gt;I&amp;#8217;d gotten pretty far along with this, including having commands which search for constraints on the values of squares, then apply those constraints, etc.  I&amp;#8217;d recently improved the ability to save the puzzle in a file.  My first pass just saved the initial values, more recently I&amp;#8217;d added saving the state of the solution as well.&lt;/p&gt;
&lt;p&gt;I was happily playing with testing, when I noticed that in one case finding a contraint, changed one of the squares from black to blue.  I spent several days trying to figure out why the initial status of that square was being changed.  I pulled out all kinds of Ruby-foo to figure this out. I made sure that the variable was only accessed by a reader and writer accessor methods so that I could print a backtrace when it changed, but I never could find the culprit.&lt;/p&gt;
&lt;p&gt;Of course the punchline was that there was no such culprit.  I finally realized what was happening when I, for what was probably the hundreth time, started the application and read in the test file containing the puzzle.  That&amp;#8217;s when I realized that the square in question was actually &lt;b&gt;not&lt;/b&gt; one of the squares which had a value given in the initial puzzle.  The &lt;b&gt;real&lt;/b&gt; bug turned out to be that, non-initial squares weren&amp;#8217;t being drawn correctly when the puzzle was read.  It wasn&amp;#8217;t until I did something which caused that square to be re-displayed that the value got drawn in the correct, blue, color.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Gerry Weinberg had a lot to say about such things in his thirty-six year old classic book &amp;#8220;The Psychology of Computer Programming.&amp;#8221;  He calls this psychological set, which is the tendency for us to see what we want/expect to see.  It&amp;#8217;s what makes proofreading hard, we tend to mentally read a word like &amp;#8216;dack&amp;#8217; as if it were printed &amp;#8216;duck.&amp;#8217;  He describes an experiment which showed that commented code can be harder to debug than uncommented code because most comments describe what the code is &lt;b&gt;supposed to&lt;/b&gt; do rather than what it &lt;b&gt;actually&lt;/b&gt; does.
&lt;p&gt;So the next time you get stuck on what seems like a hard debugging session, step back and consider whether your minds-eye or your pre-conceptions are blocking you from considering the real reason for the symptoms you are diagnosing.</description>
      <pubDate>Mon, 12 Mar 2007 12:26:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:df538843-d72f-487d-bb83-37ddd2682008</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/03/12/are-you-aiming-at-the-right-bug</link>
      <category>ruby</category>
      <category>debugging</category>
      <category>psychology</category>
    </item>
  </channel>
</rss>
