LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

understanding the global variables concept

Solved!
Go to solution

hi guys,

i just have a quick question..

I found out that my main program uses 855 global variables.

it wasn't built by me.

i am hoping if you guys have dealt with this before?

What do you think its a good approach for me. 

Find ways to get rid of all global variables using cluster?

or leave it the way it is because its working and don't borther with it.

or is there another solution?

thank you in advance

Best regards,
Krispiekream
Message 1 of 17
(5,319 Views)
Solution
Accepted by krispiekream
If I were you I would just whimper a little and then conform to the architecture I inherited.
Message 2 of 17
(5,311 Views)

hahaha...

i am not a consultant though..

i am here till i quit, get FIRED. or die...hahaha. 

it bugs me that everytime i want to use the variable i have to look around for it...

i dont know if its worth the time to fix it though..and i dont want to break anything either..

Best regards,
Krispiekream
0 Kudos
Message 3 of 17
(5,303 Views)
It's situations like the one you're in that have veteran programmers telling people to avoid using Global (and Local) Variables whenever possible.  Not because having one or two is really a bad thing.  But because when you end up with nearly a thousand of them, the risk of race conditions goes way up and you've probably got a bad programming structure to boot. 

I'm not sure what your best approach for your current code is.  That's going to depend on a number of things. 

Have a look at this Basic Functional Global Variable Example.  If you look around the forums, you'll find a number of discussions around these.  Also have a look for Action Engines.

   

---------------------
Patrick Allen: FunctionalityUnlimited.ca
Message 4 of 17
(5,300 Views)
Patrick is right.  Functional Globals are better to use than Global Variables because they do not allow race conditions because they are not reentrant, and therefore can only be written to in one place at a time.  However the task of replacing all your Globals with Functional Globals or Clusters sounds like a lot of work.  I just inherited a code base containing 1200 VIs and nearly as many Globals as you have.  The modifications I make use Functional Globals where appropriate and when I have to use an exisiting global I am very careful not to introduce a race condition.  Luckily for me I have just been tasked with rewriting this code using the Statechart Module, goodbye Globals!
Message 5 of 17
(5,291 Views)

You can use Find and Replace... to locate all the variables and to replace them with the functional global.  Of course the Functional Global will have an additional input to be wired, so you will still need to go to each place in the diagram where they occur.  Find Next is your friend for that. 

 

Lynn 

Message 6 of 17
(5,267 Views)
The problem with Find Next is that you first have to know what Global you are looking for.  In my case there are so many globals that are poorly named I have no idea what global I need to do the job.  I am sure that krispiekream has run into this too.
Message 7 of 17
(5,264 Views)

yeah..i run into that too....

function global variables is using wires/shift registers to carry data from one subvi to the next right?

isn't that the same as using clusters of data as in bundle and unbundle of data?

 

Best regards,
Krispiekream
0 Kudos
Message 8 of 17
(5,251 Views)

krispiekr...,

 

Now that the laughter has subsided and the rest of us have had time to thank the gods that we're not dealing with your application...... 

 

You can pick your battles and try to improve areas that are obvious.  I would be tempted to see if I could group signals into clusters or arrays and use Functional Global Variables to pass data between vi's.  Still going to be tough trying to track down all the variables.

 

 

jmcbee,

 

If I must wimper it's always into a cold beer.

Message 9 of 17
(5,245 Views)

Hi krispiekream,

 

a FGV is like a global, but without race conditions (if implemented correctly).

 

A cluster is just a datatype...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 17
(5,245 Views)