- Think about what you want to do, and how you want to do it, before starting coding.
- Build up a program in small steps. Write a bit of code, test it to see if it works, then write the next bit. If you write a large chunks of code, it may well have several mistakes in, and the combination of mistakes will be very difficult to find and correct. If using a feature that you haven't tried before, then write a small webpage to try it out first, before adding it to your main code. If you put it in a complicated program straight away and it doesn't work (which is quite likely), then you won't know if it's a simple typing error or that you've misunderstood how the code works.
- Use copy-and-paste whenever you can. Mistakes can creep in whenever you type something, and it can be difficult to remember all the brackets and quotes. Copy something that you know works already and change it to do what you want.
- Be careful of quotes. Make sure that you remember both the start and end quotes. If you have quotes inside quotes, they must be of different types (i.e. single and double quotes) although it doesn't matter which comes first.
- Alert boxes are useful for debugging a program (trying to find out why it doesn't work). You can put one in at any point in the program to print out anything you want, to see if it is what it should be. In extreme cases, you can find out if you even get to that point of the program!
- Define all your variables, and make them global unless they are strictly temporary.
- Lay out your code to make it easy to read. Use spaces, blank lines and new lines so the underlying logic is plain, and the blocks of code easy to distinguish.
- Use comments especially at the start of functions, to explain what they do. Even if you've used a sensible function name, and even though you are convinced that the function is obvious, you'll probably forget all about it quite quickly, and will be thankfully for any hint you've left yourself. You might want to change the code some time in the future, for example. I've had to rewrite whole webpages because I couldn't figure out what I'd done in an old one!
- Be careful of infinite loops. Make sure the conditions of your loops will terminate. Don't forget to increment your counters!
- Have a rest if your mind is getting fogged up. It's well known that computers give off magical rays which inhibit thinking! Well, perhaps not. But it's amazing what walking away from the computer for a bit will do. And there's something called fresh air...
- Keep It Simple, Stupid (otherwise known as the KISS principle). If there is a simple way of doing things, and a complicated way of doing things, choose the simple way. Why make life harder than it is already?
© Jo Edkins 2005