r/LabVIEW • Expert • 1d ago

No Follow-up Are inter-iteration error clusters an antipattern?

If you have a thing you're looping over, I've found it's usually a bug to put the error cluster in a shift register. Usually the things I iterate over are "the same" in some sense, so passing the previous loop's error state will usually trigger all kinds of silent failure. I find it's better to use an indexing tunnel, then merge errors after the loop completes (you can reduce allocations for large loops by adding a conditional on error status).

How do you handle these in your applications?

2 Upvotes

5 comments sorted by

6

u/SeasDiver Champion 1d ago

Depends on what is in the loop and why the error is being generated.

For example, looping on a Data Read, an error may indicate an unrecoverable fault so there is no reason to continue and a shift register may be the better option for the error cluster.

On the other hand, if you are iterating over writing safety values to multiple output instruments, even if one fails you still want to try and safeguard everything else so a shift register error is inappropriate in that case.

A well written program will examine the specific use case and use the appropriate technique. It is not one approach fits all circumstances. Better still is appropriate error handling inside the loop to appropriately respond to errors as they occur. I do tend to default towards merge error and indexing on for loops.

2

u/CarryTheBoat CLA/CTA 1d ago

It used to make a loop run faster for some reason, literally just error on a shift register vs error on a plain terminal. Not sure about an indexing tunnel.

For what you’re describing, most of the time I just conditionally stop or index like you described.

Which one depends on what is most important. Do I want to see all potential errors for all iterations in one go, is an iteration expensive and I want to stop as soon as possible, can it be parallelized, etc.

2

u/dzakich NI Employee 1d ago

Coming a bit late to the discussion, just wanted to add a bit more to the great answers provided. So ot isn't universally an antipattern; it comes down to loop semantics and error-management intent (complete-all, fail-fast, or recovery).

If your goal is independent iterations (complete-all), then a shift register here is a bug. Because standard subVIs typically wrap logic in an error case, one failure turns every subsequent iteration into a silent no-op (especially bad in reference-closing loops). A conditional indexing tunnel wired to error status is the right pattern, with some caveats, namely 0-iteration For Loop( silently drops) and multiple failures (merge Errors only keeps the first error)

If your goal is to fail-fast or stateful iterations, use a shift register with a conditional stop terminal. If iteration n+1 depends on iteration n (something like streaming over a single TCP connection, writing chunks to one file handle), you want to abort immediately to avoid corrupting state. A for loop with an error shift register and no conditional terminal is almost always a defect as it just spins through empty no-ops.

Now, if your aim is continuous recovery (SMs / QMH), then ashift register in a while s required so a fault in one state propagates to a centralized error-handling state on the next iteration, where it is logged, handled, and explicitly cleared before resuming

1

u/Unusual_Contest5778 1d ago

Depends on what is in the loop

  • if the execution takes long and it doesn‘t matter if every iteration is executed, use an conditional terminal and end the loop after the first error (often when initialsing things)
  • if the code doesn‘t execute when there‘s an error, then use shift registers (most common)
  • if the code must execute in every iteration, then use an auto indexing tunnel (clean up code) 

1

u/HarveysBackupAccount 1d ago edited 14h ago

Some of it depends on the code inside the loop - what the sub VI's do when they receive an error on the input.

Some VI's execute regardless, some don't execute if there's an error on their input. So, you have to make a choice each time.

Edit: one example where I always pass errors on the shift register is if e.g. the loop is a state machine and it has an "Error Handler" case that it goes to whenever there's an error on the line