VI Scripting Pattern

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 VI's end up hard to read.

Even well organized scripting VI’s 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.

VI Scripting Pattern

To use this VI snippet, click once to show the full image before dragging it.

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 update including the VI Scripting Pattern is not yet out on the LabVIEW Tools Network, so for now either use the LabVIEW 2012 snippet above, or download the LabVIEW 2009 VI.