Wednesday, September 27, 2023

πŸ“š OPTION EXPLICIT !!!

Wow.  I'm only discovering this now.

While haphazardly scanning the wwwBASIC.js code, I stumbled upon the javascript code that handles  "OPTION EXPLICIT" in a BASIC program.

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

πŸ–₯ Fan Dance

This is a port and mod of a SpecBAS program by ZXDunny. Run the program View the source code And, a "less is more?" version: Run t...