• Mutt Rockne (unregistered)
    Alex Papadimoulis:

    Matthias L was browsing through some code from a (publicly available) web application that his company developed and came across this rather curious bit of code. It's a JavaScript function that runs in the HTML document's OnLoad() event. Matthias stared at it for a little while, and its existence remained a complete mystery. He sent it to me, so I stared at it for a little while. I've got nothing. Any brave "code defenders" want to take a stab at this?

    <font color="#000099">function</font> headerInit()
    {
    <font color="#000099">var</font> intCount=0;
    intCount=1;
    intCount=2;
    intCount=3;
    intCount=4;
    intCount=5;
    intCount=6;
    intCount=7;
    intCount=8;
    intCount=9;
    intCount=10;
    intCount=11;
    intCount=12;
    intCount=13;
    intCount=14;
    intCount=15;
    <font color="#000099">if</font> (intCount != 0)
    {
    document.getElementById(<font color="#990000">'btnChkRslts'</font>).click();
    }
    }

    Also, I wanted to thank Mike for bringing back the Hotel Reservation System from Hell while I was at the VS.NET Launch Event yesterday. If you're curious, I wrote about my experience. It was good overall, much in part because it was the first time I've ever seen such miniature jars of honey.



    Its just there to slow down the browser.... don't want it to go TOO fast.
  • ToxicFrog (unregistered)

    Any brave "code defenders" want to take a stab at this?

    ...I got nothing.

  • (cs)

    They just wanted to prove that they COULD count to 15 ....

  • (cs) in reply to Mutt Rockne

    Anonymous:

    Its just there to slow down the browser.... don't want it to go TOO fast.

    I actually think this is why it was created... some misguided attempt to put a slight pause before the Click event is triggered.

    Weird

  • (cs)
    Alex Papadimoulis:

    <font color="#000099">function</font> headerInit()

    Isn't it obvious? It initializes the header.
  • Anon (unregistered)

    Someone who didn't quite understand Duff's Device?

  • (cs)

    Obviously someone gets paid by the line.

  • Anonymous Coward (unregistered) in reply to ToxicFrog

    THIRD!
    The coder was just kidding. Brillant!
    The coder should have used IsTrue() or a ternary operator.

    The name of the button is a nice touch:

    <font color="#990000">btnChkRslts

    Damn vowels!
    </font>


  • Monday (unregistered)

    Classic obsessive compulsive syndrome. You know those people that have to lock the door 3 times before it's properly locked? Same thing...intCount has to be not zero 15 times in a row.

  • Hexar (unregistered)
    Alex Papadimoulis:

    Matthias L was browsing through some code from a (publicly available) web application that his company developed and came across this rather curious bit of code. It's a JavaScript function that runs in the HTML document's OnLoad() event. Matthias stared at it for a little while, and its existence remained a complete mystery. He sent it to me, so I stared at it for a little while. I've got nothing. Any brave "code defenders" want to take a stab at this?

    <FONT color=#000099>function</FONT> headerInit()
    {
      <FONT color=#000099>var</FONT> intCount=0;
      intCount=1;
      intCount=2;
      intCount=3;
      intCount=4;
      intCount=5;
      intCount=6;
      intCount=7;
      intCount=8;
      intCount=9;
      intCount=10;
      intCount=11;
      intCount=12;
      intCount=13;
      intCount=14;
      intCount=15;
      <FONT color=#000099>if</FONT> (intCount != 0) 
      {
        document.getElementById(<FONT color=#990000>'btnChkRslts'</FONT>).click();
      }
    }

    Also, I wanted to thank Mike for bringing back the Hotel Reservation System from Hell while I was at the VS.NET Launch Event yesterday. If you're curious, I wrote about my experience. It was good overall, much in part because it was the first time I've ever seen such miniature jars of honey.

    This is obviously for performance--they are simply unrolling the loop.  Way faster than this:

    <FONT face="Courier New"><FONT size=2><FONT color=#000099>function</FONT> headerInit()
    {
      for (intCount = 0; intCount <= 15; intCount++)
         ;</FONT></FONT>

    <FONT size=+0><FONT size=2><FONT face="Courier New">  <FONT color=#000099>if</FONT> (intCount != 0)
      {
        document.getElementById(<FONT color=#990000>'btnChkRslts'</FONT>).click();
      }
    }</FONT></FONT></FONT>

    Although not quite as fast as:

    <FONT face="Courier New"><FONT size=2><FONT color=#000099>function</FONT> headerInit()
    {
       </FONT></FONT><FONT size=+0><FONT size=2><FONT face="Courier New">document.getElementById(<FONT color=#990000>'btnChkRslts'</FONT>).click();
    }</FONT></FONT></FONT>

  • AlbertEin (unregistered)

    They must be stupid!

    They should know that this is the way to go:

    <font>function</font> headerInit()
    {
    <font>var</font> intCount=0;
    var i;
    for (i = 0; i < 1; i++)
    for (i = 1; i < 2; i++)
    for (i = 2; i < 3; i++)
    for (i = 3; i < 4; i++)
    etc, etc, etc.
    intCount = i;
    <font>if</font> (intCount != 0)
    {
    document.getElementById(<font>'btnChkRslts'</font>).click();
    }
    }

  • arty (unregistered)

    I'd be almost certain that this works around a problem in a deranged browser that doesn't have the whole document loaded before calling the onload handler.

  • Eddie (unregistered) in reply to arty

    Arty, I was thinking the same thing. But that makes it even funnier. They should have just used the for loop and cut out the if statement, were that the situation.

  • Tool (unregistered)

    I don't see a WTF here.

    He's just making *extra* sure that the variable is initialized before it is accessed.

  • Jesse (unregistered)

    Actually I think I have a semi-plausible explanation for this.  It's not handwritten code; it was generated by a JSP or some other preprocessing layer that generates the page we're seeing.  The loop is not a JavaScript loop, it's a JSP loop and hence the loop code is invisible at this stage.  The 'if' check at the end makes sense because the JSP loop could have run zero (or possily one) times resulting in this generated JavaScript code:

    <font>function</font> headerInit()
    {
    <font>var</font> intCount=0;
    <font>if</font> (intCount != 0)
    {
    document.getElementById(<font>'btnChkRslts'</font>).click();
    }
    }

    We have no way to know what the JSP loop was supposed to do because we can't see the code, but perhaps it was running through a list of items that correspond to checkboxes and adding something to the header for each item whose checkbox would be checked.  The input data in this case didn't have any items which would result in a checked checkbox.

  • (cs)

    I think this code could only have been written by nazis without CS degrees.

  • Dave (unregistered) in reply to Jesse

    I bet you just hit the nail on the head. He's got server-side processing generating a bit of JavaScript (I've done this myself), and instead of doing a server-side check to see if the loop ran more than zero times, he just outputs the loop counter every time and lets the JavaScript check.

    Lazy, but understandable. Huh.

    captcha: 'dinky'

  • (cs) in reply to Jesse
    Jesse:
    Actually I think I have a semi-plausible explanation for this.  It's not handwritten code; it was generated by a JSP or some other preprocessing layer that generates the page we're seeing.  The loop is not a JavaScript loop, it's a JSP loop and hence the loop code is invisible at this stage.


    This makes sense.  Were there, by any chance, 15 of something? (rows, columns, emoticons, etc)
  • (cs)

     Mine goes to 11.

  • (cs)

    On a serious note, I know exactly what's going on here.

    This is a PHP or ASP file, and the server side is doing some kind of validation. For every step, it increments a numerical variable and spits it out into Javascript. That way, when it gets to the end of whatever it's processing, as long as one of the situations was met and intCount was incremented, it should perform the click event.

    Like if someone was filling in some user info, and you wanted to make sure they filled in at least one field. If "First Name" is filled in, increment by one. If "Last Name" is filled in...and so on. As long as one has a value, activate the click() event on that button.

    Granted this is an utterly retarded way of handling the situation...

  • (cs)

    It could be that some compilers/interpreters may think he wanted intCount to be zero.  By setting the variable to a non-zero number 15 times in a row, it knows he's serious, that he knows what he's doing, and leaves it alone.  I've seen it a million times.

  • (cs) in reply to Jesse

    Jesse:

    My answer..dammit

    Good job Jesse. I got it a few minutes too late. And now for your prize: the trampoline chicks from The Man Show are here with vats of butter and a half-dozen lemmings. You control the action, my friend.

  • (cs) in reply to rogthefrog
    rogthefrog:
     Mine goes to 11.

    Best thread ever.
  • (cs) in reply to Manni

    how literal was Alex when he said "Html document"?  I would tend to agree with the ASP theory.......but if it's an index.html then obviously there's no asp involved.  Unless something magical happens that I don't know about.

  • Freak_B (unregistered) in reply to Manni

    Guys, i am really disapointed that you do not understand the brilance of this developer.

    We all know that you never know the OS on which the client side javascript code will run. What kind of systems people run these days... heard something about a burning fox recently.... [;)]

    Now lets say you got a system that use a int definition that has a maxvalue of 14 then it is very likely that the number 15 will be interpreted as minvalue. This could evenwell be a unsinged int resulting in value 0. You would really not want to go checking results with button actions on those systems... would you? counting to 15 for this variable is only to generate javascript errors on those systems that do not allow integers to be greater then 0.

     

  • (cs) in reply to AlbertEin

    isTrue(headerInit()) ? 42 : fileNotFound;

  • Damien (unregistered)

    Looks like it might have been some code that leaked out of a code generator.

  • diaphanein (unregistered) in reply to Damien

    Its obvious, of course, that this is done for thread safety. 

  • (cs) in reply to JohnO
    JohnO:
    I think this code could only have been written by nazis without CS degrees.


    ...or possibly nazis above the age of 30 with a purdue CS degree?

    I vote for the "this is server-side generated code that for some reason should ensure that the button is not clicked if no assigment statements have been output. Doesn't take are the WTFery, just a very pausible explanation...


  • (cs)

    blink blink
    I have trouble believeing that a computer would generate this.  On some level this defies all known logic ....

  • (cs) in reply to JohnO
    JohnO:
    I think this code could only have been written by nazis without CS degrees.


    Sincerely,

    Adolph Eichmann
    I invented Gene Nixon.
  • steve (unregistered)

    <font>The source code could have been (in a jsp).
    ...
    <c:set var='count' value='0' />
    function</font> headerInit()
    {
    <font>var</font> intCount=0;
    <c:forEach items='${fields}' val='field' >
    <c:if test='${field.visible}' ><font><c:set var='count' value='${count + 1}' /></font>intCount=${count};</c:if>
    </c:forEach>
    <font>if</font> (intCount != 0)
    {
    document.getElementById(<font>'btnChkRslts'</font>).click();
    }
    }
    ...
    Then it would have been only mildly wierd to have not used a boolean.

    There, I defended it, I'm not proud.

    steve

  • Martin (unregistered)

    Maybe the html is generated, and some code is missing...

  • (cs) in reply to Freak_B

    Anonymous:

    Guys, i am really disapointed that you do not understand the brilance of this developer.

    I agree. This dude has got skills.

  • steve (unregistered) in reply to Mutt Rockne

    Speaking of wtf, the site's editor couldn't escape a less than sign (it just removed the JSP tags sorry about the worthless example)... .

  • (cs)

    The real WTF is that they should have used JavaScript.

    Oh.

  • Malhar (unregistered)

    Getting paid by number of lines!! I wouldn't be surprised if they had done the same for intCount = 1000.

  • (cs) in reply to MikeB
    MikeB:

    Anonymous:

    Guys, i am really disapointed that you do not understand the brilance of this developer.

    I agree. This dude has got skills.

    Correction: m4D 5k1llz

  • Simon H (unregistered)

    <font>Originally it was:

    function</font> headerInit()
    {
    <font style="color: rgb(128, 0, 128);">var</font> intCount=0;
    intCount==1;
    intCount==2;
    intCount==3;
    intCount==4;
    intCount==5;
    intCount==6;
    intCount==7;
    intCount==8;
    intCount==9;
    intCount==10;
    intCount==11;
    intCount==12;
    intCount==13;
    intCount==14;
    intCount==15;
    <font style="color: rgb(128, 0, 128);">if</font> (intCount != 0)
    {
    document.getElementById(<font style="color: rgb(128, 0, 128);">'btnChkRslts'</font>).click();
    }
    }

    The coder gave up trying to set that pesky intCount at 15, and asked his/her supervisor to help debug it.

  • danhash (unregistered)

    i'm thinking maybe it's similar to those stupid CSS "hacks" that are floating around. most browsers would go ahead and "click" the button, but maybe there's a certain browser with a flaw in it's javascript implementation and he was exploiting that flaw to get a specific effect on a specific browser.

  • Evan Charlton (unregistered) in reply to ItsAllGeekToMe
    ItsAllGeekToMe:

    how literal was Alex when he said "Html document"?  I would tend to agree with the ASP theory.......but if it's an index.html then obviously there's no asp involved.  Unless something magical happens that I don't know about.



    I take it you've never heard of setting MIME-types or handlers? index.html could contain PHP code, ASP code, Perl, whatever....
  • vhawk (unregistered)

    Huh ?????   Nope - you have me here ....

  • (cs) in reply to vhawk

    Maybe the guy was paid "per line of code"?

  • (cs) in reply to Jesse
    Anonymous:
    Actually I think I have a semi-plausible explanation for this.  It's not handwritten code; it was generated by a JSP or some other preprocessing layer that generates the page we're seeing.  The loop is not a JavaScript loop, it's a JSP loop and hence the loop code is invisible at this stage.  The 'if' check at the end makes sense because the JSP loop could have run zero (or possily one) times resulting in this generated JavaScript code:

    <FONT size=+0>function</FONT> headerInit()
    {
    <FONT size=+0>var</FONT> intCount=0;
    <FONT size=+0>if</FONT> (intCount != 0)
    {
    document.getElementById(<FONT size=+0>'btnChkRslts'</FONT>).click();
    }
    }


    We have no way to know what the JSP loop was supposed to do because we can't see the code, but perhaps it was running through a list of items that correspond to checkboxes and adding something to the header for each item whose checkbox would be checked.  The input data in this case didn't have any items which would result in a checked checkbox.

    just what i was thinking! *nod*nod*         

  • Madge O'Reene (unregistered) in reply to Hexar
    Anonymous:

    This is obviously for performance--they are simply unrolling the loop. 

     

    LMAO - I'm going to steal that joke and reuse it on some jnr programmers where I work [Y]

  • Dave Sag (unregistered) in reply to WTFer

    What I want to know is why does the code need to simulate a button click as part of the header initialisation. You can explain the rest of it as being generated code, but why the click?

  • Anonymous coward (unregistered)

    Paula Bean is back.  Brilliant!!

  • (cs) in reply to Dave Sag

    Anonymous:
    What I want to know is why does the code need to simulate a button click as part of the header initialisation. You can explain the rest of it as being generated code, but why the click?

    I agree. It would make more sense to just set the value:

    document.getElementById(<FONT color=#990000>'btnChkRslts'</FONT>).checked =  true;

  • erlando (unregistered) in reply to RiX0R
    RiX0R:

    Anonymous:
    What I want to know is why does the code need to simulate a button click as part of the header initialisation. You can explain the rest of it as being generated code, but why the click?

    I agree. It would make more sense to just set the value:

    document.getElementById(<font color="#990000">'btnChkRslts'</font>).checked =  true;



    Why is everyone assuming that this is a checkbox. The prefix "btn" would suggest button to me in which case a click makes sense..
  • (cs)

    Many years ago, I was writing a hard-core Real-Time aviation software for a fighter-jet. We used to pull all sorts of tricks to gain ANY improvement in speed-performance. One day, I found the following snippet of code, in a larger function, not once but three times: Assuming a table of flags that only one of which was ever set to true (there actually was a reasonably good reason to have an array of flags, instead of just saving the index), and a range-checked index x set elsewhere:

            int x;
    boolean VideoSources [16];

    for (int i = 0; i < 1; i++) {
    if (i == x && VideoSources[i]) {
    // Do something
    break;
    }
    }

    I was trying to understand this code, and spent a long time trying to figure out what the bloody catch was. I mean, there just HAD to some reason to write the code this way, right? After consulting three other engineers, I replaced it with:

            if(VideoSources[x]) {
    // Do something
    }

    So much for efficiency, eh? Neat community. I just joined, after seeing yesterday's horror.

Leave a comment on “Not Trusting a For-Loop?”

Log In or post as a guest

Replying to comment #:

« Return to Article