84

What is the difference between the alt and opt fragments in UML sequence diagrams?

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
ettozyame
  • 851
  • 1
  • 6
  • 5
  • 3
    compare picture http://www.uml-diagrams.org/sequence-diagrams-combined-fragment.html#operator-alt and http://www.uml-diagrams.org/sequence-diagrams-combined-fragment.html#operator-opt – xmojmr Aug 11 '14 at 17:04

5 Answers5

148

alt is used to describe alternative scenarios of a workflow. Only one of the options will be executed.

opt is used to describe an optional step in the workflow.

For example, for an online shop purchase sequence diagram you may use opt to describe how the user can add gift wrapping if she wishes. alt may be used to describe two variants of payment: using credit card or wire money transfer.

example of alt and opt sections in the sequence diagram

In the UML specification the meaning is described in section 17.12.15. opt and alt are two different operator types and here's how the specification describes them:

alt:

The InteractionOperatorKind alt designates that the CombinedFragment represents a choice of behavior. At most one of the operands will be chosen. The chosen operand must have an explicit or implicit guard expression that evaluates to true at this point in the interaction. An implicit true guard is implied if the operand has no guard.

opt:

The InteractionOperatorKind opt designates that the CombinedFragment represents a choice of behavior where either the (sole) operand happens or nothing happens. An option is semantically equivalent to an alternative CombinedFragment where there is one operand with non-empty content and the second operand is empty.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
  • This is the correct answer. I'd like to emphasize (since there are other answers here saying sth different) that opt has only a single operand, so either something or nothing happens, whereas alt defines multiple alternative behaviors. From the UML specification: "The interactionOperator alt designates that the CombinedFragment represents a choice of behavior. At most one of the operands will be chosen." vs. "The interactionOperator opt designates that the CombinedFragment represents a choice of behavior where either the (sole) operand happens or nothing happens." -- note the *sole* – dsteinhoefel Jun 18 '18 at 08:46
  • Between the two most voted replies there is something missing. According to you, opt is "optional" according to the other it is just an "if". The word "optional" carries meaning, as in making a choice. A user can make a choice, a program cannot but it can do something similar enough like doing sth based on a parameter. But a program can also do things that look nothing like a choice at all like print a message in case of error (everything else being the same). Please someone clarify if it's an "if" and nothing more or if it carries meaning at all. – DPM Jun 04 '20 at 22:06
  • Sequence diagram describes a set of interactions sequences. `opt` fragment in diagram means that the diagram describes two possible interactions sequences - one with the interaction in the `opt` fragment and another without this interaction. The condition that describes may range from some deterministic calculation (`2+2==4`) to user choice (`red pill selected`) to random event (`atom decays`). It's up to diagram author what the intention is. – Roman-Stop RU aggression in UA Jun 05 '20 at 02:41
  • what software/ website did you use for specific sequence diagram? – deirdreamuel Mar 18 '22 at 16:29
  • 1
    @deirdreamuel I've used plantuml. Note that the default theme [has been changed](https://twitter.com/PlantUML/status/1491847768436060168) recently, so if you want the color scheme from the answer use `skin rose`. – Roman-Stop RU aggression in UA Mar 18 '22 at 16:55
  • In your opinion, opt is "not necessary", in the opinion of another, it's just "if". The expression "not necessarily" carries a meaning, as when choosing. The user can make a choice, the program can't, but it can do something similar enough, for example, execute "sth" based on a parameter. And the program can also do things that are not at all similar to the choice, for example, print a message in case of an error (everything else remains the same). Please clarify if this is an "if" and nothing more, or if it matters at all. – Protect children of Donbas2014 Apr 13 '22 at 09:09
26

Alt is alternative flow (SWITCH or if. IF with two paths) Opt is IF with one flow. If you use Opt, code will be executed or not !

Vladimir
  • 2,066
  • 15
  • 13
11

UML - Sequence diagram Alt vs Opt

alt - if else
opt - single condition

enter image description here

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
-2

Alt (alternative) is indeed similar to "if" but neither is limited to 2 options only, both can actually have many "else", many options, as needed. For example: //if (a=b) then a++; else if (a>b) then a--; else b++. In any case, if Alt is part of a code that is being called, Alt will execute.

Whereas Opt (Optional) not necessarily will be executed even if it is contained in the middle of a sequence or code that is being called. Typically, to execute an Opt sequence requires external interaction from user that is making a decision where many options were presented (like choosing an online paying method). In this particular case, as opposed to an Alt sequence, if the paying methods were "credit card", "pay pal" or "prepaid", the code will have 3 Opt sequences - with only one flow each -, but one and only one of the Opt sequences will actually execute.

Hope this helps!!

Best, SD

SofiaD
  • 7
-5

They are basically the same.

alt is more used for several choices, like a switch sentence group in C programming language. While opt is more used for only two choices, like a if sentence.

But, don't get surprised, if you see both concepts used interchangeably.

umlcat
  • 4,091
  • 3
  • 19
  • 29
  • wow thanks i never thought so, it helps a bit but i have to see it in a real case :D – ettozyame Feb 22 '14 at 07:58
  • 6
    That's not true! ALT means alternative, while OPT means optional (see @Roman Konoval answer)... – ufo Sep 08 '14 at 20:09
  • Downvoted since this is really not true, opt means a sole alternative that may be executed or not, alt means multiple alternatives of which exactly one will be executed. See my comment to the first answer, cites the UML specs. – dsteinhoefel Jun 18 '18 at 08:51