Looping only once



  • As a QA Engineer that does a lot of programming I understand that sometimes bugs that seem simple on the frontend are actually more complicated on the backend. Thus when I end up finding stupid bugs in our application I tend to err on the side of caution and hope (for sanity sake) that the people working for us actually have some concept of how to program (since you know, it's their job and they've been doing it for much longer than me). It's also rare that I get a chance to see the offending code that caused a bug.

    One day I was testing a feature, that (in a nutshell) would go through rows in a datagrid in our application and sum up all the values in column B if Column A was a specific value. Sounds easy right. Unfortunately, the system was only adding up the value of the first matching row it found and none other. I end up marking the test as "Testing Unsuccessful" and sending it back to the developer with a reason why. Our bug system emailed both of us with the status update.

    Next I get an email (I was cc'ed) sent by the developer to the lead developer, claiming that he looked at the code, could not figure out why it was performing this way and needed help finding the problem. The lead developer than responded back with the code snippet that the first developer had written. After seeing the code snippet, I manually went into TFS and verified that the function did not exist before the first developer wrote this code, so he was solely responsible for the code snippet. Sorry for the formatting, I don't know how to do code in these forums.

    for (int x = 0; x < foundRows.count; x++)
    {
    totalCount += foundRows[x]["Value"];
    break;
    }



  • @KallDrexx said:

    As a QA Engineer that does a lot of programming I understand that sometimes bugs that seem simple on the frontend are actually more complicated on the backend. Thus when I end up finding stupid bugs in our application I tend to err on the side of caution and hope (for sanity sake) that the people working for us actually have some concept of how to program (since you know, it's their job and they've been doing it for much longer than me). It's also rare that I get a chance to see the offending code that caused a bug.



    One day I was testing a feature, that (in a nutshell) would go through rows in a datagrid in our application and sum up all the values in column B if Column A was a specific value. Sounds easy right. Unfortunately, the system was only adding up the value of the first matching row it found and none other. I end up marking the test as "Testing Unsuccessful" and sending it back to the developer with a reason why. Our bug system emailed both of us with the status update.



    Next I get an email (I was cc'ed) sent by the developer to the lead developer, claiming that he looked at the code, could not figure out why it was performing this way and needed help finding the problem. The lead developer than responded back with the code snippet that the first developer had written. After seeing the code snippet, I manually went into TFS and verified that the function did not exist before the first developer wrote this code, so he was solely responsible for the code snippet. Sorry for the formatting, I don't know how to do code in these forums.



    for (int x = 0; x

    It looks like you're writing codez.

    Would you like help?

    • Plz send me teh codez
    • I'm too leet to seek help from a paperclip

  • Discourse touched me in a no-no place

    Code for those wondering:

     

    for (int x = 0; x < foundRows.count; x++)
    {
    totalCount += foundRows[x]["Value"];
    break;
    }



  • woops my bad.


    Thanks




    edit wow that came out horribly formatted.



  • I propose a new C++ rule: unreachable increment step in a for loop is an error.



  • @fluffy777 said:

    I propose a new C++ rule: unreachable increment step in a for loop is an error.

    An error punishable with retroactive abortion.

    Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.



  • @KallDrexx said:

    Sorry for the formatting, I don't know how to do code in these forums.

    You must be one of the mysterious few who doesn't use IE here. I really don't understand how a website called "The Daily WTF: Curious Perversions In Information Technology" can continue to run this bullshit forum software that doesn't include a cross-platform, WYSIWYG post editor.
    
    Anyway, the bullshit forum software lets you inject some of your own html and css into the post. So wrap your post with <pre style="white-space: pre-wrap;">...</pre> and that will take care of it.


  • @savar said:

    You must be one of the mysterious few who doesn't use IE here. I really don't understand how a website called "The Daily WTF: Curious Perversions In Information Technology" can continue to run this bullshit forum software that doesn't include a cross-platform, WYSIWYG post editor.

    WTF are you talking about?  It works fine in Firefox and Safari and always has.



  • [quote user="Renan "C#" Sousa"]Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.[/quote]

    what's wrong with this code? The break is not inside an if

    for (int i = o; i <max; ){
      if (condition1()){
        dosomething();
         i++;
         continue
      }
      if (condition2()){
        dosomethingElse();
        i+=2;
        continue;
      }
      if (condition3()){
        doYetAnotherthing();
        i--;
        continue;
      }
      break;
    }
    

    Except the fact while() loops are better suited for such crap, of course.
    [/QUOTE]



  • @savar said:

    You must be one of the mysterious few who doesn't use IE here. I really don't understand how a website called "The Daily WTF: Curious Perversions In Information Technology" can continue to run this bullshit forum software that doesn't include a cross-platform, WYSIWYG post editor.

    Anyway, the bullshit forum software lets you inject some of your own html and css into the post. So wrap your post with <pre style="white-space: pre-wrap;">...</pre> and that will take care of it.

    
    Ah, I'm using Chrome.  Thanks for the pre tag info.
    
    

  • 🚽 Regular

     [quote user="Renan "C#" Sousa"]

    Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.

    [/quote]

     Which will inevitably produce gems such as:

     for (var i=0; i<max; i++) {

        doSomething();

        if(true) {

            break;

        }

     }



  • @tchize said:

    what's wrong with this code? The break is not inside an if

    That's why <font face="courier new,courier">else</font> was created...

    for (int i = o; i <max; ){
      if (condition1()){
        dosomething();
        i++;
      } else if (condition2()){
        dosomethingElse();
        i+=2;
      } else if (condition3()){
        doYetAnotherthing();
        i--;
      } else break;
    }
    


  • @tchize said:

    what's wrong with this code?

     A missing semicolon on the first "continue"?


  • Discourse touched me in a no-no place

    @morbiuswilters said:

    [quote user="savar"]You must be one of the mysterious few who doesn't use IE here. I really don't understand how a website called "The Daily WTF: Curious Perversions In Information Technology" can continue to run this bullshit forum software that doesn't include a cross-platform, WYSIWYG post editor.

    WTF are you talking about?  It works fine in Firefox and Safari and always has.

    [/quote]

    Totally broken in Chrome (you get the plain interface.)

    And for some reason, it's intermittant in IE where I am at the moment (sometimes works, sometimes doesn't,) though I'm more prone to blaming the sysadmin here for breaking stuff than CS in this instance.



  • @tchize said:

    [quote user="Renan "C#" Sousa"]Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.

    what's wrong with this code? The break is not inside an if

    for (int i = o; i <max; ){
    [/quote]
    • Inconsistent formatting - space before <, but no space after it. Also, depending on the style, the space before the close paren when there isn't one after the open paren might count (but it could be a space after the semi.)
    • Ugly style: no space between the close paren and the open curly.

    @tchize said:

      if (condition1()){

    • poorly named subroutine.  It should indicate something about the condition it evaluates, other than the fact that it's the first one.
    • subroutine apparently relies entirely on global state.

    @tchize said:

        dosomething();

    • I think this one was actually listed on the main site quite some time ago.  While 'dosomething()' may seem like an ok name for an example, you may reconsider when you encounter production code which uses that exact literal name.  (If it hasn't been listed on the main site, then I really should submit it, because I've had to fix several apps which had names like that for all of their subroutines and functions.  No, that's not what I was told to fix, that was just an obstacle in my path, which I did not leave for the next dupe.)

    @tchize said:

         i++;

    @tchize said:

         continue

    • Missing semi, as already mentioned.

    @tchize said:

      }
    if (condition2()){

    • Missing else.  This suggests there's a way to fall from the first if to the second, but there isn't.

    @tchize said:

        dosomethingElse();

    • As if camel-casing wasn't bad enough, we have partial camel-casing.  Sigh.

    @tchize said:

        i+=2;

    • Inconsistency: in the for statement above, there were spaces around the assignment.  Here, there's no spaces.
    • Bad style: no spaces around the assignment operator.

    @tchize said:

        continue;
    }
    if (condition3()){
    doYetAnotherthing();
    i--;
    continue;
    }
    break;
    }

    Adding up all the issues, including the duplicates I didn't point out, that's 22 issues, and that's not counting all of the useless continue statements.  25 issues for 18 lines of code is pretty impressive.  Congrats.



  • @tchize said:

    [quote user="Renan "C#" Sousa"]Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.

    what's wrong with this code? The break is not inside an if

     [/QUOTE][/quote] Then "what's wrong with it" is just that it's not an example of a "break step that is always executed".  Hence, as you correctly go on to observe, it does indeed not mean that "the loop will always break in the first iteration".



  • @PJH said:

    Totally broken in Chrome (you get the plain interface.)

    Since the forum pre-dates Chrome, that's not very surprising.  Rich text editing in browsers has always been a WTF and each browser tends to have its own take on how RTE should be implemented, with the result that the editor has to be patched to support new browsers.  And since the free version of Community Server was discontinued we're going to be stuck with the editor we have for awhile.  Blame Microsoft and Netscape/Mozilla for making a mess of web standards, blame Google and sundry purveyors of Web 2.0 for trying to make a document and media retrieval system a basis for an applications stack, blame Google for releasing their own crappile into the already bloated and Balkanized browser market.  Or I guess just blame Community Server and Alex since that's what most people do anyway.

     

    @PJH said:

    And for some reason, it's intermittant in IE where I am at the moment (sometimes works, sometimes doesn't,) though I'm more prone to blaming the sysadmin here for breaking stuff than CS in this instance.

    That probably is Community Server's fault.  It really is a shit piece of software and it utilizes TinyMCE poorly.  You're a sharp chap and I figure you must be quick with the mouse when replying; most likely you are trying to click something before all of the JS loads which has the unfortunate effect of killing scriping on the page.  Try waiting several seconds until everything has finished loading before proceeding to edit.



  • @DaveK said:

    @tchize said:

    [quote user="Renan "C#" Sousa"]Actually, any break step that is always executed should be an error. Written as they are, it means the loop will always break in the first iteration, defeating the purpose of putting code in a loop. Break steps should always be inside the scope of "if" statements.

    what's wrong with this code? The break is not inside an if

    Then "what's wrong with it" is just that it's not an example of a "break step that is always executed".  Hence, as you correctly go on to observe, it does indeed not mean that "the loop will always break in the first iteration".[/quote]

    I take issue with your concept of "what's wrong with it".  Yes, it's a lousy example of a counter-example.  But I'm not going to fault tchize's code for not being fundamentally crippled.



  • @morbiuswilters said:

    Blame Microsoft and Netscape/Mozilla for making a mess of web standards, blame Google and sundry purveyors of Web 2.0 for trying to make a document and media retrieval system a basis for an applications stack, blame Google for releasing their own crappile into the already bloated and Balkanized browser market.  Or I guess just blame Community Server and Alex since that's what most people do anyway.
     

    lol, yo.



  • @morbiuswilters said:

    @PJH said:

    Totally broken in Chrome (you get the plain interface.)

    Since the forum pre-dates Chrome, that's not very surprising

    But it doesn't predate WebKit, does it?



  • @toth said:

    But it doesn't predate WebKit, does it?
     

    But I'm fairly certain it predates Safari on Windows, the only other meaningful browser using (a form of) Webkit.

    PS.
    Safari on Windows is a little poor. I'm not sure why people use it, even on a Mac. It has the same lack of features as Chrome, but without the smooth interface style (except on the Mac, where everything has a smooth interface style). Porting to the Windows shell was, as usual, done without much attention for detail and no love at all.



  • @dhromed said:

    @toth said:

    But it doesn't predate WebKit, does it?
     

    But I'm fairly certain it predates Safari on Windows, the only other meaningful browser using (a form of) Webkit.

    Really? Huh, live and learn.

    And yeah, Chrome is very nice.


Log in to reply