Documentation · Static metrics

km hal

Halstead complexity metrics

Computes Halstead complexity metrics per file by extracting operators and operands from source code.

km hal [path]

Metrics

SymbolMetricFormulaDescription
n1Distinct operators—Unique operators in the code
n2Distinct operands—Unique operands in the code
N1Total operators—Total operator occurrences
N2Total operands—Total operand occurrences
nVocabularyn1 + n2Size of the “alphabet” used
NLengthN1 + N2Total number of tokens
VVolumeN * log2(n)Size of the implementation
DDifficulty(n1/2) * (N2/n2)Error proneness
EEffortD * VMental effort to develop
BBugsV / 3000Estimated delivered bugs
TTimeE / 18 secondsEstimated development time

Higher effort, volume, and bugs indicate more complex and error-prone code.

Options:

FlagDescription
--format {table,json,short,terse}Output format (default: table)
--include-testsInclude test files in analysis (excluded by default)
--top NShow only the top N files (default: 20)
--sort-by METRICSort by effort, volume, or bugs (default: effort)

Example output:

Halstead Complexity Metrics
──────────────────────────────────────────────────────────────────────────────
 File                      n1   n2    N1    N2    Volume     Effort   Bugs
──────────────────────────────────────────────────────────────────────────────
 src/loc/counter.rs       139  116  3130  1169   34367.7   24070888  11.46
 src/main.rs               37   43   520   185    4457.0     354743   1.49
──────────────────────────────────────────────────────────────────────────────
 Total (2 files)                     3650  1354   38824.7   24425631  12.95

Supported languages

Rust, Python, JavaScript, TypeScript, Go, C, C++, C#, Java, Objective-C, PHP, Dart, Ruby, Kotlin, Swift, Shell (Bash/Zsh).