Programming hints and tips

Where do bugs come from?

A great program is the result of a great idea implemented without bugs from a sound design. We are all good programmers, and none of us deliberately put bugs into our code. So where do all the bugs come from?

One of the big differences between humans and computers is the type of mistake each makes. Most people do not make mistakes because they are incompetent or stupid, but because they forget things. Computers, on the other hand, almost never forget things, but they make mistakes because they are, basically, stupid.

People's mistakes occur when their memory or intuition does not give them the information they need. Examples include forgetting the exact name of a function (and using something close, which may actually exist), or changing one area and forgetting to change another, or forgetting that the variable called keysPressed is a flag rather than a counter. In that last case, calling the variable keyWasPressed would have avoided the problem.

Several of the ideas in my Rules Of Coding are based on the philosophy that the code should help me remember what needs to be done. This is why, for example, I try to avoid duplications. These almost always lead to somebody changing one copy and forgetting to change the other.

So when you are working on that next great work, take a little time when you write a new function to give it just the right name. Make sure that your variables do what another programmer would expect them to do. Put in the comments that will remind you (or the next programmer) what you intended when you wrote the code. When you modify the code and find you forgot something (such as why there's a '+1' in that expression), add some comments to explain it. (Even adding comments to say you don't know why may help some future programmer).

If programming is the art of implementing without bugs, then human programmers should be able to get better at it by finding programming styles and techniques which help their memories to naturally provide the right information.

Want to:
Read more from the soapbox
- Read Awk Words.
- Look at my AWK scripts.
- Read Robert's Rules Of Coding.
- Go back to the front gate.
- Visit another wagon.

Make a product that even an idiot can use, and only an idiot will want to.
Page maintained by Rob.