Reduce, Reuse, Recycle Your Code: LabVIEW Rookie Mistake No. 3

Publish Date: Oct 20, 2014 | 4 Ratings | 4.50 out of 5 | Print | 1 Customer Review | Submit your review


LabVIEW rookie mistake number three is ignoring code modularity. Reuse, reduce, recycle—this is the mantra used by recycling agencies to encourage consumers to save resources and protect the environment. You recycle your plastic, paper, and glass to avoid unnecessary waste, so why don’t you recycle your LabVIEW code to avoid wasting time? By creating modular code, you’ll set yourself up to reuse, reduce, and recycle code and save future development time.


Figure 1. You recycle your plastic, paper, and glass, why not your LabVIEW code?


Reuse: Taking Advantage of Code Modularity

Have you ever written a LabVIEW program from scratch? No, not just starting from a blank VI. We are talking about building the entire application, including subVIs, using only primitives and functions included with LabVIEW, with no help from instrument drivers, APIs, example code, or past projects. It is possible to do this, and may even be a useful exercise in understanding every level of your application, but it’s usually more practical to start by reusing building blocks of existing code.

Separating code into modules, or building blocks, is what allows us to efficiently reuse code. Modular programming is defined by Wikipedia as “a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.” More simply, modular programming in LabVIEW is designing your subVIs with the mentality that they can be reused like LEGO® bricks.


Figure 2. Design your subVIs with the mentality that they can be reused like LEGO® bricks. "Lego Color Bricks" by Alan Chia - Lego Color Bricks. Licensed under CC BY-SA 2.0 via Wikimedia Commons.


Figure 3 shows an example that you might want to reuse in future applications. Set Enable State on Multiple Controls is a VI that allows you to wire an array of front panel control references and then enable or disable the entire lot. Programmatically enabling and disabling controls is often required functionality on the Certified LabVIEW Developer (CLD) exam and is a great practice to make more intuitive UIs. This code also is well qualified for reuse because the scope of its inputs and outputs is limited to a specific task and not dependent on external references or architectures.

Figure 3. With this conveniently reusable code, you can easily enable or disable multiple controls.


You can conveniently reuse this subVI by dropping it into the block diagram of a new application you are working on. Now, how will you find utilities or libraries of components you’ve used before when developing new code? One simple way to organize past code for reuse is to save copies in a “Shared” folder in your LabVIEW Data directory. Additionally, for commonly used code, you can create a palette to quickly access specific subVIs using the functions palette or Quick Drop.


Reduce: Convert Top-Level Code to SubVIs

Though important, modular programming does not happen on its own. It requires intentionality on the part of the developer to benefit from it. Reducing your top-level architecture to functional blocks helps you simplify development, aids in debugging, and increases the potential for reuse.

For example, if you were developing a driver to communicate with an oscilloscope, you might create perfectly functional code to configure and read a DC value. Though functional, this code would be difficult to reuse, hard to debug, and easy to mess up. Figure 4 shows what this code looks like after being reduced into subVIs—it’s easily readable and obvious what each component does. Additionally, only constants that are relevant to the user are exposed on the top-level VI, which prevents you from messing up a component you’ve already developed and tested.

Figure 4. Oscilloscope Driver Code After Being Reduced to Modular SubVIs


You can easily create a subVI by selecting the section of code you want to put in a subVI and clicking Edit»Create SubVI. Instantly, LabVIEW creates a new VI, copies the selected code to the block diagram of the new VI, wires each input and output on the connector pane, and then replaces the original selected code with the new VI. As a word of caution, think carefully about which inputs and outputs you select to be wired to your subVI because it’s easy to end up with subVIs that are not adequately generic for reuse. After creating a new subVI, customize the icon and save the subVI with a descriptive name so it’s easy to reuse later in development.


Figure 5. Basic DAQ Application for Which Code Could Be Reduced to a SubVI


For example, the code in Figure 5 has a nifty cluster and Case structure that the user can programmatically choose to write to an ASCII, TDMS, or binary file. This code is cluttering the block diagram, and it might be convenient to reuse. Additionally, its scope is limited enough that it won’t depend on open references to resources in order to function correctly. After converting it to a subVI, it will be easy to reuse in other applications.

Sometimes functional sections of code span beyond an individual subVI—for example, a driver, class, or library. In these cases, choosing an appropriate way to save and document your code, such as using a library file (.llb), packed project library (.lvlibp), or VI Package (.vip) makes it easier for you to reuse and share. If you have a design pattern that you commonly start from, you may want to create a Project Template to use as a starting point for future projects.


Recycle: Share With the Community

The beauty of recycling is that you can take material that you no longer need and turn it into something useful for someone else. With the NI Community, recycling is even more valuable—the code that you share can be used by thousands of other developers!

Posting example code on the NI Community is easy—all you need is a simple write-up and some attached code. To make your example the most useful for your fellow programmers, be sure to follow the Community Contributed Example Template. When saving, make sure to click File»Save for Previous Version and go a few LabVIEW releases back.


Figure 6. Find a useful community example here.


In addition to the good feeling of sharing your awesome code with hundreds of thousands of other LabVIEW users, you may get valuable feedback from the community on how you can improve your code. Additionally, while searching on the community, you may discover code that is useful for you to recycle in your own development.

In addition to the NI Community, you can choose from several other collections of valuable code to recycle in your own applications. OpenG Libraries is a package that installs hundreds of convenient utilities and functions developed and distributed by a community of expert developers. These VIs span all types of application areas and include string and array manipulation, application control, and error handling.


Figure 7. OpenG Libraries is a package that installs hundreds of useful VIs to your functions palette.


The OpenG Libraries package is distributed through the LabVIEW Tools Network, an app store that features over 250 free and paid third-party add-ons to help users expand the power of LabVIEW. Some add-ons are simple productivity tools like the LAVA Code Capture Tool, and others are advanced turnkey solutions for data logging or even wireless communications testing.

Another useful source of code to recycle is drivers for communicating with third-party instruments. By searching on NI’s Instrument Driver Network (IDNet), you can choose from more than 10,000 drivers to easily start automating or reading measurements from an instrument such as a Tektronix oscilloscope or Keithley digital multimeter. By leveraging an API created for interacting with your instrument, you can save a huge amount of time on developing your own instrument driver.

Hopefully now you feel confident in the three Rs of modular programming—reuse, reduce, and recycle. By being intentional in creating, maintaining, and using subVIs and code libraries, you can save yourself significant development time and avoid a common rookie mistake.

This article is part of the “Top 5 LabVIEW Rookie Mistakes” series. Subscribe to NI News to keep up with the series.


LEGO is a trademark of the LEGO Group.


Back to Top

Customer Reviews
1 Review | Submit your review

Helpful advice  - Oct 7, 2014

Having modular code helps out significantly. Works well if you happen to change hardware and something in the code interface is different, but that happens infrequently. Saves development time, but you also have to be somewhat organized in building libraries of code blocks. Document, comment internally, make the terminal names meaningful. It would be nice when an article is part of a series to have links to the previous and following articles.

Bookmark & Share


Rate this document

Answered Your Question?
Yes No