Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Option _Explicit Keyword(s) of day XXX:
#1
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.
b = b + ...
Reply
#2
I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial:

https://www.qb64tutorial.com/lesson2

I used Steve's Cheetos example too Smile
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#3
(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 Smile

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.
b = b + ...
Reply
#4
Let *all* know the power of CHEETOS in programming!  MuHaHaHaHa!
Reply
#5
(06-04-2023, 05:39 PM)SMcNeill Wrote: Let *all* know the power of CHEETOS in programming!  MuHaHaHaHa!

Orange keys are definitely in fashion right now.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#6
(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 Smile
@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]
Reply
#7
(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 Smile
@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]
Correct. If OPTION _EXPLICIT were the first line of code in the example, the variable mm would have been flagged immediately. Also, a constant is not an ordinary variable. A constant can't have its value changed once set. The IDE will flag this if you try it:

CONST Foo = 10
Foo = 3

"Expected variable = on line 2"

The IDE does not recognize Foo as a variable because it's been declared as a constant.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#8
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.
Reply
#9
(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 Smile
@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]

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.
b = b + ...
Reply
#10
@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 ;-))
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)