Evil bugs in your code - Kore Nordmann

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 missing 'if'

( $test->check( $foo ) ) { // Why is this always executed? }

An additional ;

while ( $line = fread( $fp, 1024 ) ); { $text .= $line; } // Damn, why is $text still empty?

Unexpected operator precendence

// WTH isn't this a valid query? $nodeList = $xpath->query( '//*[@attribute="row_' . $row + 1 . '"]' );

The non working for loop

// Parse error: Unexpected ; in line 23 foreach ( $i = 0; $i < 10; ++$i ) { // Do something }

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