[VBScript] Am I missing something, or is this code just a WTF?



  • Yes, it's me again.  And no, I haven't rewritten that CRM yet ;-)

     I'm actually working on trying to automate some of the processes here, so employees don't have to manually hit an ASP page to perform the task.  My predecessor left a VBScript file that downloads a .txt file from our vendor's website containing updated product stock.  I'm refactoring it at the moment to make it more readable.  I came across this snippet (all indentation is kept how it is in the file):

     

    ctr=0
    

    if ubound(linary)>35 then

    'loop through the lines to get at the data
    for each a in linary

    ln=cstr(linary(ctr))
    if left(ln,1)="D" then
    psku= trim(mid(ln,2,15))
    puom= trim(mid(ln,17,2))
    prest=mid(ln,19,800)
    pstr=""
    pstrt=1
    pstp=8

    for b=1 to 100 step 1
    pstr=pstr&mid(prest,pstrt,pstp)&","
    pstrt=pstrt+8
    next
    

    pstr=left(pstr,len(pstr)-1)
    myFile.writeLine psku&","&puom&","&pstr
    end if
    ctr=ctr+1
    next

    Maybe I'm forgetting something, but wouldnt the variable "a" (assuming it's a string) already be the same thing as ln = CStr(linary(ctr)), and because it's a For Each loop there'd be no need to use the counter at all? I mean, "a" isn't even USED in the loop - it might as well be For ctr = 0 To UBound(linary). But then again I'm no expert on VBScript. I'm just trying to refactor the code smells (and believe you me, this code is RIPE!) a little to make my job easier.

    Am I right? 



  • All abord the phail boat set sail for phail!!!!1!!!



  • I think I got run over by the "Failboat" when I took this job ;-) 



  • One of the biggest signs of bad code is when you see someone using foreach and a counter.

     This clearly shows that they don't understand what is actually going on. That code is a serious WTF.



  • The question is, is the variable ctr used outside that snippet? I'm guilty of occasionally using counters inside loops, where I have a pressing need to know how many iterations occurred after it finished. This may be a case like that.



  • It was not used outside, although it's used outside of other, similar functions (copy/paste programming at its best!), so that's probably why its there.  Of course, the variable names are so cryptic that I'm not 100% sure about that.  Using abbreviations for all your variable names is NOT good coding practice, unless you want job security (which, judging from the way the code is written, this guy was going for in droves.  And, evidently it worked as the company stuck with him for several years until I applied for the job, and then got rid of him)



  • @Kyanar said:

    I'm guilty of occasionally using counters inside loops, where I have a pressing need to know how many iterations occurred after it finished.
     

     

    This may or may not be a WTF, depending whether you could get the same info from a single line outside the loop, e.g. size(linary) or whatever VBScript's syntax is for that.  It wouldn't be a WTF if you were looping through data as you got it from an outside source, and/or you cut the loop iteration short in some cases and needed to exclude those from the count.

     


Log in to reply