UML Sequence Diagram is one of the interactive graphs Understand can draw of your code — call trees, dependencies, control flow, and more.
UML Sequence Diagram
Languages: Ada, C#, C++, Java, Pascal, PythonTargets: Functions, Subprograms
Variants: Classic, Compare
Show a UML sequence diagram of the calls a selected function, method, procedure, or task makes, in the order they occur.
Each column is an actor — the class, file, or other scope a called entity
belongs to — with a vertical lifeline running down it. A solid arrow is a
call, landing on an activation box on the callee's lifeline; a dashed arrow
returning to the caller is labeled with the callee's return type.
An if, switch, or similar conditional is drawn as an
alt combined fragment labeled with its condition, with each branch
as its own section inside; a for, while, or
do while loop is drawn the same way as a loop fragment
labeled with its header. These fragments can nest to any depth, following the
actual control flow of the function.
Rooted at handle below, the leftmost column is sequence.cpp
(the file handle is defined in, since it isn't itself a member of a class),
followed by a column for each struct it calls into: Logger,
Metrics, Cache, and Store. The calls to
log.info and metrics.record happen unconditionally;
cache.has(id) is both the condition of the first alt
fragment and, since evaluating it is itself a call, the first message shown
inside it, followed by metrics.record and cache.get
when it's true. store.fetch happens next, followed by a second
alt fragment for val < 0 (log.error
and metrics.record), and a loop fragment for the
retry loop, which calls store.save and checks
cache.has(id) again in a nested alt before breaking.
Finally, log.info is called once more after the loop.
See the following code and corresponding graph
struct Cache { bool has(int id); int get(int id); };
struct Store { int fetch(int id); void save(int id, int val); };
struct Logger {
void info(const char* msg);
void error(const char* msg);
};
struct Metrics { void record(const char* key, int val); };
int handle(int id, Cache& cache, Store& store,
Logger& log, Metrics& metrics) {
log.info("handling");
metrics.record("requests", 1);
if (cache.has(id)) {
metrics.record("hits", 1);
return cache.get(id);
}
int val = store.fetch(id);
if (val < 0) {
log.error("fetch failed");
metrics.record("errors", 1);
return -1;
}
for (int retry = 0; retry < 3; ++retry) {
store.save(id, val);
if (cache.has(id))
break;
}
log.info("done");
return val;
}
UML Sequence Diagram - Classic
UML Sequence Diagram - Compare
The Compare variant shows how the graph has changed relative to the project's comparison database.