Saturday, January 27, 2007

Programming errors

In my last entry, I said, "I don't know. The trouble with software is - you know there's going to be a problem with it eventually. You just haven't found it yet."

This reminded me of something I read in the second C language book I read (the one right after reading Kernighan and Richie). I don't have the book anymore and don't even remember the name. So, this isn't an exact quote but it went something like, "You'll find that as you gain experience with the C programming language, the mistakes you make will become more subtle and more difficult to find." I've found this to be fairly accurate.

While I still make stupid mistakes like the famous:

if ( condition ) ;
{
function();
}

where function() is always called regardless of the state of condition (because I mistakenly put a semi-colon after the if ( condition ) statement). I find more of my errors involve the architecture of the program rather than syntax errors. Of course, when moving to C++, whole new realms of errors are opened to you. The Java language removes some of these errors from your repertoire but adds others.

So, I seem to paint a rather pessimistic picture. You never gain on the errors. The more you write, the more experience you gain and the more difficult it gets to find the errors. But in reality, I find that as I gain experience, I have learned to avoid many errors. I find it pays to write your program as if someone else will be needing to read it, understand it and use it. This keeps me from using tricks while coding. It helps me to keep my code transparent. The days of needing to use obtuse coding styles so that code will run faster or take less room are gone. Compilers have improved over the years.

It pays great dividends to write code that makes sense, uses meaningful symbol names and is well commented. While comments can sometimes get out-of-sync with the code when you go back to make changes in the code, I find comments to be very important. You can only explain the reasoning behind an algorithm by using comments. What I'd like to see, someday, is a compiler that lets you embed links, pictures and diagrams in the comments so you can point to a paper or website that explains the reasoning behind that section of code.

Comments are not the only way to make code better, though, and I try to not depend on comments completely.

No comments: