Programming Rules

I read an article on the web by Rob Pike. He is an important guy in the UNIX/C world. I know I own at least one UNIX text authored by him. For some reason, I thought he was also one of the authors of the awk scripting language. However that does not seem to make sense as the letters in awk are supposed to represent the first letter of the last names of the three guys who invented the language.

It seems I digress. The article I read was entitled “Notes on Programming in C”. The funny thing is that many of the topics discussed by Pike are not specific to C. They can be applied to most programming languages. That is kind of like a Code Complete by McConnell. Let’s start with some of the C specific items as they are few. One is that you should not include another file within an include file itself. In other words, you should not nest include files. Another tip was that pointers are preferable to objects. I think that means pass pointers to objects instead of making copies of the objects themselves.

Now let’s move on to some of the guidance that can be applied whatever programming language you are working with. The first is the short variable names are often better. For instance, it is reasonable to use the variable i for a loop index variable. Next you should strive to eliminate comments. This may sound contrary to what you were taught when you first started programming. However the best code is clean, simple, and easy to understand on its own. It does not need to be littered with comments. This eases maintainability. You can also trust the code always. Comments that are wrong or misleading are harmful.

Finally let’s talk about procedures and functions. Procedure should be named according to what they actually do. That sounds simple. But you would be surprised how some procedures are named which have nothing to do with their function. Next you know that a function returns a value. You should name the function based on the type of value it returns. Any functions returning a BOOL should be in the form of a question. Examples of this are isOpen() and canDelete().