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?
If you liked this blog post, or learned something please consider using flattr to contribute back: .
Comments
Fields with bold names are mandatory.
s0enke at Mon, 03 Dec 2007 21:11:56 +0100
if ($blah = 1) {
Link to comment//always true, forgot second =
}
sapphirecat at Tue, 04 Dec 2007 14:26:58 +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?"
Link to commentMostly, my problems anymore are missing/extra semicolons or parens, which the compiler picks up on before things get too out of hand.
motzel at Tue, 04 Dec 2007 21:46:02 +0100
@s0enke:
Link to commentIf you reverse operators it will never be problem again. So instead:
if($blah == 1)
always use
if(1 == $blah)
With such a notation, when you accidentaly forgot one of equal sign, compiler report a syntax error.
John J. at Tue, 04 Dec 2007 22:23:27 +0100
@motzel
Link to commentI 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.
Saur at Wed, 05 Dec 2007 07:18:22 +0100
never had that bugs... it's kinda lame bugs.
Link to comment"If you reverse operators it will never be problem again"
that is nice. thx.
open source at Wed, 05 Dec 2007 09:29:39 +0100
I never did any of this bug ;)
Link to commentChris at Thu, 06 Dec 2007 13:41:33 +0100
The last minute variable change :
Link to commentfor ($i = 0; $l < 10; ++$i)
{
// Here we go for the infinite loop
}
Patrice at Thu, 06 Dec 2007 17:38:25 +0100
You place a ; after the while .
Link to comment$i = 0;
while ( $i < 10 );
{
$i++;
//Do Something
}
Fatiha at Fri, 07 Dec 2007 14:08:26 +0100
Put an empty line before :
Link to comment<?
header("file.php");
?>
and you have that error :
Cannot add header information - headers already sent by (output started at [script1]) in [script2] on line [line]
Fatiha
tbela99 at Fri, 07 Dec 2007 18:20:00 +0100
I just had headaches about this one
Link to comment//$a is previously null
//excpecting $a to be an array
if($a = get_some_data($param) && isset($a[1]) {
// the following code is never executed
}
//I should have set $a before testing