Tuesday, May 15, 2007

A Little Coding Exercise

For many years, I spent all my workday at Motorola writing programs for internal use. Before that, I even dabbled in commercial software. But since then, the only coding I've done is a few tools for myself of my relatives. Instead of designing software, I've been designing novels.

But today I broke down and wrote a little addition to my website, a coupon manager. The idea is that I can make a number of coupon codes that I can print on the back of my business cards, or hand out at conventions. A person would then go to the Coupon page on my website and enter the magic code. Then, they would receive a download or special offer or something (I haven't worked all that out yet.)

The core of the software was a little XML database containing coupon codes, what was to be offered, and how many times the same code could be used before it expired. Seeing that I'd written other software using dumbed-down XML databases, I copied over the bulk of the code from the software that I'd written for my wife Mary Ann Melton's website and my sister Mary Solomon's site. These are all dynamic sites that generate the pages on the fly from an XML database.

All went well and the program came together fairly quickly. Then, as is often the case, a puzzling bug appeared.

I had populated the database with three items, each with their own counters. I was happily entering coupon codes and downloading test files, when all of a sudden, everything stopped working. Once the first test file reached the end of its counter limit, none of the codes could be recognized anymore.

I puzzled over it. I wrote debugging code. I reset the counters. It repeated. Once ANY ONE of the coupons reached its expiration limit, ALL of the codes became invalid.

Finally I gave up trying to debug the new subroutines I had created and started following the logic even into the old code that had happily been producing web pages for years. And there it was. Off in a subroutine that parsed the XML database into variables, was a rare bug that caused the parser to fail once it saw '0' as a value. In all the time that it had been working to create web pages, there had never been an element that contained nothing but a single zero, so it had never failed.

Now, mystery solved. I just have to decide whether to recode the parser ( an opaque one-liner regular expression), or just to write the code to never reach zero.

You know, coding is much more fun than SudoKu.

1 comment:

Chris said...

Good job finding the bug! Not always easy to track those down, esp in legacy code.