QotD: Stupid Programming
Posted November 30th, 2005 @ 02:52pm by Erik J. Barzeski
Question: Guess how long I stared at this before I found the problem? How long did it take you?
My Answer:
for($j=0; $j<count($dealers); $j++); { if($dealers[$j]['state'] == $state) $dealers_in_state[] = $dealers[$j]; }
The answer (highlight to read): [Took me about 20 minutes. For some reason I had it stuck in my head that $dealers wasn't being loaded correctly because it was from a file I had include('d') in the main file.]
You are encouraged to answer the Question of the Day for yourself in the comments or on your blog.
Posted 30 Nov 2005 at 2:43pm #
Assuming that you didn't already have $i in use for something else, a "use strict;" would have fixed that. (although you would then have to use "my", "our", or "use vars" in order to declare your variables.)
run "perldoc strict" for more info.
Posted 30 Nov 2005 at 2:45pm #
It's PHP, Tim.
Posted 30 Nov 2005 at 2:46pm #
Don't worry Erik, it happens to the best of us. Most of the problems I have when programming are simple mistakes.
Posted 30 Nov 2005 at 2:49pm #
Heh. Took me a minute, too. It helped that you pointed out there was an error, else I might have stared at it for quite a while.
Posted 30 Nov 2005 at 4:00pm #
Okay, no fair changing the code sample. 😛
Now that I see it, the real problem was far more subtle and insidious.
Posted 30 Nov 2005 at 4:47pm #
Heh, how were we to guess how long it would take to see such a subtle error regarding variables from outside the code-example? 😛
You've still got a syntactical error: There's not supposed to be a ; after the for-declaration. But I guess such an error would've been obvious to catch from PHPs error-output.
Debugging is rarely fun 😉
Posted 30 Nov 2005 at 4:49pm #
Oh, I think I misread the explanation. The ; _was_ the error then I presume?
Posted 30 Nov 2005 at 8:16pm #
Well, if the error is the semicolon at the end of the for line, that's one reason to have your opening accolade on the same line as your for statement ex:
for($j=0; $j<count($dealers); $j++) {
if($dealers[$j]['state'] == $state) {
$dealers_in_state[] = $dealers[$j];
}
}
That way, you don't end up putting semicolons where they don't belong...
Oh and another thing, aren't accolades necessary within the if statement too?
Posted 30 Nov 2005 at 9:42pm #
I saw it immediately, but only because I knew there was a problem to be found. I think I've spent more time looking for simpler stuff though.
Posted 30 Nov 2005 at 9:58pm #
OK, I must be really dense (and I consider myself a fairly advanced PHP programmer! HA!). What IS the error, other than the semicolon?
Posted 30 Nov 2005 at 10:56pm #
What do you mean "other than the semicolon"?
Posted 01 Dec 2005 at 1:20am #
Plus, you really want to write
$cnt = count($dealers);
for ($j = 0; $j < $cnt;$j++) {
}
Otherwise count() gets called on every iteration.
Posted 01 Dec 2005 at 2:03pm #
or if you're really bored you can use the following madness in php:
for($j=0; $j<count($dealers); $j++)
if($dealers[$j]['state'] == $state)
$dealers_in_state[] = $dealers[$j];
Although i guess it won't make the mistake any more obvious 😉
Posted 01 Dec 2005 at 11:48pm #
my favorite is putting the ';' on an if statement. loops become much more obvious much more quickly than complex conditionals. As for Patrick's comment, I much prefer seeing it the way Erik did it. It's easier for me to group code together and scan it quickly.
Posted 02 Dec 2005 at 12:12am #
I typically like brackets when something's going to extend over a range of more than one line. Even in my example I use brackets for the
for
loop but not for the conditional.