Subscribe to
Posts
Comments
NSLog(); Header Image

QotD: Stupid Programming

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.

15 Responses to "QotD: Stupid Programming"

  1. 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.

  2. It's PHP, Tim.

  3. Don't worry Erik, it happens to the best of us. Most of the problems I have when programming are simple mistakes.

  4. 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.

  5. Okay, no fair changing the code sample. 😛

    Now that I see it, the real problem was far more subtle and insidious.

  6. 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 😉

  7. Oh, I think I misread the explanation. The ; _was_ the error then I presume?

  8. 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?

  9. 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.

  10. OK, I must be really dense (and I consider myself a fairly advanced PHP programmer! HA!). What IS the error, other than the semicolon?

  11. What do you mean "other than the semicolon"?

  12. Plus, you really want to write

    $cnt = count($dealers);

    for ($j = 0; $j < $cnt;$j++) {

    }

    Otherwise count() gets called on every iteration.

  13. 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 😉

  14. 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.

  15. 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.


Comments RSS

Leave a Reply


Warning: Undefined variable $user_ID in /var/www/vhosts/nslog.com/httpsdocs/wp-content/themes/nslog/comments.php on line 96

Please abide by the comment policy. Valid HTML includes: <blockquote><p>, <em>, <strong>, <ul>, <ol>, and <a href>. Please use the "Quote Me" functionality to quote comments.