LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it ok to use local variables within the same loop?

example.png

 

Most examples for local variables show them being used between multiple loops. But what if my code is starting to get messy and I want to reduce the amount of wires in a loop, so I do something like the basic example above. Is this acceptable? Or will I get odd behavior? For example, if I used a shared variable with an RT FIFO; if I'm reading from the shared variable from multiple places I'll get undesirable results.

0 Kudos
Message 1 of 22
(3,127 Views)

That very simple code is the most basic example of why NOT to use local variables. There is no way to determine if "numeric 2" will be written to first, or the local variable read first. 

 

If you have too many wires, I would suggest making a cluster that can contain multiple values and data types.

Message 2 of 22
(3,123 Views)

 


@StevenD wrote:

That very simple code is the most basic example of why NOT to use local variables. There is no way to determine if "numeric 2" will be written to first, or the local variable read first. 

 

 


If they are being implemented between different loops, how is that making it determinable which will be read or written first?

 


 

If you have too many wires, I would suggest making a cluster that can contain multiple values and data types.



Great idea on the cluster, thanks!

0 Kudos
Message 3 of 22
(3,118 Views)

You will have the classic race condition.  When is that local variable read compared to when the terminal was written?  You cannot determine this.  Data Flow is, by far, the most efficient method of passing your data around.  So if your loop is getting too unwieldy, you might need to think about your architecture.  Could things be moved to another loop?  Would it make sense to make more subVIs?  Could a bunch of wires be bundled into a cluster or, in my opinion, preferably a class?  (A class would likely combine the ideas of more subVIs (methods) and bundling the data (private data)).  There are a lot better ways to deal with messy code than switching to local variables.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 22
(3,113 Views)


If they are being implemented between different loops, how is that making it determinable which will be read or written first?

 


It is still not deterministic. Wires dictate data flow and program execution. There are a bunch of tools that work much better than local variables (queues, notifiers, etc). 

Message 5 of 22
(3,103 Views)

@StevenD wrote:


If they are being implemented between different loops, how is that making it determinable which will be read or written first?

 


It is still not deterministic. Wires dictate data flow and program execution. There are a bunch of tools that work much better than local variables (queues, notifiers, etc). 


Copy that

0 Kudos
Message 6 of 22
(3,094 Views)

Would a channel writer/reader setup be better than a local variable for sharing data between loops?

0 Kudos
Message 7 of 22
(3,088 Views)

@David99999 wrote:

Would a channel writer/reader setup be better than a local variable for sharing data between loops?


Probably over a billion times better, but I didn't check the math on that.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 8 of 22
(3,081 Views)

My answer would be a reluctant yes. I haven't ever used a channel writer and don't really like how they look and feel, but they exist for this kind of thing.

 

A longer answer would be, as crossrulz said, think about better architecture. Without knowing what you are doing we can only give vague answers. Things like what you are doing with the data, how fast it updates, and your current architecture come into play if you want a better answer. Personally I like to have independent loops controlled by a queue (and optionally a notifier for return data) for any piece of hardware that is in my application. But, as the adage goes, more than one way to skin a cat. 

0 Kudos
Message 9 of 22
(3,077 Views)

@David99999 wrote:

But what if my code is starting to get messy and I want to reduce the amount of wires in a loop, so I do something like the basic example above. 


(You've already got good advice on why not do that, so I won't repeat)

 

Wires are not messy! Local variables are messy!

 

If your code is starting to get messy, the problem (and the solution!) is entirely elsewhere!!!

Message 10 of 22
(3,070 Views)