Saturday, September 30, 2006

VBScript == teh SuxOrz


VBScript sucks in so many - oh, so many! - ways.

The interaction between VBScript and ADO is particularly non-intuitive and bug-ridden. Let me make a note of this one in particular, before I push it down into the part of my brain where it will become just another mote swirling around with all of the other instances of VBScript sucktitude in my memory.

Sometimes you want to know if a field of a database record is NULL, because some VBScript functions like Replace() will choke if you pass them a NULL field. This does not work:


for objField in rsQ.Fields
  if IsNull(objField.Value) then
    strValues = strValues & "NULL,"
  else
    strValues = strValues & "'" & Replace(objField.Value, "'", "''") & "',"
  end if
next
ASP will crash out saying "invalid use of Null: Replace()" even though the value passed the IsNull() check. This, however, works fine:


for objField in rsQ.Fields
  scratchVal = objField.Value
  if IsNull(scratchVal) then
    strValues = strValues & "NULL,"
  else
    strValues = strValues & "'" & Replace(scratchVal, "'", "''") & "',"
  end if
next
One of the nicest projects I ever inherited was from a guy who had done a site in "old" ASP (pre-dotNet) but he did all of the scripting in server-side JScript. It helped that he was a good programmer, but with JScript you spend less time fighting the language. Unfortunately, you can only combine VBScript and JScript under some narrow conditions, so if you inherit a site already done with VBScript you can't just start writing all of your changes in JScript. Even if you fit inside those narrow conditions at the beginning, you'll probably very quickly reach a point where you need to include some JScript into VBScript at a very specific point or vice versa, and the necessary ordering of the code just isn't going to work out.

What you can do that's better yet is to start a gradual migration to ASP.NET, using this really great trick for sharing session state between ASP and ASP.NET on the same server.

No comments: