K-Pad is a multi-featured notepad / organizer for Windows: print-out pages or booklets of photos, tables, and rich text.More...
This is my support blog, featuring help, tutorials, and comment. Welcome. :-)

Monday 27 October 2008

Transaction Coding

K-Pad crashed on me at the end of last week. I'd been testing new list code, albeit on a large file that I'd been working on for some time. "That's okay," I thought, "I'll just load the auto-save. I couldn't have lost more than a minute of work..."

But no. The auto-save file was AWOL.

Investigating, I stumbled upon the irony: the auto-save itself had failed, resulting in the crash. So I lost 25 minutes of work. Annoyed, I had a closer look at the coding around the auto-save. In meta language, this was:

  1. Run auto-save after x minutes;
  2. If a current auto-save file exists, delete it;
  3. Save to the vacant auto-save file slot;
  4. Return.
The code crashed on step 3. Result: thanks to step 2, I didn't even have the previous auto-save file.

This was lazy programming on my part. Yes, the save shouldn't have failed. However, it did, and I didn't deal with it. Enter transaction coding. The new meta code is now:

  1. Run auto-save after x minutes;
  2. If a current auto-save file exists, move it to designated temp directory;
  3. Save to the vacant auto-save file slot - but catch any errors this produces;
  4. In the event of any error, and a previous auto-save exists, make a note, and move the file back;
  5. In the event of any error, and no previous auto-save exists, make a note, and try again in a minute;
  6. In the event of no error, delete the previous auto-save file from the temp directory;
  7. Return.
I already had similar transaction coding with the regular save, but it was sloppy of me to forget the auto-save. At least this is now fixed for the next release.

No comments: