Posts: 4,698
Threads: 222
Joined: Apr 2022
Reputation:
322
06-04-2023, 04:29 PM
(This post was last modified: 06-04-2023, 04:36 PM by bplus.)
This thread started so people can talk about Option _Explicit without hijacking other threads.
Use Option _Explicit in your code to save yourself from typos, probably the number one cause of grief to any coder on any level.
Yes it forces you to declare every variable you use. Yes Dimster that includes For ... Next index variables.
If declared variable is a Const, you don't have to DIM it, same goes for Static, same goes for ReDim (or should).
But there is an interesting by-pass, if I recall, stay tuned... Nope! the example I had in mind didn't work as I remembered.
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 4,698
Threads: 222
Joined: Apr 2022
Reputation:
322
06-04-2023, 04:56 PM
(This post was last modified: 06-04-2023, 04:58 PM by bplus.)
(06-04-2023, 04:38 PM)TerryRitchie Wrote: I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial:
https://www.qb64tutorial.com/lesson2
I used Steve's Cheetos example too 
LOL yeah Cheetos is very memorable!
I recommend also while you are DIM your variables, to do them one variable per line at a time and include a commented description.
@TerryRitchie I think you already do this and I remember Walter recommending it as well.
example:
Dim as long i ' a general index for For ..Loop structures in main code
BTW Option _Explicit won't save us from the blunder of using a variable already DIM'd but not the one you intended to use at that particular spot, that would require a truly intelligent AI in the IDE.
This could happen when people use i, ii, iii, iiii... all you have to do is fail to type the proper number of i's.
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 3,447
Threads: 376
Joined: Apr 2022
Reputation:
345
Let *all* know the power of CHEETOS in programming! MuHaHaHaHa!
Posts: 473
Threads: 70
Joined: Apr 2022
Reputation:
18
So here is my beef with Option _Explicit, and it only pertains to those variables used as a control variable in a loop. Once the loop is finished the control variable carries a value of Loop Limit +1.
If I do not DIM a control variable or I avoid using Option _ Explicit
- I'm in less danger of an unexpected value to the control variable. I will use the same variable over and over again and I can count on it's value being one of the values in range ? TO ?.
Should I need to use the control variable in a sub or function, it is only then that I Dim it
- Because my control variables are generally one letter ( x, y, etc) I'm in no danger of misspelling it
- As your program adds routines, should you need a new control variable you just create it and carry on. If you use Option _Explicit you have go back and address Dimming its type and
then back to your new creation. Depending on the size of your program this can be cumbersome
- Control variables are necessary and plentiful and often created on the fly. I feel it's a waste of time and a loss of focus to be constantly Dimming them
That's my 2 cents. Perhaps For Next should be exempt from Option _ Explicit scrutiny
Aint called Dimster for nothin.
Posts: 4,698
Threads: 222
Joined: Apr 2022
Reputation:
322
06-07-2023, 02:58 PM
(This post was last modified: 06-07-2023, 03:01 PM by bplus.)
(06-07-2023, 02:09 PM)Kernelpanic Wrote: (06-04-2023, 04:38 PM)TerryRitchie Wrote: I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial:
https://www.qb64tutorial.com/lesson2
I used Steve's Cheetos example too 
@Terry (and @bplus) a constant is just an ordinary variable that can be accidentally changed without the Option Explicit.
From your tutorial:
![[Image: Terry-Const-ohne-Option-P-07-2023.jpg]](https://i.ibb.co/fH5GQ4G/Terry-Const-ohne-Option-P-07-2023.jpg)
KP "@Terry (and
@bplus) a constant is just an ordinary variable that can be accidentally changed without the Option Explicit."
And once again I say that if you declare a variable a Const, you don't need Option _Explicit to tell you you messed up when you try to use it like a REAL variable, see here!:
Code: (Select All)
Const i = 1
for i = 1 to 10
? i
next
As you can plainly see the IDE ain't buying your blunder!
@KernelPanic that's it from me for you on this subject.
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 4,698
Threads: 222
Joined: Apr 2022
Reputation:
322
@Dimster Option _Explicit is an option, you don't have to use it.
I don't usually use it in small codes until I have to start chasing down bugs which doesn't take long with my style of coding ;-))
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever