Muller's World

Mike Muller's Homepage

2006-06-30 VBScript

Last week I needed to get Outlook to delete all of the messages in a folder, so I decided to try writing a macro in VBScript. Now, I started programming way back in the early 80's, so my first programming language was BASIC. On a very small number of occassions in the years since, I have had an opportunity to see how MS has evolved the language.

My latest experience has done nothing to improve my impression of this company or the language. VBScript appears to me to be nothing more than a twisted perversion of the original language into the era of object-oriented scripting languages.

My biggest complaint is the statement syntax. When I create a macro I get an editor containing something like this:

   Sub test()

   End Sub

So far so good. "Sub" introduces a subroutine, "End Sub" closes it off. Perhaps a little more verbose than I would like, but consistent. Now we declare a variable:

   Dim message As Object

"Dim???" WTF? Now you have to appreciate the history of this. In the anciant MS BASIC that I used back in the Dark Ages, "dim" was a statement that was a short form of "dimension" - it was used to declare array variables, and was necessary because the interpreter needed to know the dimensions of an array. So rather than introduce a new keyword for general variable declaration, MS decided to extend the use of "Dim" to encompass all variable declarations.

Now lets move onto control structures. VBScript has a "For Each" statement (I'm assuming they still preserve the "For i = 1 To 10" type syntax) which is all well and good. So given that the "Sub" control structure was closed with "End Sub," one might expect "For Each" to be closed with "End For" or some such, right? Wrong. "For Each" is closed with "Next." Again we can trace this back to the BASIC of yore: in the old days, before any notion of structure was introduced to the language, "next" was effectively a branch back to the matching "for" (IIRC, you actually specified the statement number after the next, so it would be something like "100 for x = 1 to 10; 110 next 100").

The "While" statement turns out to be equally silly in its adherence to archaic syntax. "While ... End While?" "While .. Next?" Nope. "While ... Loop!" To be honest, I have no idea where the "Loop" came from. I'm not even sure if "while" was part of the language back in my day.

Now here's the kicker: after all of this cruft with the other control statements, the "If" statement (probably the oldest construct in BASIC) actually restores some sanity. They chose to close off the "If" with "End If." Thank you. Now go fix the other control structures.

Going beyond syntax, there's this whole business with variables that contain objects (as opposed to mere numbers or strings). You can use "x = 100" to assign a numeric variable, but you must use "Set x = New Thingamjig" to assign an object variable. Then, at the end of your subroutine, you have to use "Set x = Nothing" to uninitialize your variable (this is according to one tutorial I read and all of the examples I have seen). Once again, huh??? The fact that you must uninitialize a local variable in a scripting language frightens and confuses me.

So then, after writing my brief script (to delete all of the messages in a folder, functionality that arguably should have been built into Outlook) I run it - and Outlook locks up. No screen repaints, no nothing until the script is done. Now this would be perfectly reasonable behavior if I were writing an extension in C++, but should a macro written in a scripting language exposed to your stupid end users really lock up the main thread? I think not.

To be fair, the one thing I have to say that I like about the VB world is the fact that the editor will pop up a list of method names after you type a variable name and a dot. Of course, that's more a feature of the editor than of the language itself.

So how did MS users end up with such a crappy language? From the looks of it, at no point in the history of the evolution of BASIC did anyone throw anything away. And for the life of me, I can't understand why. It's not as though you would actually try to run code written in an earlier basic incarnation in VBScript. Furthermore, if you really wanted to be able to do that, there are better ways to accomplish that goal. I can't believe that normalizing the syntax would have been so terribly confusing to BASIC veterans that they would have abandoned the platform.

So I hope that for the sake of MS weanies everywhere, MS starts out with a clean slate for their next incarnation of BASIC. Or, better yet, just throws it away and makes their tools extensible through all of the other scripting languages out there. Not that this would make me switch from Linux, of course :-)