Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats better to SHARED or not
#6
(11-07-2024, 05:02 PM)bplus Wrote: If you want to use Subs and Functions in other programs don't Share and don't Const their variables.

I had a routine that used P2 Const for _Pi(2) that I have to fix in every program not setup with that const.
It's really nice to plug-in with Copy/Paste a routine that's already been tested in another program. It saves  time for focus on the current program's needs.

Another consideration is that QB64 does not ByVal the varaibles passed through routines, so there is always a risk of accidently unintentionally changing a variables value in calling  module. You can also wonder which is better a GoSub or a Routine call without parameters to pass. So if you consider Sharing a bunch of stuff, consider GoSub instead.
If I want a variable to retain its value outside of a function's scope, I'll just use a global variable, and declare them all together at the top of the entire program (right after the global Const & Types). This makes it easy to know what global variables are declared, without having to look inside all the functions & procedure to see what's shared. For me that keeps it simpler. 

One thing that bugs me about shared variables inside a routine is, how do you know the function is being called the first time so you know to initialize its value the first time? Seems a little ambiguous. Do you have to declare a second boolean variable called something like bInitialized to track whether the function has been run at least once to know the shared vars have been initialized? Seems messy, so if I'm going to use variables that retain their value, I'll just declare them globally and initialize them all at the start of the program (or if I really want to keep my routines portable, don't use global variables at all, and just pass everything as parameters). 

Also, when declaring a variable as shared inside a routine, is the variable scope unique to that routine? So if I have a function MyFunction1 with a shared variable x%, and another routine MyFunction2 with its own shared x%, are they the same or does each function have its own instance of x%? (If I was at my PC, I could write a test prog and find out, but I am not.) 

I guess if they have their own instance, it could make sense to use shared if the value is only used inside the function, but intuitively it just feels messy, kind of like it's going against some design principle that I'm not remembering. It just seems cleaner to keep retained values either global to the whole program or pass them in parameters (per proper structured programming).

To keep things safe for parameters you just want to send "by value", I just do the simple thing and declare local copies, copy the parameter values to those, and only reference the actual parameter variables if I want to return a new value.
Reply


Messages In This Thread
Whats better to SHARED or not - by doppler - 11-07-2024, 04:23 PM
RE: Whats better to SHARED or not - by ahenry3068 - 11-07-2024, 04:29 PM
RE: Whats better to SHARED or not - by bplus - 11-07-2024, 05:02 PM
RE: Whats better to SHARED or not - by madscijr - 11-07-2024, 10:01 PM
RE: Whats better to SHARED or not - by doppler - 11-07-2024, 08:04 PM
RE: Whats better to SHARED or not - by madscijr - 11-07-2024, 10:13 PM
RE: Whats better to SHARED or not - by doppler - 11-08-2024, 03:10 AM
RE: Whats better to SHARED or not - by bplus - 11-07-2024, 11:39 PM
RE: Whats better to SHARED or not - by bplus - 11-07-2024, 11:44 PM
RE: Whats better to SHARED or not - by vince - 11-08-2024, 06:39 AM
RE: Whats better to SHARED or not - by madscijr - 11-10-2024, 06:13 PM
RE: Whats better to SHARED or not - by Pete - 11-08-2024, 06:59 PM



Users browsing this thread: 1 Guest(s)