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?

closed as not constructive by user40980, BЈовић, EL Yusubov, Jalayn, Walter May 10 '13 at 13:51

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 3 '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 3 '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 3 '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, 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 3 '11 at 22:06

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.

  • 7
    It's called Test Driven Development. – CaffGeek Aug 3 '11 at 16:58
  • @Chad - so it is. Added a link to TDD. – kevin cline Aug 3 '11 at 21:31

Here are a fiew options to eliminate annoying and common coding mistakes:

  1. 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.
  2. 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.
  3. 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.
  4. Desk check. This is an old school technique; print your code and read it. Mark errors (with a pen or pencil) then fix them.
  • +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 3 '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 3 '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.

  • 1
    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 3 '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 3 '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.

  • not so practical ATM. My only other collogue programmer works from home. Still nice idea – Tom Squires Aug 3 '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:

  1. Take the first item on the checklist -- say, missing semicolons.
  2. Go through your code, and inspect for that item -- and that item only.
  3. 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.