LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Community Nugget 04/23/2007

Allow me to start with a preemptive response from a random user:
Globals are evil!
They are the worst thing ever to have been created. They will make your hair fall out and do bad things (TM) to you!
 
Many people feel this way because globals are dangerous. It is very easy to use them, but they don't have the protection needed for the parallel nature of LabVIEW and so they can easily cause bugs.
They also prevent proper seperation of code modules.
 
They do have their advantages, though, and now that warning is out of the way I want to present a use case for which globals are currently relatively appropriate.
 
 
Text based languages usually have the concept of logical constants. These are similar to variables, except you only set their value once when you write the code.

A common example is declaring Const PI=3.14 and then using the text PI instead of 3.14 throughout the rest of the code. That way, the code is much easier to read and if you wanted to have more accuracy, you would just need to change the value in a single place.
LabVIEW already has a PI constant, but this could apply to any other constant you need.
 
Another example would be declaring Const Delimiter = ";" in a system where you have to generate delimited strings. If the delimiter changes (for example, if you have to merge your code with an already existing program which uses another value), you simply change the value of the constant once.

Other examples include using constants for display strings and localized strings.
 
 
LabVIEW does not currently have constants, but you can do this by creating a global variable and never writing to it.

The process for doing this is:
  1. Create a global VI. Make sure it has a properly identifying name and icon (for example, MyProject_Constants_Display Strings.vi). Preferably, it should be used only to hold constants.
    Also, be sure to create as many of these as necessary, so that you can copy and move your code around later, if needed (since global VIs are part of the hierarchy).
  2. In the global VI, create a control of the type you need. You can append or prepend "CONST" to its label, to avoid mistakingly writing to it.
  3. Set its value and then right click it and select Make Current Value Default. This part is critical because that sets the value of your constant. I place a prominent free text in the FP of the global VI alerting me to remember this step when I modify values.
  4. Save the global VI.
  5. You can now simply use the global variable and only read from it without ever writing to it.
 
To be honest, this is far from an optimal solution. The 3 main disadvantages are that
  • a global can still be written to,
  • you have to remember to set the default value (those two can cause bugs) and
  • adding CONST to the label makes the constant name unnecessarily long.

If your code needs to be secure, then this would be a security hole and not usable at all.

 
Personally, I think that these could all be solved with the addition of a Global Constant VI to LabVIEW which will be similar to a global VI, except it would have a different icon in the terminal, it would accept the value changes without needing to set the values to default and it would be read only.

Also, not that it usually matters that much, but LV should be able to constant-fold it.
 
Another optional implementation - allow the VI to have a block diagram and make sure it executes first (although that would make it much more complicated and is probably unnecessary).
 
That's my wishlist item for this thread.
 
 
P.S.
There are other ways to create constants today, but they are more cumbersome. For example, you can create a VI for each constant or you can create a single VI with an enum or a string which will select the constant you like, but they all require a lot of overhead.
 
For a list of past nuggets, see here.
If you want to write a nugget yourself, see here.

___________________
Try to take over the world!
Message 1 of 39
(12,071 Views)
Hi tst, thanks for writing a nugget for this week 🙂

I'm not sure if I missed a point but could you please specify what's the advantage of using globals as constants over to using subVIs as constants. I've been using the latter and I think it's pretty easy and gives the added value that you can use conditional disable diagram structure to make the constant platform dependent. This can be advantageous for example if your decimal point format changes from platform to another.

p.s. Those who are interested in running LabVIEW on top of virtual machine to make development more flexible, please check out my today's blog. A link is provided in this other thread.

Tomi
--
Tomi Maila
0 Kudos
Message 2 of 39
(12,039 Views)
I also usually use a sub VI to declare a constant. However I've never used a lot of them in one program, so I see how this could become cumbersome. I was just thinking about this not too long ago, how there should be a global constant in LV. I would definitely use that all the time.
0 Kudos
Message 3 of 39
(12,027 Views)
Interesting read.  It is making me question my globals.  In my current project I am using globals to store the destination printer and the printer settings.  I init them from an INI on startup and save them to the INI on exit.  Is there a better way to do this?


Cheers,
Greg Cole
“It is not the ship so much as the skillful sailing that ensures the prosperous voyage.”
0 Kudos
Message 4 of 39
(12,023 Views)

For constants I use the same approach as toni.  A single subvi that returns a constant.  This provides a level of protection against writing to the global anywhere in the code.  Here you go into the subvi and change the constant that the output is wired to.  You have a single instance to change to fix the entire codes constants.  Another adcvantage I have found with using a subvi as a constant is to allow for an informative icon (such as pi) to indicate the constants use.  I have another type of constant vi I use for allowing "pre-binding" the constant value.   Here the vi will look in the default ini file for a key in the section constants->constant name and initialize a shift register with the new constant value in a file.  If the file or value doesn't exist, a default value is used.  A good case for this is a constant called simulate in a daq system.  The constant is by default false, but when I debug the system I just add the flag simulate = 'true' and my "constant" will change on the next execution.  I still avoide globals but admire there ease of use.  This has always been a hotly contested topic.

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 5 of 39
(12,012 Views)
The great advantage of subVI constants over other types of constants is that you can later change the implementation to use something like configuration file without needing to modify all the code using the constant.

Tomi
--
Tomi Maila
0 Kudos
Message 6 of 39
(11,997 Views)

Tomi, the main issue with a VI is that you have to create a file and icon for each VI. If you have one or two, that's easy, but if you have a lot, it would be hard.

As you mentioned, a big advantage of using VIs as constants is that they can have code in them, so your constant can have complex conditions (and it would be much harder for a user to misuse them).

Greg, if you can gurantee that the read happens before anything else happen (and if you can handle corner cases like if the file is not found) and your global is only written to in one place, you should be fine. Since you say you save the global, I assume that means the value is written to from more than one place and you might have race conditions. In any case, the case you mention is different from I refered to, since constants are defined in the code, not in configuration files.


___________________
Try to take over the world!
0 Kudos
Message 7 of 39
(11,997 Views)
Greg, the subvi structure will make sure you do read the ini files correctly.  On first call initialization a shift register to the constant value.  If the ini file fails, either return an error or return a constant default value (in your case return not a path and let the caller handle this).  I think the biggest advantage to using a subvi is that it is not possible that a user of this constant (ie members of your programming team, someone who inherit the code or myself many years from now) can not change this constant programatically.
 
Paul
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 8 of 39
(11,985 Views)
Hi tst,

Nice Nugget.....for me I always stay away from Global last time it took me 2 days to find out that it was global that I used that made my application
not to function properly.I think I have to work on how to use Global, just as you said in this nugget......

Keep the Nuggets coming.

CLAD
Certified LabVIEW Architect
Certified TestStand Architect
0 Kudos
Message 9 of 39
(11,967 Views)

Good Nugget Topic tst!

I'm still in the sub-VI camp on this topic because they have a diagram which gives more more flexibility. They also can tolerate type definitions changing without loosing the desired values.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 10 of 39
(11,965 Views)