Thursday, April 17, 2008

The sweetest thing

Every now and again I have a moment of realisation where I am thankful that somebody else thought of something before I did. I had one of these just the other day. They are a little bit like this xkcd comic.

I was trying to find a bug in a binary tree and the problem would not manifest itself immediately. The code worked for a seemingly arbitrary period of time, but at a depth of N a get out clause needed to be true and it wasn't. Therefore it kept on recursing until it either could not create any more branch-nodes or it generated a stack-overflow.

I attempted to step through my code trying to find my bug, but I find it hard to debug recursive code. You’re never too sure how deep you need to go before the problem manifests. After missing the bug I pondered how I could represent the tree to find the bug. One of the things I have been teaching myself to do of late, is when I have a hard to reproduce bug, ask myself the question "How can I best model my data to visualise the bug?". It is not quite changing my work practices to stop bugs before they occur, but it really helps once they do manifest.

XML came to my rescue. I added a dump routine that iterated the tree dumping it out in well formed xml. I then used Notepad++ to display the xml with indenting and the bug was immediately visible.

So the moral of the story is: I love xml and am glad somebody else thought of it a long time ago. It’s a sweet way to visualise certain types of data and with the assistance of text indentation it is even better.


Just so you don’t think I’m blinded by xml-lust: Xml for network messaging formats is still a bad idea. Seriously, any format where you spend 50% of your bandwidth describing the data being sent is wasteful.

p.s. I am aware that this is a bit of an odd post.

No comments: