Debugging With Print

Álvaro Ramírez has a guilty secret: he sometimes debugs code by inserting print statements into it. I was really happy to see his post because it’s a guilty secret that we share. Debuggers and similar tools are great and really useful but sometimes it just seems like putting in a couple of print statements is easier.

Ramírez is even lazier than I am—which some say, not without reason—makes him a better programmer than me. In any event, rather than printing a descriptive message indicating where in the code the message came from, he’s more apt to just print something like “yay” or “got here”. That’s fine when there’s only one such statement but is less useful when there are a number of similar print statements. Ramírez’s way of dealing with this is to add a numerical suffix—yay1, yay2, ⋯ —to distinguish which print he got to.

After a while, it became a chore to keep track of the latest suffix. Ramírez’s solution was to write a bit of Elisp that searches through the buffer for the keyword, “yay” or whatever, records the highest numerical suffix, and inserts a print for the keyword with the next value of the suffix. Of course, being Ramírez, he had to tweak the code to be a bit more flexible. That included an option to insert random words instead of a fixed keyword with a numerical suffix.

Often, a debugger is the right solution. After all, when you stop at a breakpoint you can query the executable for the values of various variables and other useful information. Still, sometimes it’s overkill and a simple print is all you need. If you find yourself inserting a lot of prints, Ramírez’s solution may be just what you need. Take a look at his post for more information.

This entry was posted in Programming and tagged . Bookmark the permalink.