Main Content

Overall Design Cyclomatic Complexity

Since R2022b

Metric ID

slcomp.OverallCyclomaticComplexity

Description

Use this metric to determine the overall design cyclomatic complexity for a unit or component.

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.

For example, the design cyclomatic complexity of an if-else statement is two because the if statement represents one decision and the else statement represents the default path. The default path is not included in the number of decisions because no decision is made to reach a default state.

function y = fcn(u)
    if u < 0
        % one decision
        y = -1*u;
    else
        % default path
        % zero decisions
        y = u;
    end
end

The overall design cyclomatic complexity metric counts the total number of execution paths in a unit or component. The default path is only counted once per unit or component.

The overall design cyclomatic complexity is equal to the sum of the:

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 has a minimum value of 1 because every design has at least one default execution path, even if no decisions are made within the design.

Collection

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

Results

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

Examples

Suppose your unit contains four Simulink® decisions, five Stateflow® decisions, and six MATLAB® decisions. The overall design cyclomatic complexity for your unit is 16 because the sum of four Simulink decisions, plus five Stateflow decisions, plus six MATLAB decisions, plus one default outcome equals 16.

See Also

Related Topics