Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
(11-24-2022, 07:19 PM)james2464 Wrote: Nice program...the geometry becomes really interesting.
I don't think I've seen OPTION _EXPLICIT before. (Maybe I just didn't notice it yet)
After checking the wiki to see what it's about, I don't really understand. How do I request this for a keyword of the day?
I won't write code without having OPTION _EXPLICIT as my first line of code. As others have pointed out it forces you to declare every variable but I find it handy for another reason as well.
Often times when I'm writing code I'll create lots of variables, or share variables, within my functions and subroutines. As the code progresses some of these variables end up not being used. A quick ' (REM) in front of a declared variable will show if I'm still using it. For example
SUB ThisIsMySUB()
SHARED ThisVariable AS LONG
SHARED ThatVariable AS LONG
DIM AnotherVariable AS INTEGER
DIM YetAnotherVariable AS INTEGER
etc..
When I get close to completing a project and start the process of cleaning the code I can just REM out each SHARED and DIM line individually. The IDE will complain if a variable is in use because OPTION _EXPLICIT is the first line of code. If the IDE complains I know the variable is in use, if no complaints I can safely remove the declaration.
Also, OPTION _EXPLICIT removes the possibility of typos in variable names, the source of many, MANY, a sleepless night debugging.
I highly suggest always using OPTION _EXPLICIT as your first line of code. In fact, I wish the IDE had an option to enable this feature permanently.
Posts: 229
Threads: 25
Joined: Aug 2022
Reputation:
23
Very interesting. Thanks for providing this insight.
I'll definitely use this. A quick test made it obvious how helpful it can be.
Posts: 1,588
Threads: 59
Joined: Jul 2022
Reputation:
52
Some people don't like things imposed on them; this is well said for many that were very used to GW-BASIC or QBasic quirks and refuse to change their habits "because it doesn't let me run faster". When QuickBASIC came out, a lot of users of interpreted BASIC didn't adopt it because they absolutely hated type sigils, including the dollar sign, including to decorate the string functions. They sort of won their way in Freebasic but to their chagrin, "OPTION EXPLICIT" isn't necessary with the "default" language mode of that system. Those older programmers even turned up their noses at the subprograms instead of "DEF FN" and "GOSUB... RETURN", and at "SELECT CASE... END SELECT" instead of "ON... GOSUB/GOTO" although flaws were emphasized about the older constructs. QBasic became popular such that there are still people preferring to pass on Freebasic because "QB" language mode isn't recommended anymore for new programs, and they might be passing by QB64(PE) for other reasons making them suspicious.
Well, it could include a person trying to do a complex calculation. However, if the programming system doesn't let him/her start variables wherever he/she wants, at the risk of the final result being wrong, or zero when he/she didn't expect it, then he/she isn't even going to touch it. "I just want my program to run" could include using the older constructs that QB64(PE) still supports but were either eschewed or modified by other BASIC dialects. There are an awful lot of programs out there relying on implicit variable creation, on "FOR... NEXT" loops doing what should be left instead to "_DELAY", on arrays that begin at subscript zero, on the "default" type being single-precision and many other things, and even on the default screen being 80 characters across by 25 lines.
The "OPTION _EXPLICIT" will always be a choice made by the programmer. It will be his/her choice if he/she wants to track down typos but keeps blaming the product "for being stupid". Yes I had this happen to me programming in BASIC and Lua. I militantly insist in the version of the Lua interpreter directly from the Brazillian original programmers and their website, not these other spins obviously adding OOP and making sure "everything" has to be declared. But that is just me. I use "OPTION _EXPLICIT" with programs in QB64 because I'm trying to recover from that impulsive nature. Also it's a good idea when sharing programs on this forum to help new people understand the concepts better.
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
I never understood why new concepts and commands are sometimes looked upon as changing the old ways, especially for something useful like OPTION _EXPLICIT.
I use it because it makes coding easier. The first time I encountered a high-level language that forced variable declaration (Turbo Pascal 3.01) I was hooked. It's just a no-brainer to have such a mechanism in place in my opinion of course.
Posts: 1,588
Threads: 59
Joined: Jul 2022
Reputation:
52
(11-24-2022, 11:33 PM)TerryRitchie Wrote: I never understood why new concepts and commands are sometimes looked upon as changing the old ways, especially for something useful like OPTION _EXPLICIT.
I use it because it makes coding easier. The first time I encountered a high-level language that forced variable declaration (Turbo Pascal 3.01) I was hooked. It's just a no-brainer to have such a mechanism in place in my opinion of course. LOL you almost detected that "Unknown identifier" was my favorite error message from that compiler in high school.
Posts: 14
Threads: 2
Joined: Oct 2022
Reputation:
1
11-25-2022, 03:41 AM
(This post was last modified: 11-25-2022, 03:43 AM by King Mocker.)
I've done another one.
Similar to the first one but subtly different. It has an interesting effect when bouncing off the edges when the speed is increased, and when the number of points is changed.
Code: (Select All) Option _Explicit
Screen _NewImage(800, 600, 32)
' Triangle variables
Dim Shared As Integer XD(30), YD(30)
Dim Shared As Single AD, AD2, RD
Dim Shared As Single M, NI
Dim Shared As Single R, A2, A
Dim Shared As Long linecolor, linecolor2
'common variables
Dim Shared As Integer x(30), y(30)
Dim Shared As Integer N, XC, YC, size, i, j
Dim Shared As Integer dx, dy, scale, maxsize, minsize, maxdxy, shape
Dim Shared As Single alpha, spinspeed, adif
Dim Shared As Long shapecolor
Dim Shared As String k
linecolor = _RGB32(200, 200, 200)
linecolor2 = _RGB32(200, 0, 0)
'#####################################################
'## Main loop
'#####################################################
XC = 400: YC = 300
size = 150: scale = 10: minsize = 50: maxsize = 200
dx = 5 + Int(Rnd * 5) + 1: dy = 5 + Int(Rnd * 5) + 1: maxdxy = 40
TriangleMesh
End
'#####################################################
Sub TriangleMesh ()
linecolor = _RGB32(200, 200, 200)
linecolor2 = _RGB32(200, 0, 0)
N = 20 ' Outer Ring line count
M = 5 ' Inner Ring Line count
AD = _Pi / N:
AD2 = 2 * AD: A2 = 0: A = A2
RD = 1 / M
Do
_Limit 10
R = 1: A = A2
For i = 1 To N
x(i) = Cos(A) * size + XC: y(i) = Sin(A) * size + YC
A = A + AD2
Next i
Cls , _RGB32(0, 0, 0)
Locate 1, 1: Print "Down/Up: Dec/Inc. size -/+ : Dec/Inc # of Points"
Locate 2, 1: Print "A/D : Dec/Inc X direction W/S : Dec/Inc Y direction"
For j = 1 To M
R = R - RD: A2 = A2 + AD: A = A2
For i = 1 To N
XD(i) = R * Cos(A) * size + XC: YD(i) = R * Sin(A) * size + YC:
A = A + AD2
Next i
'##################
'## Draw the shape
'##################
For i = 1 To N
NI = (i Mod N) + 1
Line (x(i), y(i))-(x(NI), y(NI)), linecolor2
Line -(XD(i), YD(i)), linecolor
Line -(x(i), y(i)), linecolor
Next i
'Set outer to inner ring
For i = 1 To N: x(i) = XD(i): y(i) = YD(i): Next i
Next j
_Display
k = UCase$(InKey$)
WASD
If k = "-" And M > 3 Then M = M - 1 ' Press - key, decrease points on shape
If k = "+" And M < 15 Then M = M + 1 ' Press + key, increase points on shape
'##############################
' Get new shape screen position
'##############################
XC = XC + dx
YC = YC + dy
'#####################################################################
'## change direction of shape and keep it within the screen boundaries
'#####################################################################
If XC > _Width - size - scale Then dx = -dx
If XC < size + scale Then dx = -dx
If YC >= _Height - size - scale Then dy = -dy
If YC < size + scale Then dy = -dy
Loop Until k = Chr$(27)
End Sub
'##########################################################################################################################################
Sub WASD ()
If _KeyDown(20480) And size >= minsize And XC >= size + scale And YC >= size + scale Then size = size - scale ' UP Arrow
If _KeyDown(18432) And size <= maxsize Then ' Press DOWN Arrow and Size is not at maxiumum size
If _Width - size - (2 * scale) > XC And _Height - size - (2 * scale) > YC Then ' is not off right or bottom of screen
If XC >= size + (2 * scale) And YC >= size + (2 * scale) Then ' is not off left or top of screen
size = size + scale ' Increase Size of shape
End If
End If
End If
Select Case k
Case "A": If Abs(dx) > 1 Then ' 65
If Abs(dx) >= 1 And Abs(dx) <= maxdxy Then dx = Sgn(dx) * Abs(dx) - (Sgn(dx) * 1)
End If
Case "D": If Abs(dx) < maxdxy Then '68
If Abs(dx) >= 1 And Abs(dx) <= maxdxy Then dx = Sgn(dx) * Abs(dx) + (Sgn(dx) * 1)
End If
Case "W": If Abs(dy) < maxdxy Then '87
If Abs(dy) >= 1 And Abs(dy) <= maxdxy Then dy = Sgn(dy) * Abs(dy) + (Sgn(dy) * 1)
End If
Case "S": If Abs(dy) > 1 Then '83
If Abs(dy) >= 1 And Abs(dy) <= maxdxy Then dy = Sgn(dy) * Abs(dy) - (Sgn(dy) * 1)
End If
End Select
End Sub
Posts: 3,885
Threads: 174
Joined: Apr 2022
Reputation:
202
Interesting ball design.
b = b + ...
Posts: 2,673
Threads: 325
Joined: Apr 2022
Reputation:
214
(11-24-2022, 05:35 PM)King Mocker Wrote: Just curious,,doesn't _Keyhit use the same key press repeat delay that Inkey$ uses?
So, why use this in place of k=ucase$(inkey$)? Apart from the cool looking code.
Basic answer: numbers are faster than strings.
k isn't = ucase$(inkey$) as you assert above.
You'd need k = ASC(ucase$(inkey$)) to come close to the right answer. (I say CLOSE, because it doesn't deal with extended inkey$ values, such as the arrow keys.)
A true equivalent would be:
Code: (Select All) Do
k = _KeyHit
i$ = InKey$
If i$ <> "" Then
If Len(i$) = 1 Then i$ = i$ + Chr$(0)
k1 = CVI(i$)
Else
k1 = 0
End If
If k Then Print k, k1
_Limit 30
Loop Until k = 27
If you run the above, you'll see there's STILL a difference between the _keyhit value and what we've translated for our inkey$ value -- _KEYHIT can give us both an UP and DOWN code, while INKEY$ just tells us when a key is pressed DOWN.
^ And that's the differences in the two in a nutshell.
Posts: 2,673
Threads: 325
Joined: Apr 2022
Reputation:
214
(11-24-2022, 11:33 PM)TerryRitchie Wrote: I never understood why new concepts and commands are sometimes looked upon as changing the old ways, especially for something useful like OPTION _EXPLICIT.
I use it because it makes coding easier. The first time I encountered a high-level language that forced variable declaration (Turbo Pascal 3.01) I was hooked. It's just a no-brainer to have such a mechanism in place in my opinion of course.
Option _Explicit!!
HSSSSSS!!! EVIL!!
All kidding aside, me and Option _Explicit just aren't compatible. It's not that I'm against learning new ways; it's just that it doesn't suit my coding style with the rules it enforces.
Where me and Option _Explicit have our irreconcilable differences comes from this type of code:
Code: (Select All) SUB whatever
SHARED x, y, z
Since QB64 requires $INLCUDE code to be broken down into two modules -- *.BI and *.BM -- I usually tend to try and get around this with a simple SHARED statement when all I need to do is share a few variables between my library's subs and functions.
Option _Explicit, however, doesn't think that SHARED inside a sub/function counts as a valid declaration for the variable type in question. It wants you to DIM x, y, z inside the main module, before it ever shows up in the subs or functions...
...and that kind of defeats the whole purpose of trying to use SHARED to reduce $INCLUDE files down to a single file, rather than having two of them.
Somehow, it just seems kind of silly to me to have this for "My_Example_Library.BI", just so it'll stay valid with Option _Explicit:
Code: (Select All) DIM x, y, z
Personally, I've just came to the conclusion that me and it just aren't meant to work together.
|