//Comment This Out

Sunday, February 27, 2005

Poorly Written Code

Last Friday, at work, I was looking at some of the code in our codebase and found the following: for ( ; ; ) { // do something if (some condition) break; // do something } Yes, someone wrote a for loop, without any arguments! What really bothers me is that this is something most first year CompSci students wouldn't do, or at the very least, taught not to do this. A few days before that I also came across this type of code: if (A) methodA(); else if (B) if (!C) methodA(); Not as bad as the above code, but still very confusing. I rewrote it as the following: if (A || (B && !C)) methodA();

4 Comments:

  • for without arguments is clear enough, though more conventionally written as while(true). Sometimes it's even the clearest way to code a loop.

    That if monstrosity is pretty inexcusable, though!

    By Blogger Zman Biur, at 1:21 PM  

  • The issue for me wasn't that it was unclear what the code did. It was quite clear what it did.
    It was the idea of using for ( ; ; ) in any circumstance. I would have been happier (slightly) if the coder had written while (true).
    Even in that case, I do not like having unconditional looping (or rather, unconditional at the beginning of the loop, if they use break somewhere in the middle). It sort of defeats the purpose of having a conditional there to begin with.

    By Blogger Avrom, at 7:19 PM  

  • How about this:

    while(fork()){
    //do nothing
    }

    :)

    By Anonymous Anonymous, at 4:22 PM  

  • Yes, I had a professor in university who told us about that one. He said it was the perfect way to crash a Unix based OS and completely annoy the sysadmin and incur the wrath of anyone else trying to use the system at the time.

    By Blogger Avrom, at 10:11 PM  

Post a Comment

<< Home