How do you guys use local variables when programming in LabVIEW?
I've been wondering about the ways that local variables are typically used in real-life LabVIEW applications. I'm familiar with how they work and some of the caveats and warnings that come with them, but was interested in specific examples and applications/design patterns in which you've found them helpful. (Keeping this rather vague on purpose to see what examples you all might have!)
Any descriptions, use cases, or code snippets you'd be willing to share?
Thanks a bunch!
Most common use cases for me are to initialize or reset the front panel, and in simple test code where a local variable is an easy quick solution. For example, if I'm putting together a prototype user interface and need access to the same control in more than one event case, but not too many of them, I'll use a local variable, since putting the control outside the event case can have unexpected or undesired effects (in terms of reading the control value before an event executes that might affect the value of that control).
Mostly user interface:
Initialize controls on program start
Set value of controls based on program results. Too many different examples. For example unpress button if measurements ended programmatically.
Modify value of controls, linked together (for example cursor level on graph affects digital control that can be used to set the same level).
Slide with multiple sliders. (for example one - center, 2 others at fixed width) From value change event find slider that moved and adjust position of 2 others.
Not user interface:
When cluster-indicator is not worth making it a typedef, but I use Bundle by name.
I do my very best to make sure there are no local Variables in my LabVIEW code. Local variables are subject to Race Conditions (since there is no data flow), are hard to trace and locate (since there are no wires), and in most cases can be eliminated in favor of "better ways of doing this". An example is communicating between two loops, such as using a single Stop button to stop two parallel loops. I've used various methods to do this, but never (or, at least, not in the last 5 years after I "learned better") a Local Variable copy of the Stop control.
To make a control also and indicator. When the User changes a control, that causes something who's result is written into the control via a local. For example, if the User changes "setpoint" of an oven from 500 to 5000 degrees, but the maximum temperture setpoint can be is 800, then the control will show 800, not 5000. Or if some other process triggers a safety limit on the oven, then that setpoint reduction will be reflected in the "control".
This use of locals is done within a single loop, and thus in a defined order with no posibility of race conditions.
I like DrJ's example, which I coded up here:
But then I thought, "This looks like putting a Max on a Control and coercing it to not get too big". If this is what you want to do, then do it (which explicitly says "I'm making this control to have the following Max value, and you cannot set it bigger!!!"). Here's the other case of this Diagram Disable (enabled for clarity):
Now this is in two pieces. The first piece of code that sets the Upper Limit goes in the initialization section (and if you want to modify the Safe Temp, you simply do it again). The Event code now is dirt simple -- just read the value and use it, as you know the values are safe by design (well, by LabVIEW code, anyway).
I rarely use local variables, most cases that I use them are similar to DrJ's. Bob S's method of limiting the input also works, but as usual there are edge cases where it does not work, and then a local works well. (For example, a bandpass filter, the high frequency limit should be greater than the low frequency limit.) Race conditions can be avoided with well written code. Just remember that local variables create a data copy, which is fine for scalars, but not for large arrays.
Too often I see people using local variables when they could just use wires (this creates race conditions). So, if you can wire it, DO NOT use a local variable.
But then I thought, "This looks like putting a Max on a Control and coercing it to not get too big".
That only works in simple cases, where nothing else can affect the item of interest except the control. In more complex cases, particularly once one has started to get separation between 'User Interface' and 'Business Model', and maybe have multiple UI components, it becomes much more valuable (and simpler) to have the UI 'view' the 'model'.