Sat, 02 Sep 2006

Tilde strcmp.

At a previous employer, I spent about six weeks writing tests for code written by another developer. This developer had a PhD in engineering and years of experience in low level coding in both C and assembler. When I joined the project he had already been working on it for a number of months.

I'm very keen on the ideas and practice of Test Driven Development (TDD) and I'm also quite good at finding and writing tests for all those horrible little corner cases. I tried to sell the techniques of TDD to the developer but he wasn't buying. According to him doing testing would slow him down.

However, he was already doing some (rather ad-hoc) testing. Whenever he completed a new module he would temporarily hack the module that fed data into his new module, run the whole thing in the debugger and then in the debugger verify that the output of his new module was correct. He would then undo the hack in the preceding module and never test his new module again.

I tried to convince the guy that something more rigorous and repeatable was required. I told him how I had done development with testing and development without testing and that I much preferred the with testing. I explained how in my experience development with testing could proceed more quickly and with more confidence than development without testing. I laid it out as "I've tried it your way, how about you try it my way?" but he still wasn't buying.

Our manager however did understand the importance of testing so he suggested that I do it. Thats how I spent the next six weeks writing tests and filing bugs. During the first two weeks of testing, I was filing about 6 new bugs per day. I'm pretty sure that it was during this time that I found some other code written by this developer that contained this little boo boo.

  if ( ~ strcmp (s1, s2))
      /* Code goes here. */

Yes, thats a tilde character (ie bitwise not operator) where a logical not operator would make infinitely more sense. This was not the only error I ever found, just one of the most mind bending.

Fortunately, a couple of months after finding this and writing the test code I was able to get a much better job elsewhere.

Posted at: 08:37 | Category: WTF | Permalink