liveBook · Manning

Halstead complexity

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

Halstead complexity is a family of software metrics derived from counts of distinct and total operators and operands in a program.

Halstead complexity measures assess software complexity by counting the operators and operands used in a program. They turn code size and cognitive complexity into concrete measurements that can support discussion beyond personal aesthetic judgment. section 6.2

The family includes several related measures:

  • Program length (N1) and program vocabulary (n1) describe the amount and variety of operators and operands. section 6.2
  • Volume (V), difficulty (D), and effort (E) provide further views of code size and the cognitive work involved in understanding it. section 6.2
  • Ask Copilot to calculate the measures for a class, then continue with follow-up questions about whether the resulting values indicate simpler or harder code. section 6.2

A visitor-class assessment

For the class Department-StatisticsVisitor, a Copilot exchange reports a Halstead Complexity Measure of 2 and a Halstead Difficulty Measure of 1. It labels both values as good, while identifying 10 as a bad value and explaining that high difficulty means the code is hard to understand. Treat these answers as a starting point for investigation rather than as an automatic quality verdict. section 6.2

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Defines several measures and demonstrates Copilot-based interpretation, but does not derive their formulas or calculation procedure.
Examples
Uses the Department-StatisticsVisitor class and Copilot questions reporting Halstead complexity 2 and difficulty 1.
What you can do after
Evaluate code complexity with Copilot-assisted Halstead measurements and use the results as evidence for quality discussions.
How it connects
Connects Halstead measures with cyclomatic complexity, maintainability index, code coverage, testing, and refactoring.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.