feat(halstead): add -Ehalstead extension for Halstead complexity metrics#485
feat(halstead): add -Ehalstead extension for Halstead complexity metrics#485ArmaanjeetSandhu wants to merge 2 commits into
Conversation
Adds an optional `-Ehalstead` extension that computes Halstead complexity measures (volume, difficulty, effort, plus n1/n2/N1/N2, vocabulary, length, time and estimated bugs) for every function. Three columns (H-volume, H-diff, H-effort) are shown; all measures are also exposed on FunctionInfo via a `halstead` object and flat `halstead_*` attributes that work with --sort and --Threshold. Operator/operand classification is language-specific behind a small explicit interface (HalsteadClassifier), staying in 1:1 correspondence with the tokens lizard already emits. A precise Python classifier ships in-tree; other languages fall back to a generic C-family classifier. Readers may override selection via a `halstead_classifier` attribute, the intended seam for folding this into core later.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 12ae6f853b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| FUNCTION_INFO = { | ||
| "halstead_volume": {"caption": " H-volume "}, | ||
| "halstead_difficulty": {"caption": " H-diff "}, | ||
| "halstead_effort": {"caption": " H-effort "}, | ||
| } |
There was a problem hiding this comment.
Export all Halstead columns in CSV output
Defining -Ehalstead with multiple FUNCTION_INFO entries makes the current CSV writer drop every Halstead metric, because lizard_ext/csvoutput.py only appends extension columns when len(FUNCTION_INFO) == 1. As a result lizard --csv -Ehalstead ... produces the same CSV columns as a run without the extension, so users cannot consume the new metrics in CSV even though the default table shows them.
Useful? React with 👍 / 👎.
|
@terryyin the CSV issue flagged by the Codex bot is a pre-existing bug, not something Halstead introduced. The same |
Addresses #464.
What this adds
An optional
-Ehalsteadextension that computes Halstead complexity measures per function:Three columns are displayed (
H-volume,H-diff,H-effort). The full set (n1,n2,N1,N2, vocabulary, length, volume, difficulty, effort, time, estimated bugs) is available programmatically viafunction.halsteadand via flathalstead_*attributes that also work with--sort/--Threshold(e.g.-s halstead_volume).Design
Following the guidance on the issue:
HalsteadClassifieris the per-language extension point:classify(token)returns operator / operand / skip, one label per token, preserving 1:1 correspondence with the tokens lizard already emits (so this stays easy to fold into core later). A precisePythonHalsteadClassifierships in-tree; a generic C-family classifier is the fallback for other languages.get_classifier) checks, in order: ahalstead_classifierattribute on the reader (the seam intended for per-reader hooks in core), a registry keyed bylanguage_names, then the generic fallback.HalsteadMetricsholds the two operator/operandCounters and derives all measures lazily, so values always reflect the final counts.FunctionInfoas read-only properties, so they default to zero for functions that never went through the extension and integrate with the existing sort/threshold/output machinery.Counting convention
Halstead numbers are sensitive to how operators vs operands are counted. In short: operands are identifiers, numeric and string literals, and value-literal keywords (
True/False/None, etc.); operators are punctuation/operator symbols plus operator/control keywords (if,for,return,def,and, …); paired delimiters are counted individually; and tokens are attributed to functions exactly astoken_countis (sodef/classand the function name belong to the enclosing scope).Tests
test/testHalstead.pyadds 41 tests covering the basic counts, every derived measure (against hand-verified numbers), the flatFunctionInfoattributes, operator/operand classification (keywords, literals, strings, numbers, Python value literals and soft keywords), classifier selection precedence, theHalsteadMetricsvalue object, end-to-end C++ via the generic classifier, and extension statelessness. No regressions. Verified working via the console script,python -m lizard, and multiprocessing (-t 2).