Posts: 117
Threads: 33
Joined: Apr 2022
Reputation:
8
(11-07-2024, 10:13 PM)madscijr Wrote: (11-07-2024, 10:08 PM)Kernelpanic Wrote: I don't really understand your problem description, but in general, local variables are preferable to global ones in every respect. In general, but sometimes one can't avoid it.
GoSub is one of those things... The QBasic/QuickBasic reference says that this is only supported for compatibility reasons, but it is not recommended. Yeah, gosub is one of those hallmarks of spaghetti code that I completely avoid!
I got the same aversion about GOTO. It's the sauce in spaghetti code "Once you use it hard to control where it goes, or gets used." I basically try to only use it as a way at the bottom of code to get to the top. I avoid for/next, in favor of do/loops. In a do/loop I can use loop exit, instead of another goto.
Posts: 301
Threads: 10
Joined: Apr 2022
Reputation:
44
(11-07-2024, 08:04 PM)doppler Wrote: Sort of what I expected. Better to ask before tripping over a landmine of problems. I have fallen for the undeclared shared variable, only to see it as a local to the sub only. Wondering why nothing happen after the sub call.
Thanks all around. It's unrelated to the Shared/local discussion, but this problem can be solved by using `Option _Explicit`. It forces you to declare every variable, that way if you didn't declare a variable shared but also didn't declare it locally then you'll get an error at build time.
Posts: 296
Threads: 16
Joined: Apr 2022
Reputation:
51
global variables are reserved for either inexperienced, lazy, or really advanced programmers -- stick to the middle of the pack for the safer bet
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
Quote:@doppler - I got the same aversion about GOTO.
Sometimes GoTo is really OK, otherwise you would have to use a more complex loop. I posted something about this a while ago. The original is: Debugging the Software Development Process. It contains a good example of GoTo.
The random generator with GoTo:
Code: (Select All)
FalscheEingabe:
Print
Input "Anzahl der Zufallsnummern (Max 10): ", anzahlNummern
If anzahlNummern > 10 Then
Beep: Print "Maximal 10 Nummern!"
Sleep 2
'System
Cls
GoTo FalscheEingabe
End If
Posts: 744
Threads: 34
Joined: Apr 2022
Reputation:
49
Sharing is for commie pinkos.
Tread on those who tread on you
Posts: 2,219
Threads: 225
Joined: Apr 2022
Reputation:
110
11-08-2024, 06:59 PM
(This post was last modified: 11-08-2024, 07:01 PM by Pete.)
Consider using TYPE for variables, unless you are working with mostly arrays. Very easy to pass typed variables instead of adding each variable to the sub or function call. Personally, I got tired of looking at sub calls with 20+ passed variables. Using TYPE solved that clutter situation, nicely.
I used to do all global variables, to save memory, back in the 1990's. To make things even more difficult, the names had to be very short to save memory, too. Most of mine were just 1 or 2 characters. Still, I managed to get a 250 K multi-modular routine running that way, so I'm not one to knock the value of global variables. They can work, but now that name length isn't an issue, I'd probably use some underscore system to identify what routine it was involved in like cnt_keyboard%, for example.
Pete
Okay, had to come back to edit in @SpriggsySpriggs. That comment cracked me up!
Posts: 740
Threads: 104
Joined: Apr 2022
Reputation:
14
11-10-2024, 06:13 PM
(This post was last modified: 11-11-2024, 01:18 AM by madscijr.)
(11-08-2024, 06:39 AM)vince Wrote: global variables are reserved for either inexperienced, lazy, or really advanced programmers -- stick to the middle of the pack for the safer bet Global variables work for me, but I make sure to name them in such a way that identifies them as global - ie g_MyVar or m_MyVar (in VBA, the m is for module, as in variables common to the current module).
But any routines I expect to be truly portable can't rely on global variables, their values are passed via parameters.
Posts: 371
Threads: 30
Joined: Jul 2022
Reputation:
25
(11-08-2024, 06:39 AM)vince Wrote: global variables are reserved for either inexperienced, lazy, or really advanced programmers -- stick to the middle of the pack for the safer bet
LOL
global variables are like GOTOs, GOSUB and DEF FNMyFunction!
Only the worse and the best of coders use them!
Do you have a Function that can distinguish between the first and the second type of coder?
It would be interesting...
Well
I post this picture for remembering the field of existence of variables and how to work with them (find, see, read, modify etc)
In BASIC we have keywords like COMMON, DIM (REDIM) SHARED, SHARED for declaring variables visible from different levels of code.
There is something like this way to share variable also in other programming languages?
|