Recent

Author Topic: Global Variable in program  (Read 2590 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Global Variable in program
« Reply #15 on: October 06, 2022, 08:37:57 am »
example is forthcoming. couple of hours. currently in meeting.
Specialize a type, not a var.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Global Variable in program
« Reply #16 on: October 06, 2022, 08:56:48 am »
sorry Thaddy, I don't do Business Meetings anymore  :)

I think I need an example. Global variables behavior was bugging me lately too. Especially when I expand on original code to add a new feature.

We all get a bit sensitive about the term, Global Variables, I suggest because when badly done, for example, early BASIC code, it made for a confusing system to maintain. But here we can, with care, work better.

Hmm, lets assume you have a project that uses several units. And all units have nice interface and implementation structure.  Lets assume one unit has -

Code: Pascal  [Select][+][-]
  1. interface
  2. type
  3.     TClassA = class
  4.     .....
  5.     public
  6.         MyAVar : integer;
  7.         ......
  8. type
  9.     TClassB = class
  10.     ....
  11.     private
  12.         MyBVar : integer;
  13.         ......
  14.      
  15. var
  16.         MyGlobalVar : integer;
  17.  
  18. implementation
  19. ...
  20. var MyRegionalVar : integer;
  21. ...
  22. //code for methods of TClassA  and TClassB

Now, there are four "global-ish" variables there, showing quite different characteristics. Easy stuff first,
  • MyBVar can only be used within TClassB methods so, I suggest its are not really global at all.   
  • MyAVar can be used within the TClassA methods AND can be used by anything that has access to an instance of TClassA. For every instance of TClassA, there is a MyAVar. 
  • But MyRegionalVar is a different thing altogether !  Its a global, there is only one variable in existence and that can only be used within TClassA and TClassB. Definitely more global than above but, importantly, another unit that 'uses' this unit cannot see MyRegionalVar.
  • Finally (*) we have MyGlobalVar, because its declared in the interface, it can be used by any unit that 'uses' this one. Yep, thats really Global !
(*) There are a number of other things possible here too, Class variables for example, just outside this lecture  :P

So, why are Globals bad ?  The real problem is when reading code, you can be left wondering if some process, maybe in a completely unexpected place, is changing the value of that variable. And wondering what will be affected if you alter its value. This is not so much of a problem in a "regional", you are looking at the only unit that uses it but is still well avoided if possible, especially if its a big unit.

Before we use a Global, consider using things like functions that return the value of a local variable (ie a MyBVar) to other units that need only read access or Properties that can control access and thus take away a lot of the mystery. But if you do decide to use a Global, make sure its well documented - "This is a global declared in UnitXX and tells us how many eggs broke, only UnitYY should change it." , maybe as a comment every time its used ! If its hard work using Globals, maybe you will look a bit harder to find an alternative  :).

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Global Variable in program
« Reply #17 on: October 06, 2022, 09:09:58 am »
Not business, charity fund.
Specialize a type, not a var.

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Global Variable in program
« Reply #18 on: October 06, 2022, 10:10:17 am »
@dbannon
Shouldn't the terms Visibility and Lifecycle be introduced first, and then asked what the Global means to the OP? Because IMHO for the beginners, the great deal of confusion comes from the misunderstanding of those.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Global Variable in program
« Reply #19 on: October 06, 2022, 10:39:57 am »
Shouldn't the terms Visibility and Lifecycle be introduced first, and then asked what the Global means to the OP? Because IMHO for the beginners, the great deal of confusion comes from the misunderstanding of those.

I agree, although I think "lifetime" is a bit more common in English for this.

Also in Pascal (unlike C etc.) "scope" and "visibility" are distinct because of the possibility of nested function definitions... in fact "scope" and "lifetime" are arguably equivalent.

MarkMLl
« Last Edit: October 06, 2022, 10:44:26 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018