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?
-
If your example of getting the angle bracket in the wrong direction was within a test of inequality then one good way to avoid or minimise that is to standardise the way that you test for inequalities. I think I got this from Code Complete, that you always test consistently. I test left to right, lowest to highest. This gives you the benefit of a consistent approach, easier to read code and helps mistakes stand out. Of course you could be using angle brackets to do other things, like opening closing tags, and in that case this comment is useless. – AlexC Aug 03 '11 at 20:39
-
@Job "I use visual studio and that picks up the code that is wrong. Im talking about things that are incorrect but still compile" – Tom Squires Aug 03 '11 at 21:47
-
2@Job > Or eclipse, or netbeans, or vim, or emacs, or many others. Are you some kind of lobbist or fanboy ? – deadalnix Aug 03 '11 at 21:49
-
Make sure you switched on all warnings (except for those where you're sure you don't need them). Use a tool like [FindBugs](http://en.wikipedia.org/wiki/FindBugs), this is what it's for. While FindBugs is only one of such free tools for Java, I don't know if there's anything for VS. – maaartinus Aug 03 '11 at 22:06
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,608
- 3
- 71
- 142
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
- 3
- 7
-
+1 for all. I would only add that a good IDE that you take the time to 100% learn helps a lot also. – Ominus Aug 03 '11 at 17:09
-
#3 will eliminate a lot of those mistakes right from the beginning - should be #1 in my opinion. TDD principles have increased my code reliability by 1000%. – IAbstract Aug 03 '11 at 20:51
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,105
- 7
- 126
- 176
-
1I use visual studio and that picks up the code that is wrong. Im talking about things that are incorrect but still compile. – Tom Squires Aug 03 '11 at 16:20
-
3@Tom Squires, An you mean typing a "Less-than" when you meant a "Greater-than"? In that case I recommend paying closer attention, and to do that, I recommend more and better sleep! ;) – FrustratedWithFormsDesigner Aug 03 '11 at 16:24
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
- 11
- 14
-
not so practical ATM. My only other collogue programmer works from home. Still nice idea – Tom Squires Aug 03 '11 at 20:32
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
- 8
- 7