Eliminate Redundant Code by Combining State Actions
You can combine entry
, during
, and
exit
actions that execute the same tasks in a state.
By combining state actions that execute the same tasks, you eliminate redundant code. For example:
Separate Actions | Equivalent Combined Actions |
---|---|
entry: y = 0; y=y+1; during: y=y+1; |
entry: y = 0; entry, during: y=y+1; |
en: fcn1(); fcn2(); du: fcn1(); ex: fcn1(); |
en, du, ex: fcn1(); en: fcn2(); |
Combining state actions this way produces the same chart execution behavior (semantics) and generates the same code as the equivalent separate actions.
How to Combine State Actions
Combine a set of entry
, during
, and/or
exit
actions that perform the same task as a comma-separated list in a
state. Here is the syntax:
entry, during, exit: task1; task2;...taskN;
You can also use the equivalent abbreviations:
en, du, ex: task1; task2;...taskN;
Valid Combinations
You can use any combination of the three actions. For example, the following combinations are valid:
en
,du
:en
,ex
:du
,ex
:en
,du
,ex
:
You can combine actions in any order in the comma-separated list. For example,
en, du:
gives the same result as du, en:
.
Invalid Combinations
You cannot combine two or more actions of the same type. For example, the following combinations are invalid:
en
,en
:ex
,en
,ex
:du
,du
,ex
:
If you combine multiple actions of the same type, you receive a warning that the chart executes the action only once.
Order of Execution of Combined Actions
States execute combined actions in the same order as they execute separate actions:
Entry
actions first, from top to bottom in the order they appear in the stateDuring
actions second, from top to bottomExit
actions last, from top to bottom
The order in which you combine actions does not affect state execution behavior. For example:
Combined Actions | Order of Execution |
---|---|
|
|
|
|
|
|
|
|
Rules for Combining State Actions
Do not combine multiple actions of the same type.
Do not create data, events, or messages that have the same name as the action keywords:
entry
,en
,during
,du
,exit
,ex
.