Recent

Author Topic: Global variable alternative  (Read 4549 times)

jamie

  • Hero Member
  • *****
  • Posts: 7488
Re: Global variable alternative
« Reply #15 on: March 22, 2020, 02:42:45 pm »
I guess you could also use the CUSTOM OPTIONS in the project settings to add a compiler define but that is compile time selectable not runtime.. so take your pick
The only true wisdom is knowing you know nothing

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Global variable alternative
« Reply #16 on: March 22, 2020, 02:58:04 pm »
Hi.

As I had already suggested, using a dedicated Unit might be useful. However, an OOP-type approach will have to be used to access the "global variables" stored in it, which will in fact now no longer be traditional global variables.

Otto.
Kind regards.

jamie

  • Hero Member
  • *****
  • Posts: 7488
Re: Global variable alternative
« Reply #17 on: March 22, 2020, 03:29:46 pm »
I only use that option if I need to compile different versions of code where I need difference in code.

 For example, The difference between a basic version and a professional version of the app.
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 8525
Re: Global variable alternative
« Reply #18 on: March 22, 2020, 06:43:39 pm »
As I had already suggested, using a dedicated Unit might be useful. However, an OOP-type approach will have to be used to access the "global variables" stored in it, which will in fact now no longer be traditional global variables.

Assuming a graphical app., better is to have a global-storage type accessible via a field in the main form.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Global variable alternative
« Reply #19 on: March 22, 2020, 07:31:39 pm »
Hi  MarkMLl.

Yes, you're right. In the post you mentioned I was referring to a method so to speak "safe". I had previously tried to explain that I really appreciate the FPC because it allows a great freedom to the programmer. In fact, you can use an OOP approach mixed with techniques so to speak "traditional". Of course, this also carries risks, so I would recommend that beginner programmers study both programming techniques, which I have previously mentioned, well from the outset.

Otto.
« Last Edit: March 22, 2020, 07:34:02 pm by Otto »
Kind regards.

julkas

  • Guest

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Global variable alternative
« Reply #21 on: March 23, 2020, 03:37:07 pm »
Thank you julkas.
it was a very interesting and on-topic reading.

On my browser, perhaps because of the protections, the link to Part II does not work. In case anyone is interested this is the link to the "Part II"
The curious thing is that I often use these techniques in C#; I have now learned that some features from the .Net have been introduced in Delphi.

In this regard, I have opened a thread to talk about the introduction of PartialUnits in Lazarus without the need to make any changes to the FPC. If you have time, take a look at it.

Otto.
Kind regards.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Global variable alternative
« Reply #22 on: March 23, 2020, 10:33:32 pm »
I don't really get it why global variables in all cases should be bad. If you use a global for loop variable or similar I totally agree that this is no good design. However if for example I have an application class (Like the one in LCL) then it makes total sense to me that there is a global variable for the application class (A singleton). Though possible, it does not make any sense in my opinion to implement it as a class field of TApplication in this case.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Global variable alternative
« Reply #23 on: March 23, 2020, 11:10:32 pm »
Hi soerensen3.

I also believe that every case must follow its own rules. However, in general, it is true that the use of too many global variables can make software more complex to manage; especially in the case of many interactions between the various components of the project. On the other hand, it should be taken into account that the final performance of the code may be affected by excessive (and sometimes unnecessary) use of abstract structures. What matters most, in my opinion, is the assessment of an ideal compromise between performance and security (real or presumed to be).

Otto.
Kind regards.

 

TinyPortal © 2005-2018