Evil bugs in your code

From time to time I write some code snippets, which I find really hard to debug, and the code does not do, what it is expected to do. Especially when the code is not tested directly, but only executed in some a bit more complex environment.

Those are 4 typical errors I introduced in my code, and spend some time debugging it, because I found them really hard to spot. Luckily, once I spotted the actual bug, I find it a lot easier the next time the typo occurs. Therefore I want to share those, so that I may save you some minutes of your life hunting stupid bugs.

The non working for loop

Know some more?

Do you know more hard to detect trivial code bugs, you may spare me some minutes, if it occurs for me?

Comments

Comments are closed. This blog only exists so that all articles can still be referenced. There is no relevant activity any more on this blog. Since spammers still also find this blog comments are shut down entirely.

s0enkeat Mon, 03 Dec 2007 21:11:56 +0100

Not exactly common, but I had a big if/elseif/elseif/... chain once. I converted the final 'elseif' to an 'else', and spent half an hour trying to figure out why "else ($var > 1)" was a syntax error. I kept reading it "else $var > 1... that's true... so why is the compiler complaining?"

Mostly, my problems anymore are missing/extra semicolons or parens, which the compiler picks up on before things get too out of hand.

I agree, that's an easy way to prevent that problem. However, you have to be in the habit of writing your code that way. I try to often, but frequently forget because I have been doing it the other way since I was taught it in grade school and all through my schooling.

The other listed common mistakes are easy to prevent too if you follow best practices, but they are also easy to make.

My most common mistakes are missing syntax characters, most often the closing parens for if/else/etc. statements, usually when the case is just a function call (isset(), empty(), etc.). Its easy to catch, but still a delay.