What is the benefit of defining enumerators as a DUT?

Viewed 355

The main goal of defining enumerators is to assign a variable to some numbers and their equal strings as I understand.

We can define var a as an enum everywhere in the initializing section of our Program or Function Block like this:

a:(start,stop,operate);

tough I don't know why we can't see that in tabular view but there there is a big question that:

What is the benefit of defining enumerators as a DUT?

4 Answers

There are 3 main benefits for me:

  1. You can use the same enum in multiples function blocks
  2. You can use TO_STRING on enums declared as DUTs (After enabling it with {attribute 'to_string'} Infosys
  3. You can use refactoring on names of each component, which is impossible with local enums

When defining an enum as a DUT it is available everywhere in your code (global scope). This is helpful in many cases, but in general it is not good programming practice to have a lot of stuff available in the global scope.

Here is a bit elaboration on the topic.

In addition to the above, one benefit is that if you are using an enumeration for something like FB states, you will be able to see the descriptive status name when the program is running (READING, WRITING, WAITING, ERROR, etc.).

You can see it in the variable declarations section, in-line with your code, or in the watch window. You don’t have to remember what status number was defined in your state machine.

This benefit comes with local enumerations or DUT (global) enumerations.

In addition to other good points already made, there is another big advantage to enumerations : you can use the enumeration as the type of a variable, and when you do that, the compiler will (if {attribute 'strict'} is used in the enumeration declaration, which it probably should) refuse an assignment to that variable of a value that is not allowed by the enumeration.

In other words, you get rid of a whole class of failure modes where the variable ends up having an invalid value due to some coding mistake the compiler cannot catch.

It takes a trivial amount of time to create an enumeration, and it has benefits on many levels. I would say the real question is why not use them whenever a fixed list of meaningful values needs to be expressed.

Related