LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

String vs Enum in a state machine

In a state machine, why do some people like to use string instead of enum to select state, such as the JKI state machine?  With string, isn't it easier to have a typo?  Also, with string, you can't make a type def either.  What is the pro and con of string and enum in the state machine?

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 6
(6,167 Views)

Search around the forums, this has been talked about before. But, here is a response with a couple of pros for using a string. I, myself, go straight to the enum because I like the add case for every value option in LabVIEW with regards to case structures, and not having to deal with finding typos in a string, especially ones caused by whitespace!

Message 2 of 6
(6,161 Views)

A long discussion on this topic took place in this nugget by Darren.

 

Felix

Message 3 of 6
(6,144 Views)

The only advantage I see to using a Ring or String control instead of an Enum is for re-use as a template.  The Enum type def contains the Strings[] and Values[] property and cannot be edited at run time.  On the other hand the Value or Text.text property of a String control (except for a Strict type def where Text.text is not writable) And the Strings[] and Values[] property of a ring control can be programmatically edited.

 

So if the state machine's strings were derived from a ring control  or read as a raw string we could develop indetical states for multiple state machines without co-dependande on a single type def.  Good for development- from a template (such as the JKI tool) - bad for practice.  If the list of states is discoverable before run time replacing the state selector with an enum type defis fairly trivial without changing the root template.

 

On the Other hand sometimes your state machine is called in a system that is configurable with different "valid states" that depend on the system and the exact subset is indeterminate during development.  A string selector is a simple way to provide state fitlering.

 

 


"Should be" isn't "Is" -Jay
Message 4 of 6
(6,126 Views)

In recent years there has been a movement away from enums toward strings because enums increase coupling, that is, they increase an objects dependency on external objects such as the enum in this case.  If modularity is a high priority then programmers will do almost anything to keep coupling low and using generic strings instead of enums is one trade-off they're willing to make.  Think about it, if all of your objects know how to create and parse a collection of well-defined strings, then it becomes very easy to connect any pair of them together.  

0 Kudos
Message 5 of 6
(2,917 Views)
Hi skinnedknuckles, this is an old post from 2011
Message 6 of 6
(2,911 Views)