Wednesday Oct 19, 2005

Something I'm bad at

I've re-discovered something I am bad at. I'm slightly ashamed to admit it, but I am very bad at testing my own code. To be specific, I'm bad at spotting when fixing one problem causes or re-introduces a different one. I already know the fix for this. It is quite simple: write the unit tests as you go along, and make sure they all still pass after you've added a new bit. I let myself slip away from doing this, because I was feeling some deadline pressure. But I just proved to myself yet again that writing tests doesn't cost you time, it saves you time.

Well, lesson learned — properly this time, I hope.

Tag:

Tuesday Sep 20, 2005

Stupid errors I have made, #1 of several thousand

Today's stupid programming error involves recursive directory handing in C. I was doing this, and wondering why it didn't work:

void
foo(char \*path)
{
        /\* stuff \*/
        if(condition) {
               foo(dirname(path));
               /\* other stuff \*/
        }
        /\* yet more stuff \*/
}

Can you see the problem? Yup, that's right, dirname() modifies the string pointed at by its argument, so when you pop up a level, your path is still trimmed. The solution is to strdup(path) before the recursive call, and free() the copy afterwards.

D'oh!

Tag:

About

terryh

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today