LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

create local or global variables ?!

hey guys,

I want to use variables instead of wires , which one you suggest , local or global variables? and why ? 

thnxxx alot

0 Kudos
Message 1 of 10
(13,119 Views)

I personally try not to use either. if I need to I will use the Local Variables and do it at a bare minimum in my code. instead of Global variables I create Functional Global variables and use them instead of the LV supplied Global Variables

 

this might help:

http://zone.ni.com/devzone/cda/epd/p/id/3124

 

Jeff D.

Certified Architect LabVIEW Champion DQMH Framework

Message 2 of 10
(13,112 Views)
Neither.  They lead to race conditions and less optimized code.
Message 3 of 10
(13,111 Views)
In agreement with Jeff and Matthew I try to avoid any use of Local or Global Variables.  If you necessarily have to use on or the other, I suggest Local Variables.  I find them to be a bit more friendly, but watch out for race conditions that will more than likely occur.
Message 4 of 10
(13,103 Views)
I'll make it unanimous. It's never a good idea ro replace wires with locals. If you have a messy block diagram, you need to clean it up. Locals will just make a bad situation worse.
Message 5 of 10
(13,094 Views)

If you want to create a lot of trouble for yourself and any programmer who maintains your code, then you should use many local and global variables.

If you want to create code that's efficient and easily maintained, use wires.  You can of course, write horrible (messy) code with wires too.  But at least you won't waste time troubleshooting "race" conditions that are nearly impossible to find.

There are many style guides and other resources on this website.  Look for them, and spend a little time planning your code.  Your code will be cleaner, run faster, and easier to fix.

I've written LabVIEW code for about 10 years and never used a global variable.  I use "functional globals" defined in state machines with controlled access.  Sometimes locals are unavoidable and sometimes globals are useful.  That's why LabVIEW has them.  But they should be used as part of an overall plan.  "Planning to use globals or locals" is not a plan by my definition.

Message 6 of 10
(13,086 Views)


blue silver wrote:
I want to use variables instead of wires , which one you suggest , local or global variables? and why ? 

In LabVIEW, the wire is the variable.
 
Local and global variables have their limited use in certain UI tasks, but lead to inefficient, error prone, and hard to maintain code.
  • Local variables create extra data copies in memory, which can be expensive
  • They break dataflow and thus lead to race conditions.
  • To "fix" the race conditions, you need to clutter your code with sequences.
  • ....

... it's an evil cycle!. 🙂

Why do you think you need local and global variables?

If you "want" to use them, you are probably misguided. Typically, you should only use them if you "need" to! 😄 There are legitimate uses for them.

Message 7 of 10
(13,070 Views)

Here is a way to have local variables in wires.  Use a cluster to hold all your local variables, then use Unbundle to read them and Bundle to write to them.



Message Edited by tbob on 11-13-2007 10:05 AM
- tbob

Inventor of the WORM Global
Message 8 of 10
(13,067 Views)

Tbob,

This is a useful techniques, but you should not call any of it "local variables". That term is already taken! 😄

Message 9 of 10
(13,058 Views)
tbob:

That'll help me clean up a few diagrams quite a bit. Thanks!
Message 10 of 10
(13,022 Views)