I make a fair amount of small mistakes when I code (things such as getting an angle bracket to the wrong direction). It adds a fair amount of time to my coding because I have to debug several times and iron out the dinks. Have you got any advice on how to stop these mistakes creeping in?
5 Answers
The best method I have found is to follow test-driven development, and just write or change a small amount of code before running the tests. Then if there is a failure, you only have to analyze the small amount of new code. This means that every interesting method needs a test.
- 33,798
Here are a fiew options to eliminate annoying and common coding mistakes:
- Pay attention to details. The ability to pay attention to details is a basic human skill. Practice it and you will get better at it. Paying attention to details will lower your number of silly (we all make them) mistakes.
- Slow Down. Alot of mistakes happen because programmers are in a rush to sling code as fast as they can. Slow down and you will make fewer careless mistakes.
- Use unit tests. Write unit tests for all public and protected methods (member functions for the C++ crowd). There are many unit testing libraries, learn one and use it. I like JUnit, but that's just me.
- Desk check. This is an old school technique; print your code and read it. Mark errors (with a pen or pencil) then fix them.
- 672
Some editors and IDEs will auto-complete lots of small things for you, such as adding in braces, brackets, parentheses, as well as partial code structures for loops, etc... Yours might have that option and it might help to learn how to use it and take advantage of it.
- 46,335
Pair Programming is great for catching small mistakes. The downside is that it requires a second developer. The upside is that you get great code quality.
When my company did some experiments with pair programming, my partner and I would often get code that worked correctly the very first time it was run.
- 1,300
The Personal Software Process, from the same folks who created the CMMI, has a methodology that I find useful in ferreting out the kinds of coding mistakes that you are looking to eliminate.
The PSP provides a checklist of common coding mistakes -- missing semicolons, unbalanced parentheses, and the like. The PSP mandates that you perform this check of your code before you even compile:
- Take the first item on the checklist -- say, missing semicolons.
- Go through your code, and inspect for that item -- and that item only.
- Repeat for each item on the checklist.
The key is to only look for one type of coding error at a time. Narrowing your focus leads to much more effective detection.
The other key point is to customize the checklist over time to include the kinds of mistakes that you are most likely to make.
- 1,333