Warning: Whiney rant ahead.
I'm not aware of any case where Option Explicit causes negative side effects, except in a poor script. A silly example:
Code:
Dim LastName
LastName = InputBox("Enter last name")
:
{several lines of misc. logic}
:
[COLOR=red]LastNmae[/color] = Trim(LastName)
:
{more logic, possibly hundreds of lines or more}
:
MsgBox LastName
If one leaves out Option Explicit, to many "programmer's" the code appears to work - yet clearly the string is never getting trimmed as desired. At some point a flaw like this will cause problems, and can be hard to track down - especially in lengthy runs of script.
If one puts in Option Explicit, the source of the trouble becomes clear quite quickly.
One of the crosses the VB/VBA/VBScript/ASP/Javascript community must bear is that this is the "low-rent district" of programming. I'm sure the Perl, etc. community has its share of these issues itself.
The fact is that the cost of entry here is rather low. In the case of VBScript for example, we get a lot of people with entry-level skills writing code, whether box admin folks or ASP authors. Everyone has to start someplace though, and it may as well be here.
I welcome these folks. But why set them on such a wrong road as suggesting they
ever leave out Option Explicit?
I am
completely frustrated by assisting people with VBScript who write code like my snippet above. After spending large amounts of time assisting them in tracking down a flaw of this type, they balk at my suggestion that they must use Option Explicit.
This is a much bigger headache for me in "real life" than in online forums. At least here my boss isn't standing over me, insisting that I fix Sloppy Joe's code - yet again.
Such code tends to have large numbers of these flaws. Many of these they manage to hack around, shotgunning until they more or less get the results they desire. This leaves them with things like faulty but ineffective logic. Or dead code - such as messed up logic sitting in a branch of an If that can never be taken (until one day it goes "boom!"). A common "hack fix" for the problem above looks like:
Code:
Dim LastName
LastName = InputBox("Enter last name")
:
{several lines of misc. logic}
:
LastNmae = Trim(LastName)
:
{more logic}
:
[COLOR=red]LastName = Trim(LastName) 'Sloppy Joe's "fix"[/color]
MsgBox LastName
So now he insists that Option Explicit "breaks my code" and then refuses to use it. Grr!
This is intolerable, and the suggestion that anyone
ever write VBScript without Option Explicit sets my teeth on edge. If anyone can tell me how Option Explicit has any arguable drawbacks I would love to hear them. It is most certainly
not "for design time" because most important code will undergo maintenance in its lifetime, and a great deal of VBScript goes through repeated phases of cut-and-try until it works "well enough." I see ASP pages that effectively
never get finished, and suffer more hacking and fixing for months after being placed into production.
Code can run for weeks, and then some untested path gets executed. Bang! You just screwed up some customer's account balance - and your code chugs right along without any notification. All because of a variable name typo and no Option Explicit.
It is like working with heavy equipment or power tools without proper safety gear: gloves, safety glasses, steel-toed boots, etc. Uncomfortable? Too bad!
By the way, I don't consider avoiding the need to declare variables a legitimate argument.