Main Content

Stateflow Halstead Difficulty Distribution

R2026b
Since R2026b

Metric ID

modeldesign.StateflowMaintainability[slcomp.HalsteadDifficultyDistribution]

Description

This metric returns a histogram showing how many Stateflow® design layers in each unit fall into each Halstead difficulty range. Halstead difficulty measures how error-prone a design is based on the number of distinct operators and operands. Higher values indicate designs that are more difficult to understand and maintain. Use this metric to identify units where Stateflow design layers may benefit from refactoring.

Supported Artifacts

This metric collects metric results for Units in the project. To control what the dashboard classifies as a unit, 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 Difficulty Formula

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 with the metric ID.

metric_engine = metric.Engine;
results = execute(metric_engine,"modeldesign.StateflowMaintainability[slcomp.HalsteadDifficultyDistribution]")

View the metric result values for a specific unit.

results(1).Value
results(1).ScopeAlias

You can also collect all Stateflow maintainability metrics at once by using the group ID.

results = execute(metric_engine,"modeldesign.StateflowMaintainability[]")
Use the MetricID property of each result to identify which metric the result belongs to.

Results

The metric returns an array of metric.Result objects, one for each unit in the project. Use the Value property of the metric result object to view the metric result values. Use the ScopeId and ScopeAlias properties to identify which unit the result belongs to.

The ScopeAlias property returns the name of the unit.

The Value property returns an array of structures representing a histogram. Each structure has these fields:

  • BinEdge — The Halstead difficulty range for the bin, such as '[0,5)' or '≥50'

  • BinCount — The number of Stateflow design layers in the unit whose Halstead difficulty falls within this range

See Also

Topics