Main Content

Halstead Difficulty

Metric ID



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. One of the Halstead complexity metrics is Halstead difficulty. Halstead difficulty is an estimate of how difficult the code is to read and write, calculated based on the number of total and unique operators and operands in the design.

Use this metric to calculate the overall Halstead difficulty of MATLAB® code, Stateflow® charts, and Simulink® designs.

The overall Halstead difficulty is equal to the sum of the:

Computation Details

The metric:

  • Ignores control-flow complexity

  • Ignores branches and jumps


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

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


The metric returns results for each unit and component in the project.

For each unit and component, the metric returns a metric.Result object with properties including:

  • Value — Overall Halstead complexity for the unit or component

  • Scope — Structure with information about the unit or component

See Also

| | |

Related Topics