Global variable
|
06-26-2017, 08:29 PM
(This post was last modified: 06-26-2017 08:33 PM by Han.)
Post: #8
|
|||
|
|||
RE: Global variable
(06-26-2017 04:02 PM)DrD Wrote:(06-26-2017 03:09 PM)Han Wrote: When several programs start using the same reserved variables, and those programs should happen to call each other, then you can easily imagine the mess that could result. But the other (bigger) reason for avoiding them is if a user makes frequent use of these variables for their own quick calculations, then your program could very well destroy their data. My personal philosophy is that all programs should never touch a user's data and leave every global variable, setting, flag, etc. as they were unless the program is designed to explicitly modify the user's data and/or system settings. Global variables, from a design point of view, are there so that users do not have to declare them for every use. Imagine for a moment if global variables did not exist from a user standpoint (and without even brining in programs into the picture). Such a setup would be very tedious. I am sure that this is only one of the reasons for the existence of (system) global variables. That their availability it is also a convenience to the programmer does not mean that programmers should just haphazardly modify the contents of such global variables. If your programs are for your own personal use, then I see no problem. But if you were publishing your programs, then I think it is plainly a matter of courtesy in not tampering with the global variables -- or at least restoring their contents after completion. Quote:Other than fear of ambiguous scenario's that "could happen," are there any tangible reasons not to use globals, that you can think of? Sure, you can create a program, and destroy the global data content it relies on, but such a program still works as you designed it. Thus its not a tangible reason to blame the use of global's for those consequences. The same can be said for any other aspects of a program structure. A program is only as good as its actual design, warts and all. Just because a program runs without bugs does not mean it cannot have undesirable effects. As already stated, if your programs are for public consumption, and they modify the users' data already existing in (system) global variables, then I think that is bad practice (just my own personal opinion). Sure, they make debugging and programming easier, but you can access any program's variable from the command line -- even if not exported -- by simply fully qualifying them. And their contents persist even after the program ends. So there really is no reason to be using system variables once you are at the stage of publishing your program. Lastly, even if you do use the global variables, a quick search and replace (with locals) once your coding is complete would also ensure that user data will never get tampered by your programs. If that is too much trouble, even a wrapper program which stores the user's contents prior to running, and restoring them after completion, would be preferable in my opinion. Code:
EDIT: I would even go as far as to suggest minimizing the use of EXPORT. Graph 3D | QPI | SolveSys |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Global variable - Powersoft - 06-25-2017, 06:56 PM
RE: Global variable - DrD - 06-25-2017, 07:46 PM
RE: Global variable - cyrille de brébisson - 06-26-2017, 05:27 AM
RE: Global variable - toml_12953 - 06-26-2017, 12:27 PM
RE: Global variable - DrD - 06-26-2017, 11:48 AM
RE: Global variable - Han - 06-26-2017, 03:09 PM
RE: Global variable - DrD - 06-26-2017, 04:02 PM
RE: Global variable - Han - 06-26-2017 08:29 PM
RE: Global variable - DrD - 06-26-2017, 09:04 PM
RE: Global variable - cyrille de brébisson - 06-27-2017, 04:51 AM
RE: Global variable - DrD - 06-27-2017, 11:32 AM
|
User(s) browsing this thread: 2 Guest(s)