Main Content

Simulink Design Cyclomatic Complexity

Since R2022b

Metric ID

slcomp.SLCyclomaticComplexity

Description

The design cyclomatic complexity is the number of possible execution paths through a design. In general, the more paths there are through a design, the more complex the design is. When you keep the design cyclomatic complexity low, the design typically is easier to read, maintain, and test. The design cyclomatic complexity is calculated as the number of decision paths plus one. The metric adds one to account for the execution path represented by the default path. The design cyclomatic complexity includes the default path because the metric identifies each possible outcome from an execution path, including the default outcome.

Use the Simulink® design cyclomatic complexity metric to determine the design cyclomatic complexity of the Simulink model components in your design.

This metric counts the total number of Simulink-based execution paths through a unit or component. The number of execution paths is calculated as the number of Simulink decisions, Simulink Decision Count, plus one for the default path. The default path is only counted once per unit or component.

To see the number of Simulink decisions associated with different block types, see Decision Counts for Common Block Types.

Supported Artifacts

You can collect this metric for:

To control what the dashboard classifies as a unit or component, see Categorize Models in Hierarchy as Components or Units.

Computation Details

The metric does not exclude decisions associated with short-circuiting logical operations because Simulink runs a block regardless of whether or not a previous input alone determined the block output.

For example, in Simulink an AND block with repeating inputs runs regardless of whether or not the previous input alone determined the block output.

Collection

To collect data for this metric, use getMetrics with the metric identifier slcomp.SLCyclomaticComplexity.

Results

For this metric, instances of metric.Result return Value as the Simulink design cyclomatic complexity of a unit or component.

Examples

Suppose you have a unit that contains only an Abs block with an input u.

The Abs block can be treated as an if-else statement:

  • If the input to the Abs block is less than zero, the output of the Abs block is the input multiplied by negative one.

  • Otherwise, by default, the output of the Abs block is equal to the input of the Abs block.

if (input < 0)
  % one decision
  output = -1*input;
else
  % default path
  % zero decisions
  output = input;
end

For an if-else statement, the number of decisions is one because the if statement represents one decision and the else statement represents the default behavior. The execution path follows the default path if no decisions are made.

In this example, the number of Simulink decisions is 1 and therefore the Simulink design cyclomatic complexity of the unit is 2.

The value of the Simulink design cyclomatic complexity represents the two possible execution paths through the unit, either:

  • u < 0 and the output of the Abs block is the input multiplied by negative 1

  • u ≥ 0 and the output of the Abs block is equal to the input of the Abs block

See Also

Related Topics