Aesthetics of Debugging

Posted 18 Dec 2000 at 00:58 UTC by Bram Share This

Some people told me that if you read this essay thinking of 'turds' as 'bugs' and 'your home' as 'your code' then it takes on a whole other meaning. If you read it you'll probably see what they mean.

Bram's guide to healthy living.

Maintain a clean home

There are individuals who live in unspeakable squalor. Their homes reek of the stench of fecal matter, overpowering everyone who comes near. Noone approaches without becoming aghast at the filth. Their relatives have long since gone screaming in horror and noone, not even a highly paid cleaning crew with gas masks, is willing to come near their home. I strongly urge you to fight the constant onslaught of new turds into the home at every moment, night and day, lest you sink into the same pit as these disreputable individuals.

Hate thine enemy

The first step to controlling this menace is to rededicate yourself to fighting it. Forget your old complacency. Remember the sense of purpose you once felt.

Focus, for a moment on the thought of turds. Think of how they smell. Imagine it sitting in the middle of your home. Feel the revulsion. Let the hatred seethe through your veins. Do not forget this feeling, or you will surely become lax again in the future.

Know thine enemy

Some individuals are so pathetically blind that they cannot tell what is a turd and what is not. Many resort to the repugnant practice of, when they find a suspicious object in their home, they pick it up, leave it outside, and smell their house again. If they don't think they detect an improvement, they then bring the dubious item back inside. For such individuals, there is no hope. You must know every nook and cranny of your house, know exactly how all the parts of it appear, even the brown ones. If, upon looking at a section of your home, you honestly cannot tell whether any part of it is a turd, then you are in the midst of spiralling downwards into filth.

Three turds smell the same as one.

It is all to easy to fool oneself into thinking there is merely 'one last turd' strewn about one's home which needs to be cleaned up. If you have allowed the repugnant odor of fecal matter to stagnate, you have unknowingly laid another, and another, and your folly has left you with an unspeakable mess. Remain ever vigilant in dispensing of every turd at the slightest whiff of it's presense, or you will find yourself amidst a disaster of unknowable proportion.

An ounce of prevention is worth a pound of cure

It would be wonderful if it were possible to permanently remove the turd menace from one's life. Barring that, one can reduce it as much as possible. Eat well, exercise regularly, and above all, get enough rest, for grogginess leads to carelessness. Many a poor soul has returned to their home only to be aghast at the squalor left while in a weakened state.

Out of sight, out of mind

Upon achieving a state of cleanliness, forget not to put the thought of turds out of your mind. Dwelling on them while in the pure state can bring you nothing but misery. Enjoy the fruits of your labors for as long as possible. Only then will you be prepared to cope with filth when it inevitably arises again.

Cleanliness is next to godliness

I can think of nothing more important to one's well-being and state of mind than maintaining a fresh, clean abode. Maintain a state of purity at all times and you will find yourself happy, energetic, popular among women and friends among men.

Remain ever wary

Finally, a word of caution. One must never rest in the battle against turds. You will surely produce more, and no matter how pure you are today it will all be for naught if you become complacent tomorrow. Be brave, and never lose focus in your struggle against this eternal enemy.

It Helps to Use a Toilet Rather Than Your Hands, posted 18 Dec 2000 at 06:25 UTC by goingware » (Master)

There is no substitute for having good tools and knowing how to use them. Here are a few you should know about for various platforms.

There are a number of memory validation and leak testing tools:

  • Bounded Pointers for GCC still in development but basically working - this is Free Software
  • Spotlight for Macintosh PowerPC. As a Mac developer, I wouldn't dream of shipping a product until it tested clean with Spotlight.
  • BoundsChecker for Windows (Visual C++ only apparently)
  • Purify for Unix (but not Linux) and Windows

While Java programs don't suffer from invalid pointers they can have memory leaks because memory is not garbage collected if it is still referenced at all, even if you don't mean it to be. A tool for this and other Java performance problems is:

