Bay Area LabVIEW User Group

cancel
Showing results for 
Search instead for 
Did you mean: 

Local variables vs. functional globals for counters in state machines?

Hello all,

I'm writing some practice applications that use state machines, and need to keep track of some counter variables and array pointers that are initialized in a state machine Initialize state and are decremented within later states at the end of a process that is to be repeated.  These variables are also read at various intermediate points in the process.

Is it considered good programming practice to use local variables instead of functional globals for these (from the viewpoint of CLD exam grading), given the following?

1.  All write actions to these locals are in separate state machine states or are connected/governed by data flow wires to eliminate race ambiguity.

2.  There is no user input to these variables in the front panel.

I prefer to use functional globals for more complicated variables (such as for a cluster of data), and of course also when a variable is used by a subVI.  Any other considerations I should be aware of?

Thanks in advance!

Oliver Barrett

0 Kudos
Message 1 of 4
(5,138 Views)

Hello again,

In thinking about this some more, I should have remembered better what I learned in the NI training course about shift registers.  Using these to store variables that are updated and referenced entirely within the state machine cases (such as counters and pointers) is a natural, so I'll use this approach instead of local variables for this application.

Best regards,

Oliver

0 Kudos
Message 2 of 4
(3,776 Views)

Hi Oliver,

Thanks for the question.  Functional Globals are almost always considered better practice over Local Variables.  Personally, I try to only use Local Variables when I need to update a control on the Front Panel.  A few of my favorite benefits the Functional Global has over the Local Variable are:

1. Built-in protection from race conditions: http://zone.ni.com/reference/en-XX/help/371361E-01/lvconcepts/suggestions_for_exec/#Functional_Global_Variables

2. The ability to use a single Functional Global to manage multiple variables: http://decibel.ni.com/content/docs/DOC-5403

Regards,

Erik

0 Kudos
Message 3 of 4
(3,776 Views)

Definately use shift registers over functional globals or locals.  You should only use locals if you need interaction with FP nodes (getting/setting the value of a FP node is more efficient with a local than the "value" property node, although not by much - use a property node if you're already setting/getting other properties, or if you need to enforce dataflow/error handling that property nodes are good for).  Functional globals should only be used in their "functional" state (you're encapsulating an action on the data before and/or after it is stored), or you need to implement a by-reference paradigm (you can access the data from the functional global in multiple places by breaking the by-value dataflow paradigm).





Copyright © 2004-2023 Christopher G. Relf. Some Rights Reserved. This posting is licensed under a Creative Commons Attribution 2.5 License.
0 Kudos
Message 4 of 4
(3,776 Views)