Published May 16, 2013

Do you use VI Scripting to programmatically modify VI’s in your development environment? If you do, you’ve probably noticed what I have, that scripting block diagrams tend to end up looking like spaghetti code written by less experienced programmers.

Even well organized scripting VIs end up looking like spaghetti code and become hard to read.

I think the problem is we haven’t worked out the best practices for scripting. MGI has come up with a pattern that we think makes sense. It’s being provided as a single merge VI on the palette of the Application Control package in the MGI Library. The code that drops on your block diagram is shown below.

If you analyze it, you’ll see that we’re basically just reinventing the stacked sequence structure while addressing some of its limitations. The enumerator at top left provides descriptions of each step of our scripting process, allowing us to select cases using descriptive names and providing error reporting information about where we are in our scripting process when an error occurs. The cluster at the left allows us to use visible, descriptive parameter names for inputs and outputs of our steps rather than the tangle of wires that scripting normally produces. At top right, errors in each step are checked, stopping the sequence if one is found. If there are no errors, the loop automatically continues until the last case executes and the enumerator loops back to zero. To the right of the loop, all the references left open to provide parameters for subsequent steps are closed, even if the loop ends early because of an error.

We think this pattern improves the readability and maintainability of our scripting code. It’s also useful for other highly sequential code, but only where the wiring complexity is high, since otherwise it’s better to use subVI’s or just spread the code out horizontally.

*The VI Scripting Pattern is now on the NI Tools Network, grab the MGI Library and look in the Application Control subpalette for the Merge VI.