Comment On Not Trusting a For-Loop?

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? [expand full text]
« PrevPage 1 | Page 2Next »

Re: Not Trusting a For-Loop?

2005-11-09 14:49 • by Mutt Rockne
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?


function headerInit()
{
var 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;
if (intCount != 0)
{
document.getElementById('btnChkRslts').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.

Re: Not Trusting a For-Loop?

2005-11-09 14:49 • by ToxicFrog
Any brave "code defenders" want to take a stab at this?

...I got nothing.

Re: Not Trusting a For-Loop?

2005-11-09 14:52 • by mrsticks1982
They just wanted to prove that they COULD count to 15 ....

Re: Not Trusting a For-Loop?

2005-11-09 14:53 • by BradC
50134 in reply to 50131

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

Re: Not Trusting a For-Loop?

2005-11-09 14:53 • by WTFer
Alex Papadimoulis:


function headerInit()


Isn't it obvious? It initializes the header.

Re: Not Trusting a For-Loop?

2005-11-09 14:54 • by Anon
Someone who didn't quite understand Duff's Device?

Re: Not Trusting a For-Loop?

2005-11-09 14:54 • by Viflux
Obviously someone gets paid by the line.

Re: Not Trusting a For-Loop?

2005-11-09 14:56 • by Anonymous Coward
50139 in reply to 50132
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:
btnChkRslts

Damn vowels!




Re: Not Trusting a For-Loop?

2005-11-09 14:56 • by Monday
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.

Re: Not Trusting a For-Loop?

2005-11-09 14:58 • by Hexar
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?


function headerInit()

{
var 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;
if (intCount != 0)
{
document.getElementById('btnChkRslts').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:


function headerInit()
{
  for (intCount = 0; intCount <= 15; intCount++)
     ;


  if (intCount != 0)
  {
    document.getElementById('btnChkRslts').click();
  }
}


Although not quite as fast as:


function headerInit()
{
  
document.getElementById('btnChkRslts').click();
}

Re: Not Trusting a For-Loop?

2005-11-09 15:02 • by AlbertEin
They must be stupid!



They should know that this is the way to go:

function headerInit()
{
var 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;
if (intCount != 0)
{
document.getElementById('btnChkRslts').click();
}
}


Re: Not Trusting a For-Loop?

2005-11-09 15:02 • by arty
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.

Re: Not Trusting a For-Loop?

2005-11-09 15:09 • by Eddie
50147 in reply to 50145
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.

Re: Not Trusting a For-Loop?

2005-11-09 15:14 • by Tool

I don't see a WTF here.


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

Re: Not Trusting a For-Loop?

2005-11-09 15:15 • by 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.  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:

function headerInit()
{
var intCount=0;
if (intCount != 0)
{
document.getElementById('btnChkRslts').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.

Re: Not Trusting a For-Loop?

2005-11-09 15:20 • by JohnO
I think this code could only have been written by nazis without CS degrees.

Re: Not Trusting a For-Loop?

2005-11-09 15:22 • by Dave
50153 in reply to 50151
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'

Re: Not Trusting a For-Loop?

2005-11-09 15:23 • by frosty
50154 in reply to 50151
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)

Re: Not Trusting a For-Loop?

2005-11-09 15:24 • by rogthefrog
 Mine goes to 11.

Re: Not Trusting a For-Loop?

2005-11-09 15:26 • by Manni

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

Re: Not Trusting a For-Loop?

2005-11-09 15:28 • by Sean
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.

Re: Not Trusting a For-Loop?

2005-11-09 15:31 • by Manni
50158 in reply to 50151

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.

Re: Not Trusting a For-Loop?

2005-11-09 15:42 • by WTFer
50159 in reply to 50155
rogthefrog:
 Mine goes to 11.


Best thread ever.

Re: Not Trusting a For-Loop?

2005-11-09 15:47 • by ItsAllGeekToMe
50161 in reply to 50158

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.

Re: Not Trusting a For-Loop?

2005-11-09 15:50 • by Freak_B
50162 in reply to 50158

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.


 

Re: Not Trusting a For-Loop?

2005-11-09 15:53 • by bookem
50163 in reply to 50144
isTrue(headerInit()) ? 42 : fileNotFound;

Re: Not Trusting a For-Loop?

2005-11-09 16:04 • by Damien
Looks like it might have been some code that leaked out of a code generator.

Re: Not Trusting a For-Loop?

2005-11-09 16:09 • by diaphanein
50166 in reply to 50165
Its obvious, of course, that this is done for thread safety. 

Re: Not Trusting a For-Loop?

2005-11-09 16:09 • by pink code
50167 in reply to 50152
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...


Re: Not Trusting a For-Loop?

2005-11-09 16:19 • by HAK
*blink blink*

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

Re: Not Trusting a For-Loop?

2005-11-09 16:27 • by Wire
50171 in reply to 50152
JohnO:
I think this code could only have been written by nazis without CS degrees.


Sincerely,

Adolph Eichmann
I invented Gene Nixon.

Re: Not Trusting a For-Loop?

2005-11-09 16:38 • by steve
The source code could have been (in a jsp).
...

function
headerInit()
{
var intCount=0;

intCount=${count};

if (intCount != 0)
{
document.getElementById('btnChkRslts').click();
}
}
...
Then it would have been only mildly wierd to have not used a boolean.

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

steve

Re: Not Trusting a For-Loop?

2005-11-09 16:41 • by Martin
Maybe the html is generated, and some code is missing...

Re: Not Trusting a For-Loop?

2005-11-09 16:43 • by MikeB
50174 in reply to 50162

Anonymous:


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



I agree. This dude has got skills.

Re: Not Trusting a For-Loop?

2005-11-09 16:47 • by steve
50176 in reply to 50131
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)... .

Re: Not Trusting a For-Loop?

2005-11-09 16:58 • by murphyman
The real WTF is that they should have used JavaScript.



Oh.

Re: Not Trusting a For-Loop?

2005-11-09 17:29 • by Malhar

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

Re: Not Trusting a For-Loop?

2005-11-09 18:11 • by Savior
50184 in reply to 50174
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

Re: Not Trusting a For-Loop?

2005-11-09 20:51 • by Simon H
Originally it was:

function
headerInit()
{
var 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;
if (intCount != 0)
{
document.getElementById('btnChkRslts').click();
}
}

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

Re: Not Trusting a For-Loop?

2005-11-09 23:36 • by danhash
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.

Re: Not Trusting a For-Loop?

2005-11-09 23:43 • by Evan Charlton
50198 in reply to 50161
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....

Re: Not Trusting a For-Loop?

2005-11-09 23:55 • by vhawk
Huh ?????   Nope - you have me here ....

Re: Not Trusting a For-Loop?

2005-11-10 00:53 • by Jon Limjap
50201 in reply to 50200
Maybe the guy was paid "per line of code"?

Re: Not Trusting a For-Loop?

2005-11-10 01:40 • by mrQQ
50202 in reply to 50151
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:

function headerInit()
{
var intCount=0;
if (intCount != 0)
{
document.getElementById('btnChkRslts').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*         

Re: Not Trusting a For-Loop?

2005-11-10 02:18 • by Madge O'Reene
50203 in reply to 50141
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]

Re: Not Trusting a For-Loop?

2005-11-10 03:06 • by Dave Sag
50205 in reply to 50135
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?

Re: Not Trusting a For-Loop?

2005-11-10 03:40 • by Anonymous coward
Paula Bean is back.  Brilliant!!

Re: Not Trusting a For-Loop?

2005-11-10 04:51 • by RiX0R
50207 in reply to 50205

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('btnChkRslts').checked =  true;

Re: Not Trusting a For-Loop?

2005-11-10 05:27 • by erlando
50210 in reply to 50207
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('btnChkRslts').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..

Seen something like that

2005-11-10 05:41 • by kahnman
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.



« PrevPage 1 | Page 2Next »

Add Comment