Wow. I'm only discovering this now.
I wish I had noticed that before. That's a big deal.
Without OPTION EXPLICIT, a variable name typo gets treated like a new variable created on the spot. And that can lead to programming errors that are very hard to pinpoint.
Let's consider a simple program example:
greeting$ = "hello there"
print gretting$
The program will run without error, but the results will be incorrect. "gretting$" (a typo of "greeting$") gets immediately treated as a new variable, has no value assigned to it, and gets printed. (The result: nothing printed.)
To avoid that kind of problem, which would be very hard to resolve in a long and complex program, start the program with an "OPTION EXPLICIT" statement.
OPTION EXPLICIT
greeting$ = "hello there"
print gretting$
Upon running the above program, we immediately get an error about "greeting$" being an undefined variable. So we define it and run the program again.
OPTION EXPLICIT
DIM greeting$ = "hello there"
print gretting$
Upon running the above program, we immediately get an error about "gretting$" being an undefined variable. And we know that this is a variable name typo.
Awesome.
No comments:
Post a Comment