When to Use Enumerated Data for Code Generation

You can use enumerated types to represent program states and to control program logic, especially when you restrict data to a predetermined set of values and refer to these values by name. You can sometimes achieve these goals by using integers or strings, however, enumerated types offer the following advantages:

  • More readable code than integers.

  • More robust error checking than integers or strings.

    For example, if you mistype the name of an element in the enumerated type, you get a compile-time error that the element does not belong to the set of allowable values.

  • More efficient code than strings.

    For example, comparisons of enumerated values execute faster than comparisons of strings.

Was this topic helpful?