Having a tool like the above will save you untold hours of debugging time and will allow you to spend much more of your time doing more pleasant things like implementation and architecture. They'll make you a better programmer too because they will catch lots of bugs that would otherwise slip by into production and maybe never be caught.

I hesitate to mention this but I remember well the time when I used to debug exclusively with printf - learn to use GDB well on Linux (see Stallman's book on GDB) and learn the appropriate debugger on any platform you use. Also learn how to use an assembly debugger - I maintain a MacsBug Tips and Tricks page for that MacOS assembly debugger.

Learn how to debug effectively. You can clean the turds out of your house eventually by just swatting randomly with a shovel but it will be much more effective if you actually take aim and carry your findings to the toilet. A good start would be to search for "debugging" on and get one of the books in the list.

I got my start in debugging using the scientific method from Robert Ward's Debugging C which is regrettably out of print - a lot of the particulars in the book are outdated (it preceded the widespread use of source debuggers) but many of the principles are invalid.

Finally, learn good style in your language of choice.

If you program in C++ (what can you suggest for other languages?), it is critical that you read Scott Meyers' Effective C++ and More Effective C++ as well as the C++ FAQ which is available online and in expanded form as a published book - also see the entry crap! It's out of print I guess I should have bought one when I had the chance.

The above C++ style guides address most of the objections most people had to using C++ in the recent article Why don't C++ and free software mix - that most C++ programs are badly written and bloated. They don't have to be, and the reason they often aren't isn't so much that C++ is a bad language, but that while it is a complex language and difficult to master, it can be mastered and you can write clean and efficient programs in it (the other objects centered on the lack of good compilers and libraries, which only time will address).

The Association of C and C++ Users has an extensive collection of book reviews online, BTW - written by members who are practicing programmers - and they don't pull any punches when they think a book is a waste of your time. Browsing through the subject listing I see they cover languages other than C++ - 254 Java titles are reviewed, 30 on perl, 7 on python and so on. Especially interesting are the reviews listed under the subject of Writing Solid Code - 96 titles.

I feel that the money I spend on books is much better spent than any of the investments I've ever made in software or equipment. Capital investment is necessary to a software business but time invested in training by reading the better books on software practice will both increase one's value far out of proportion to the money spent on the books and will make the time spent programming more interesting and pleasant besides.

More debugging tips, posted 5 Jan 2001 at 16:34 UTC by emk » (Master)

Warning: Highly personal debugging rant ahead. Your mileage may vary.

Test suites, test suites, test suites!

If you don't have an automated test for a feature, that feature doesn't really exist. :-) Even the most paranoid and closely inspected code can still hide bugs if you don't test it ruthlessly.

Since test suites are a pain to write, and a pain to run, try to make things easy on yourself. Write a lightweight test harness (or snag one of the excellent ones by the Extreme Programming folks). Make sure you can write a whole bunch of new test cases in just a minute or two. Make sure your test suites run quickly, so you don't get bored.


In an ideal world, we wouldn't need to hunt for bugs. When a bug occurred, your program would print out a message containing the file name and line number of the problem.

Make friends with assert.h. Use more assertions than you need, then add some more. Check preconditions, postconditions, and even invariants.

Malloc Debugging

If you're programming in C, you're going to have lots of nasty little memory management bugs. You'll access memory which has been freed, overwrite the bounds of your arrays, and leak data structures. If you're extraordinarily careful and conscientious, you'll merely do it less often.

Lots of wonderful malloc debugging tools have already been recommended in this thread. Let me recommend two more:

MemProf: A most excellent GPL'd utility by Owen Taylor. It profiles your program's memory usage, and searches for memory leaks using a conservative garbage collector. Very slick.

Electric Fence: A nifty library by Bruce Perens. This is essentially a free software version of Purify (but without some of the more advanced features). It can be used to catch block overruns, block underruns and accesses to free'd memory. Read the man page carefully; you'll need to run your program several times with various environment variables set to test for everything.

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

Share this page