06-03-2023, 07:21 PM
(06-03-2023, 03:10 PM)Dimster Wrote: Hi Steve - I do see the value in Option _Explicit catching those misspelled variables, however my control variables (x,y,i etc) are difficult to misspell. Terry has an interesting approach to nomenclature of variables and as B and you have pointed out, you guys use it all the time. So I'm guessing here you have specific control variables which you use all the time solely for loop/iterations? You would need a least two for sort routines and perhaps a 3rd as a backup. These 3 or so control variables would be Dimensioned when? On the fly as you need them or because you know they will probably be needed some time, they are Dimensioned as a routine course after Option _Explicit?
Steve highlighted the perfect example for the use case of OPTION _EXPLICIT. Having to DIM all variables is not a negative side effect of using it. Here is an example I coded in the last half hour. Both of these code listings do the exact same thing. The first example is reminiscent of what was found in those early BASIC books. The second example is what can be done using QB64 and a bit of variable planning. Imagine being handed the first example and your task is either to find a bug or modify the code in some significant way. Now you're handed the second listing and given the same task. Which would you prefer?
Code: (Select All)
'My cool program
DIM x(59)
DIM y(59)
r = 120
FOR i = 0 TO 6.178465447 STEP .104719753
x(s) = COS(i - 1.5707963) * r + 320
y(s) = SIN(i - 1.5707963) * r + 240
s = s + 1
NEXT i
SCREEN _NEWIMAGE(640, 480, 32)
DO
CLS
_LIMIT 60
ps = s
s = VAL(RIGHT$(TIME$, 2))
IF ps <> s THEN
s6 = 0
ELSE
s6 = s6 + 1
END IF
CIRCLE (x(s6), y(s6)), 10
PAINT (x(s6), y(s6))
CIRCLE (x(s), y(s)), 10, _RGB32(255, 255, 254)
PAINT (x(s), y(s)), _RGB32(255, 255, 254), _RGB32(255, 255, 254)
_DISPLAY
LOOP UNTIL _KEYDOWN(27)
SYSTEM
Code: (Select All)
' 60th of a second sweeping clock
' Circles sweep around a clock face at 1 second and 60th second intervals
OPTION _EXPLICIT ' force variable declaration
CONST PI = 3.1415926 ' value of PI
CONST PI2 = 2 * PI ' 2 times PI (one full radian sweep)
CONST Radian60 = PI2 / 60 ' 2 times PI divided by 60
CONST Radian4 = PI2 / 4 ' one quarter of the value of PI times 2
CONST SWIDTH = 640 ' screen width
CONST SHEIGHT = 480 ' screen height
CONST CCOLOR = _RGB32(255, 255, 254) ' one second sweep circle color
TYPE Tick_Coordinate ' location of each clock tick coordinate
x AS SINGLE ' x coordinate
y AS SINGLE ' y coordinate
END TYPE
DIM Tick(59) AS Tick_Coordinate ' tick coordinates
DIM Radian AS SINGLE ' loop counter
DIM Radius AS INTEGER ' radius of clock face
DIM Second AS INTEGER ' counter: one second
DIM Second60 AS INTEGER ' counter: 60th of a second
DIM pSecond AS INTEGER ' previous second
Radius = SHEIGHT / 4 ' calculate clock face radius
Second = 0 ' reset second counter
FOR Radian = 0 TO PI2 - Radian60 STEP Radian60 ' cycle through 60 radian points
Tick(Second).x = COS(Radian - Radian4) * Radius + SWIDTH / 2 ' calculate x coordinate
Tick(Second).y = SIN(Radian - Radian4) * Radius + SHEIGHT / 2 ' calculate y coodinate
Second = Second + 1 ' increment second counter
NEXT Radian
SCREEN _NEWIMAGE(SWIDTH, SHEIGHT, 32) ' graphics screen
DO ' begin main loop
CLS ' clear screen
_LIMIT 60 ' update frame every 60th of a second
pSecond = Second ' save previous second
Second = VAL(RIGHT$(TIME$, 2)) ' get current second
IF pSecond <> Second THEN ' has a second elapsed? <-- These lines synch the 60th
Second60 = 0 ' yes, reset 60th second timer <-- second circle to the top
ELSE ' no, still within same second <-- of the sweep during second
Second60 = Second60 + 1 ' increment 60th second counter <-- value changes.
END IF
CIRCLE (Tick(Second60).x, Tick(Second60).y), 10 ' draw 60th second sweep
PAINT (Tick(Second60).x, Tick(Second60).y)
CIRCLE (Tick(Second).x, Tick(Second).y), 10, CCOLOR ' draw one second sweep
PAINT (Tick(Second).x, Tick(Second).y), CCOLOR, CCOLOR
_DISPLAY ' update screen
LOOP UNTIL _KEYDOWN(27) ' leave when ESC pressed
SYSTEM ' return to operating system