Classic ASP WTF



  • Digging through classic ASP applications is always a WTF worthy experience.

     Behold:

        ' Grab Unique Check Numbers from all numbers
        Dim UniqueCheckNum(1000)
        Dim TotalUniqueChecks
        TotalUniqueChecks = 0
       
        for i = 0 to TotalUniqueChecks
            for j = 1 to TotalElem
                if NOT ChapCheck(j) = UniqueCheckNum(i) then
                    UniqueCheckNum(TotalUniqueChecks) = ChapCheck(j)
                    TotalUniqueChecks = TotalUniqueChecks + 1
                end if
            next
        next
     

    This is so wrong, I can't believe it works.

     



  • Looks like you're missing something there ...



  • I know absolutely nothing about asp, but I don't see how in the world it would work. You would think that the for loop would never execute because TotalUniqueChecks is already 0.



  • In VB, the loop structure runs the counter up to and including the end. In short,

    for i = 0 to 10

    is equivalent to

    for (i = 0; i <= 10; i++)

    So the loop for i = 0 to 0 does execute once. Silly VB.



  • @Welbog said:

    In VB, the loop structure runs the counter up to and including the end. In short,

    for i = 0 to 10

    is equivalent to

    for (i = 0; i <= 10; i++)

    So the loop for i = 0 to 0 does execute once. Silly VB.

     

    Thanks for the clarification. I guess the variable TotalElem would have had to be a global then. Otherwise the second for would be equvalent to  for j = 1 to 0



  • It should work (I think) if the for loops are the other way around, but I think you would end up with the wrong answer as it stands...



  • @GettinSadda said:

    It should work (I think) if the for loops are the other way around, but I think you would end up with the wrong answer as it stands...

    The kicker is that it works just fine, as long as you have less than 1000 unique checks.

     I've just never quit seen a for loop abused so badly.


     



  • @Welbog said:

    So the loop for i = 0 to 0 does execute once. Silly VB.

    I don't see this as a language flaw. FOR var = a TO b simply iterates over each number starting at a and finishing with b. "Loop over all numbers starting at 0 and ending at 0" is perfectly fine. Simple (that's why it's called "BASIC" 😉 but fine.



  • @pitchingchris said:

    I know absolutely nothing about asp, but I don't see how in the world it would work. You would think that the for loop would never execute because TotalUniqueChecks is already 0.

    As someone who's spent way too much time around BASIC (starting with my 80s home micro) it makes perfect sense and if you program in BASIC you think nothing of it. FOR permits the start and end numbers to be equal to each other, simply due to the very restrictive nature of the keyword. C's for statement is radically different, which may cause confusion for anyone not used to BASIC, but BASIC isn't wrong.

    For example, if you want to iterate over a list of n items, and the list is permitted to contain a single element (as they at some stage will) then FOR will have to accept matching start and end numbers, else you're stuck. If the list's first element is zero, then your loop will have to go from 0 to 0. It all makes complete sense.



  • this is not a wtf... this is abuse!!

     the for i.. will execute more than 1 because it will check the value of totaluniquechecks every time it reach that line...
     



  • @topcat_arg said:

    this is not a wtf... this is abuse!!

     the for i.. will execute more than 1 because it will check the value of totaluniquechecks every time it reach that line...
     

    Well... unless ChapCheck(j) = UniqueCheckNum(i)



  • Also, being that it increases the totaluniquechecks inside the nested loop, that is going to get ugly.



  • @topcat_arg said:

    this is not a wtf... this is abuse!!

     the for i.. will execute more than 1 because it will check the value of totaluniquechecks every time it reach that line...

    My basic foo might be going down. (Trading basic for sanity! Whohoo!) But if I remember right then basic precalculates the final value of a for loop only once, and then runs to it. Atleast, it seems to do that in VB6...



  • @Daid said:

    @topcat_arg said:

    this is not a wtf... this is abuse!!

     the for i.. will execute more than 1 because it will check the value of totaluniquechecks every time it reach that line...

    My basic foo might be going down. (Trading basic for sanity! Whohoo!) But if I remember right then basic precalculates the final value of a for loop only once, and then runs to it. Atleast, it seems to do that in VB6...

     and you are totally right.. my mistake...
     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.