BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Why some people say Local Variables are bad...

 

Example of both Local and Global variable abuse:

 

http://forums.ni.com/t5/LabVIEW/Downconvert-VI-Requests/m-p/1522520#M567682

 

small snapshot:

 

Since this is inside a While Loop, using wires and shift registers is the way to go.

 

 

Obviously misunderstanding text-based Global variable declaration with LabVIEW's Global..

 

 

Download All
Message 241 of 396
(8,997 Views)

I hope this hasn't been posted before.

Someone actually put this up on their commercial site.

 

yeesh.png

 

 

Richard






Message 242 of 396
(8,975 Views)

Using Locals where a wire you do the trick, and shift registers would provide dataflow (hint: it's inside a loop).

 

0 Kudos
Message 243 of 396
(8,918 Views)

Ray, do you have a link where you saw that?

 

There are other glaring mistakes, for example a loop containing an event structure inside a sequence frame. This could be disastrous unless the code is very carefully constructed. There is also no error handling where it is most important: at the port configuration.

0 Kudos
Message 244 of 396
(8,916 Views)
0 Kudos
Message 245 of 396
(8,899 Views)

I do not know if I'm allowed to post this, because it is from the LabVIEW Design Guidelines from National Instruments.  I think those guidelines are available on this website, so I will post it anyway..

 

This is why I created this thread...  Unfortunately, it seems that most people do not follow the recommendation to avoid abuse of Local Variables.  They are not inherintely bad if/when used correctly, but as you can see through all the posted examples, people do abuse them too often..

 

From NI Guidelines:

 

"Avoid using local variables when you can use a wire to transfer data. Every local variable that reads the data makes a copy of the data. Use global and local variables as sparingly as possible.


  • You can use global and local variables to write VIs efficiently, but use global and local variables as sparingly as possible. If you misuse or abuse global and local variables, particularly with array data types, the memory usage of the VI increases and the performance is affected.

 

  • Also, you can encounter race conditions when reading from and writing to global variables in the same application.  Race conditions are difficult to debug because there is no data dependency between different instances of the same global variable on the block diagram.

 

  • Consider using functional global variables instead of global variables. Functional global variables do not create extra copies of data and allow certain actions, such as initialize, read, write, and empty. They also eliminate race conditions."
Message 246 of 396
(8,871 Views)

That's a short and good description Ray/NI as to how to use Locals. The 3rd point isn't completely true though, but if create a AE/FGV with error wires (and use them) you can make it race secure ... assuming you dont use it in several threads/loops in which case it might still be an issue.

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 247 of 396
(8,817 Views)

 


@Yamaeda wrote:

 assuming you dont use it in several threads/loops in which case it might still be an issue.


 

That's why it's in there...  😉

 

I am looking at code which has 3 loops running in parallel, but they each have a flat sequence structure around them and shared variables with the error cluster wired through.  But they read & write in parallel!!!!!

 

 

0 Kudos
Message 248 of 396
(8,793 Views)

Included all code that fit on my monitor.

 

seen here.

 

Message 249 of 396
(8,775 Views)

I wish I could post an image, but if I did you'd need a telescope to look at it since the designer apparently had the same size monitor used to track the shuttle's orbit over the earth. The code seems to have more local variables than actual code. You can see the glory here: http://forums.ni.com/t5/LabVIEW/Downconvert-VI-Requests/m-p/1569398#M576151

Message 250 of 396
(8,720 Views)