Could you give a (small :-) code example that we could talk about that shows a bug that you missed during TDD?

I'm going to have to owe you that one for the time being, at least as far as code goes. There are three bugs I can think of. Probably the lamest happened with the code I just recently posted. I had a battery of around 40 tests already, all succeeding, when suddenly the test suite started seg faulting while setting up tests whose fixtures where very similar to those of earlier tests.

To make a long story short, and much simpler than it appeared originally, the problem looked like this:

my %hash = map +( $_ => 1 ), ( 1..$reasonable ); my $it = Hash_Iterator->new( \%hash ); $hash{ $reasonable + $_ } = 1 for 1 .. $a_few_more; $it->start; # BOOM!
Basically, my code had not taken into account the fact that as hashes grow, perl will allocate more memory and move the (now overcrowded) entries to more spacious digs. When this happened, my iterator was left with a dangling pointer, leading to the seg fault. Shame on me for not thinking about this from the beginning, but my point is that this bug was there all along, but my "simple tests", testing simple things, one-feature-at-a-time, missed it entirely. As BrowserUk said, if the programmer writes his/her own tests he/she is bound to omit the tests that would make manifest the nasty bug; the same lapse that led to the bug, leads to the missing test.

The hallmark of this and all other nasty bugs I've run into while doing TDD, is that they kick in only after a particular extended sequence of manipulations that leaves the system in a state not foreseen by the programmer. Typical TDD tests tend to miss these bugs, because these tests, necessarily, tend to have very short horizons. The more elaborate the sequence of steps to bring the system to an unsound state, the less likely that the unknowing programmer will think of writing any test that will bring on the problem. (Note that the tests that elicited the bug I just described were not expected to fail the way they did. I was testing something else entirely. It was just good luck that they picked up this problem.)

This is a very interesting subject. I have been meaning to write a meditation/book review on TDDBE. I hope to give more details then, including, hopefully, some real code.

I think that, as you say, I probably have not quite gotten the hang of TDD yet, which accounts for some of the problems I'm having with it. But I also think that the formulation of TDD given by Beck, which is the only one I know, has been dumbed-down beyond the point of usefulness. But this is a subject that deserves more time than I can give it now.

the lowliest monk


In reply to Re^9: Self-testing modules by tlm
in thread Self-testing modules by DrWhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.