(via Slashdot)How to Keep Your Code From Destroying You
It is always good to see a short story on how to code better.
Mr Vogel’s tips(my comments)
- Tip 1: Comment like a smart person.
I don’t like comments and I only comment a section when I’m forced to do something tricky or unusual. Comments tend to get out of sync with the code and in the end the code is the truth, the whole truth and nothing but the truth. In general I prefer well named and well constructed code. The code should tell the story.
- Tip 2: Use #define a lot. No, a LOT.
This is basic. Hard-coding any values into your code will return to bite you. Enums are great if your language supports them.
- Tip 3: Don’t use variable names that will mock you.
Clear, Simple, Meaningful. And try to keep them 5-12chars or so long. Shorter and they tend to be hard to search for and longer mangle your formatting. Never even think about using I or O they look too much like 1 and 0.
- Tip 4: Do error checking. You make errors. Yes, you.
Yea so. Don’t worry about it. The last thing your code needs is for every function to start with a bunch of range checks. Unless you are doing input or interface processing, general validation is a waste. Validate when you need to validate. If a call ass-plodes when I send it null, why did I send it null. Generally you kinda know that if a method takes an object as a parameter it probably needs a non-null object. When you create the object, check that it really got created. Not very productive passing a useless object around now is it.
- Tip 5: “Premature optimization is the root of all evil.” – Donald Knuth
This is a good one. Rarely is what you think slow actually the thing that is making it slow. Write clean, clear, simple code. If its slow, run it through a profiler or even instrument it yourself. Find out where it is slow. Figure out why. Then address the problem. Guessing is for amateurs.
- Tip 6: Don’t be too clever by half.
Stupid is as stupid does. Simple works. Clever takes work. Clever works less than simple and requires more fixes, more testing, more tweaks, and gives more headaches. Guess which is more clever? The problem is that clever is more fun at the start… “The first one is free.”
My tips:
1) Get a great editor. Either spend the time and actually learn emacs or go out and buy SlickEdit. You’re a programmer not a bricklayer! Get the tools you need. They save you time, energy and frustration and let you code with out all the the muss and fuss.
Use your editor’s features. Learn search, tagging, use references, go directly to definitions, find all in project. Figure out regular expressions. Learn to block cut. File/multi-file Diff is your friend.
2) Source control. Its not coding right? Wrong! Sometimes going back is the fastest way forward. Source control saves lives.
3) KISS. Keep it simple simple. If you can’t think of a simple way, get help.
4) Always keep it working. There is nothing worse than a broken application. Lots of small changes beat the one big one.
5) Test, test, test. Automated regression tests rock. Test before, test after, test well.
6) Good formatting and good names mean more than you know. Choose wisely and don’t be afraid of search and replace. When the meaning changes, change the name. Think of a better name, change the name.
7) Methods/function should be short (generally a page or less) and do one thing.
8) Cut/paste is the tool of the Devil. Don’t do it. Make a common function. It really sucks making the same change in 20 places across 16 files. And then there is all the retesting…
9) Give/ask for help. Sometimes you just need a fresh set of eyes. The more you help others the easier it is to ask for help.
10) Always try to write positive tests. Negative conditionals wear out the brain. (Just ask De Morgan or Meg)

