JKI Blog

#4 Best Practice for using the JKI State Machine: Use macros instead of “chaining” together sequential states

Feb 18, 2015 8:30:00 AM Published by Jim Kring Leave a comment

In my last post on JKI State Machine Best Practices, I explained the #3 Best Practice: Keep the Original Size (i.e. don’t grow the structures).  In this post, I'll tell you which best practice came in fourth place.

The #4 Best Practice: Use macros instead of “chaining” together sequential states

Here's what we mean:

A "Macro" is a state that simply builds up a list of other states; calling the macro is like calling all the other states in the list.

2015-02-13_10-58-17

Note: There's no actual work done in a "Macro" frame/state of the Case Structure. We simply add a sequence of states to the front of the queue.

Example of Good Practice: Using a Macro to Invoke Three States

Here's an example of a macro that calls three states, sequentially:

2015-02-13_11-14-06

Macros make it easy to understand the flow of the code; it's obvious that a call to a macro will execute a sequence of states, and it's easy to see what that sequence of states will be.

Example of Bad Practice: Chaining Together States

Here is an example of "chaining" together states to create a sequence, where each state in the sequence call the subsequent state:

2015-02-13_11-06-38

This is much less desirable than a macro, because:

a) It's not obvious that a call to a state my result in a call to the chained state -- In the above example, it may not be obvious that a call to "State 1" will result in a call to "State 2" and then a call to "State 3"

2015-02-13_11-25-47

 

b) It is much more difficult to reuse states/code in other sequential operations, because each state in the chained sequence is tightly coupled to the sequence. For example, it is impossible to call "State 2" in the above example without it invoking "State 3".

It's hopefully clear, now, why macros are so useful and why chaining states may get us into trouble.

Looking ahead, there may be cases where you need to build up a macro dynamically/conditionally, depending on the state of system. And, you might not have that information at the time when you construct the macro. In these cases, there are some special techniques we can use that we'll talk about that in a future blog post. Stay tuned!

We'd like to hear about your best practices for using the JKI State Machine. Please post comments and suggestions, below.  And, if you have ideas for making the JKI State Machine better, please post them to the JKI State Machine GitHub Issue Tracker.

And a note from our sponsor: At JKI we use the JKI State Machine every day to help our our clients get their innovative high-tech products and technology to market fast. If you need help taking your system software to the next level or reach an important milestone, contact us to learn more about our services.

Tags: best practices, JKI State Machine, LabVIEW, LabVIEW tip, JKI State Machine, state machine

Subscribe to Email Updates

Recent Posts

Posts by Topic

see all