Option _Explicit Keyword(s) of day XXX: - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49) +---- Thread: Option _Explicit Keyword(s) of day XXX: (/showthread.php?tid=1727) |
Option _Explicit Keyword(s) of day XXX: - bplus - 06-04-2023 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. RE: Option _Explicit Keyword(s) of day XXX: - TerryRitchie - 06-04-2023 I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial: https://www.qb64tutorial.com/lesson2 I used Steve's Cheetos example too RE: Option _Explicit Keyword(s) of day XXX: - bplus - 06-04-2023 (06-04-2023, 04:38 PM)TerryRitchie Wrote: I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial: 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. RE: Option _Explicit Keyword(s) of day XXX: - SMcNeill - 06-04-2023 Let *all* know the power of CHEETOS in programming! MuHaHaHaHa! RE: Option _Explicit Keyword(s) of day XXX: - TerryRitchie - 06-04-2023 (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. RE: Option _Explicit Keyword(s) of day XXX: - Kernelpanic - 06-07-2023 (06-04-2023, 04:38 PM)TerryRitchie Wrote: I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial:@Terry (and @bplus) a constant is just an ordinary variable that can be accidentally changed without the Option Explicit. From your tutorial: RE: Option _Explicit Keyword(s) of day XXX: - TerryRitchie - 06-07-2023 (06-07-2023, 02:09 PM)Kernelpanic Wrote: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:(06-04-2023, 04:38 PM)TerryRitchie Wrote: I added OPTION _EXPLICIT yesterday to Lesson2: Introducing Variables in the tutorial:@Terry (and @bplus) a constant is just an ordinary variable that can be accidentally changed without the Option Explicit. 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. RE: Option _Explicit Keyword(s) of day XXX: - Dimster - 06-07-2023 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. RE: Option _Explicit Keyword(s) of day XXX: - bplus - 06-07-2023 (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:@Terry (and @bplus) a constant is just an ordinary variable that can be accidentally changed without the Option Explicit. 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)
As you can plainly see the IDE ain't buying your blunder! @KernelPanic that's it from me for you on this subject. RE: Option _Explicit Keyword(s) of day XXX: - bplus - 06-07-2023 @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 ;-)) |