### Metric ID

`sldesignlayer.SimulinkHalsteadComplexity`

### 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 Simulink® models and calculate model layer volume and difficulty.

### 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:

• Ignores blocks that you comment out.

The metric calculates the Halstead volume, `V`, using the equation $V=\left(N1+N2\right)×{\mathrm{log}}_{2}\left(n1+n2\right)$. `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.

The metric calculates the Halstead difficulty, `D`, using the equation $D=\frac{n1}{2}×\frac{N2}{n2}$. `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 `sldesignlayer.SimulinkHalsteadComplexity`.

```metric_engine = metric.Engine; execute(metric_engine,"sldesignlayer.SimulinkHalsteadComplexity"); results = getMetrics(metric_engine,"sldesignlayer.SimulinkHalsteadComplexity")```

### Results

This metric returns metric results for each Simulink design layer in the units and components in your project. A Simulink design layer is an artifact, like a block diagram or subsystem, that has its own canvas in Simulink.

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

• `Value` — Structure with fields for:

• `TotalOperators` — Number of Simulink blocks used in the design layer.

• `UniqueOperators` — Number of unique Simulink blocks used in the design layer. If you reuse a model component, for example using a referenced model, the metric considers the model component as one operator, even if you reuse the model component multiple times.

• `TotalOperands` — Number of inports and outports.

• `UniqueOperands` — Number of output signals from each Simulink block.

• `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.