Contenu principal

Stateflow Halstead Complexity

R2026b
Since R2024a

Metric ID

slcomp.StateflowHalsteadComplexity

Description

The Halstead complexity metrics measure the size and complexity of code, which can help you monitor code quality, identify complex areas in the design, and address software maintainability concerns.

Use this metric to count the operators and operands in Stateflow® charts and calculate design volume and difficulty using the Halstead complexity metrics.

This metric supports Stateflow charts that use MATLAB® as the action language. Charts that use C action language are not supported and do not contribute to metric results.

Supported Artifacts

You can collect this metric for Units and Components. To control what the dashboard classifies as a unit or component, see Categorize Models in Hierarchy as Components or Units.

Computation Details

Operators and Operands

An operator specifies an action that influences the content of a variable. An operand is the basic logical unit that the operator acts on.

The metric:

  • Uses the following definitions:

    • For Stateflow charts, the metric counts these operands:

      • Boxes

      • Condition Actions

      • Entry Ports

      • Events

      • Exit Ports

      • Graphical functions (Stateflow.Function)

      • History Junctions

      • Junctions

      • Simulink® functions

      • State During Actions

      • State Entry Actions

      • State Exit Actions

      • State Function Bindings

      • State Moore Actions

      • Subcharts

      • Transition Actions

      • Transition Conditions

      • Transitions

      • Triggers

      • Truth Tables

      For Stateflow truth tables, the metric counts these operands:

      • Actions in the Action column of the Action Table

      • Conditions in the Condition column of the Condition Table, plus any actions that the Condition Table calls from the Action Table

      • Decisions in the decision columns

    • The metric counts MATLAB operators, including:

    • The Stateflow operands that the metric counts include:

      • Atomic subcharts (Stateflow.AtomicSubchart)

      • Data in the Symbols pane (Stateflow.Data)

      • Events in the Symbols pane (Stateflow.Event)

      • Simulink based states (Stateflow.SimulinkBasedState)

      • States (Stateflow.State)

    • The metric counts constants and variables as MATLAB operands.

      Note

      Due to a limitation of static code analysis, the metric also counts calls to externally defined functions as MATLAB operands. The metric uses static analysis to analyze the code without executing the code. With static analysis, the metric cannot distinguish between a variable and a call to a function that you define in a separate function file.

  • Ignores objects that you comment out.

  • Ignores unresolved symbols.

  • Ignores empty lines and empty columns in the Condition Table and Action Table.

  • Ignores Stateflow states and transitions that have syntax errors.

Halstead Volume

The metric calculates the Halstead volume, V, using the equation V=(N1+N2)×log2(n1+n2). N1 is the number of total operators, N2 is the total number of operands, n1 is the number of unique operators, and n2 is the number of unique operands.

Halstead Difficulty

The metric calculates the Halstead difficulty, D, using the equation D=n12×N2n2. n1 is the number of unique operators, N2 is the total number of operands, and n2 is the number of unique operands.

Collection

To collect data for this metric, execute the metric engine and use getMetrics with the metric ID slcomp.StateflowHalsteadComplexity.

metric_engine = metric.Engine;
execute(metric_engine,"slcomp.StateflowHalsteadComplexity");
results = getMetrics(metric_engine,"slcomp.StateflowHalsteadComplexity")

Results

This metric returns metric results for each Stateflow design layer in the units and components in your project. A Stateflow design layer is an artifact, like a Stateflow chart, state, or truth table, that has its own canvas in Stateflow.

For each Stateflow design layer, the metric returns a metric.Result object with properties including:

  • Value — Structure with fields for:

    • TotalOperators — Number of Stateflow operators and MATLAB operators

    • TotalOperatorsSF — Number of Stateflow operators

    • TotalOperatorsML — Number of MATLAB operators

    • UniqueOperators — Number of unique Stateflow operators and MATLAB operators

    • UniqueOperatorsSF — Number of unique Stateflow operators

    • UniqueOperatorsML — Number of unique MATLAB operators

    • TotalOperands — Number of Stateflow operands and MATLAB operands

    • TotalOperandsSF — Number of Stateflow operands

    • TotalOperandsML — Number of MATLAB operands

    • UniqueOperands — Sum of unique Stateflow operands, UniqueOperandsSF, and unique MATLAB operands, UniqueOperandsML

    • UniqueOperandsSF — Each operand in Stateflow is unique, so UniqueOperandsSF is equal to TotalOperandsSF

    • UniqueOperandsML — Number of unique MATLAB operands

    • Volume — Size of the design layer

    • Difficulty — Estimate of how difficult the design layer is to read and write

  • Scope — Structure with information about the design layer

For information on how the metric calculates the metric results, see Computation Details.

References

[1] Halstead, Maurice H. Elements of Software Science. New York: North Holland, 1979.

[2] Stürmer, Ingo, Hartmut Pohlheim, and Thorsten Rogier. 2010. "Berechnung und Visualisierung der Modellkomplexität bei der modellbasierten Entwicklung sicherheitsrelevanter Software." In Automotive - Safety & Security, edited by B. Keller et al., 69-82. Aachen: Shaker Verlag.

[3] Yu, Sheng, and Shijie Zhou. “A Survey on Metric of Software Complexity.” In 2010 2nd IEEE International Conference on Information Management and Engineering, 352–56, 2010. https://doi.org/10.1109/ICIME.2010.5477581.

See Also

Objects

Functions

Topics