Where did I put that image again?



  • This one's from a few years back when I took over the maintenance of a ASP-based online store. There are tens of thousands of products in the catalog each with an associated images. At some point they must have started to encounter problems with a limit on the number of files within a single directory, and it must have been decided that it would be better to have multiple numbered directories with let's say a thousand images in each one. As each product has a unique ID there's no need to store the image filenames in the database, they can be named from the product ID and stored in the correct directory when they are uploaded. When it comes time to display them, we can just work out the path using the product ID and a little bit of code, right?

    Not only did they manage to put 1001 images in each folder (meaning that the folder name and the product ID would drift apart - the image for product 68000 would be in prod_img_68/68000/xxx.jpg, product 68200 would be in prod_img_69/68200/xxx.jpg). Never mind, no-one needs to calculate it - that's what computers are for. Here's the function that did exactly that:

    ---Function to find the image folder---
      Function imgFolder(intid)
        'on error resume next
        If Len(intid) <> 0 Then
          'Define Variables and set Constants
          Dim istep, i, strPrefix , prodid, icount_l, icount_u
          istep = 1001
          strPrefix = "prod_img_"
          prodid = Cdbl(intid)

          i = 1
          icount_l = 0
          icount_u = 1000
          Do Until prodid >= icount_l AND prodid <= icount_u
            i = i + 1
            icount_l = icount_l + istep
            icount_u = icount_u + istep
          Loop

          'Format and return
          If Len(i) = 1 Then
            i = Cstr(i)
            i = "0" & i
          End If
          imgFolder = strPrefix & i
        Else
        imgfolder = "prod_img_00"
        Exit Function
        End If
      End Function

    If you thought the choice of 1001 images per directory was inspired, you've got to love the fact that it checks EVERY possible value from zero upwards to see which band of 1001 it falls within. This went a long way to explaining why one of the webpages which showed images for the newest 200 products was a extremely slow (and getting slower every day that products were added and the product IDs increased). Every one of those 200 products on that page (which at the time had IDs in the 60000+ range) had to call this function and have it count from zero to 6x,xxx before returning the path of the image. Maybe the page loaded too fast and the owner wanted to slow things down a bit? Or the coder was a big fan of "The Count" from Sesame Street?

    I'm no genius coder by any means, and until then hadn't done any ASP/VBSCRIPT coding. But I gave it a whirl and came up with:

    Function imgFolder(intid)
      If Len(intid) <> 0 Then
        Dim i
        i = 1 + intid \ 1001
        If Len(i) = 1 Then
          i = "0" & i
        End If
        imgFolder = "prod_img_" & i
      Else
        imgFolder = "prod_img_00"
      End If
    End Function

    Hopefully the guy who wrote this decided against a career as a web programmer and picked something a bit more appropriate - like programming defense systems for the military or something.


Log in to reply
 

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