Smell rules: replace temp with query, primitive obsession, derived variable
Three refactoring smells as per-file lint rules, OFF by default: temp to query, primitive signatures, derived fields (G-60).
Read this to see what replace-temp-with-query, primitive-obsession and derived-variable report, what each leaves alone, and how to switch them on (all three are off by default).
How to run it
drag-lint lint <path> [--profile <name>] [--target-compiler <ver|auto>] [--rule <id>|help] [--disable id1,id2] [--rules-dir <dir>] [--json] [--labels] [--no-preprocess] [--library-db <lib.sqlite>] [--build-config <name>] [--no-budget]
Example:
drag-lint lint <file.pas> --rule replace-temp-with-query
Full flag list: lint in the CLI reference.
drag-lint lint-all [--profile <name>] [--target-compiler <ver|auto>] [--db <file.sqlite>] [--project <.dproj>] [--build-config <name>] [--rule <id>[,<id>...]] [--disable id,...] [--output <report.txt>] [--json] [--labels] [--quiet] [--lint-third-party] [--no-preprocess] [--shared-db <db> ...] [--library-db none] [--no-budget] [--changed-since <rev>]
Example:
drag-lint lint-all --db <project.sqlite> --rule primitive-obsession
Full flag list: lint-all in the CLI reference.
drag-lint lint-all [--profile <name>] [--target-compiler <ver|auto>] [--db <file.sqlite>] [--project <.dproj>] [--build-config <name>] [--rule <id>[,<id>...]] [--disable id,...] [--output <report.txt>] [--json] [--labels] [--quiet] [--lint-third-party] [--no-preprocess] [--shared-db <db> ...] [--library-db none] [--no-budget] [--changed-since <rev>]
Example:
drag-lint lint-all --db <project.sqlite> --rule derived-variable
Full flag list: lint-all in the CLI reference.
Where to click
No IDE surface: this feature runs from the CLI (see How to run it).
Details
Three refactoring smells as lint rules, and two new numbers in the metrics verb (1.63, lane Q2RULES2, gap-plan rows G-60 and G-53). The rules are per-file, read only the unit's parse tree, have severity hint, category refactoring, and are OFF by default (owner ruling Q162-7: smells ship OFF).
Running the rules
drag-lint lint <file.pas> --rule replace-temp-with-query
drag-lint lint-all --db <project.sqlite> --rule primitive-obsession
drag-lint lint-all --db <project.sqlite> --enable replace-temp-with-query,primitive-obsession,derived-variable
Or opt in per project with "enabled": ["replace-temp-with-query", "primitive-obsession", "derived-variable"] in drag-lint-lint.json. A plain lint / lint-all runs none of them.
replace-temp-with-query
A local of a METHOD (TFoo.Bar) that is written exactly once, by an unconditional top-level statement Temp := <expr> (or an inline var Temp := <expr>), and then only read, where <expr>:
- is a computation (it holds a binary operator:
FQuantity * FItemPrice,FName + ' x'); - calls nothing with arguments;
- reads no local, no parameter and no
Result; - reads nothing that the routine writes anywhere (so the value cannot change between the temp and a later read).
Such a temp is a query in disguise: a parameterless method of the class can return <expr>, and every routine that needs the value can ask for it. The finding sits on the assignment and names the temp, the expression and how often it is read.
Not reported: a temp also passed as a call argument (it may be a var / out parameter), taken the address of (@), used as a loop variable or written twice; an assignment inside an if, a loop or a try; an expression that only copies (T := FPrice); a plain routine (no class to put the query on); a routine holding a with statement (names are ambiguous there).
primitive-obsession
A routine signature of four or more parameters (PRIMITIVE_MIN_PARAMS), EVERY one of a built-in simple type (string and the other string types, the integer and floating types, Char, Boolean, Currency, TDateTime / TDate / TTime), three or more (PRIMITIVE_MIN_SAME) of the same type: SetAddress(const AStreet, ACity, AZip: string; ANumber: Integer). The concept the parameters carry (an address, a size, a connection) wants its own record or small class passed as one parameter.
Reported once per routine, where its signature is decided: the declaration inside a class / record / interface, the interface-section declaration of a plain routine, or an implementation-only routine. Never on an override (the ancestor decided the shape; the virtual ancestor is reported instead). Not reported: a signature with one non-primitive parameter (Sender: TObject), an untyped parameter, fewer than four parameters, or four primitives of mixed types with no three alike.
How it differs from its neighbours: data-clump (G-58) looks for the same group of parameters in several routines across the index; too-many-parameters counts parameters of any type. primitive-obsession needs neither repetition nor a long list -- only a signature built entirely from primitives with one type dominating.
derived-variable
A field of a class declared in the unit that the class's own methods assign at two or more places (DERIVED_MIN_SITES), every time with the same expression (blanks and case ignored), where the expression reads at least one OTHER field of the class and not the field itself:
procedure TOrder.SetQuantity(AValue: Integer);
begin
FQuantity:= AValue;
FTotal:= FQuantity * FItemPrice; { site 1 }
end;
procedure TOrder.SetItemPrice(AValue: Double);
begin
FItemPrice:= AValue;
FTotal:= FQuantity * FItemPrice; { site 2: same formula }
end;
FTotal is mutable data that is always derivable: replace it with a function (or a read-only property with a getter) that computes FQuantity * FItemPrice on demand, and the setters no longer need to keep it in step. The finding sits on the field declaration and lists the methods that recompute it.
A constructor or destructor assigning a literal (FTotal := 0) is not counted as a site. Not reported: two sites with different expressions; an accumulator (FAcc := FAcc + X reads itself); an expression that reads a local or a parameter of its method; a field also passed as a call argument or taken the address of; a class with a method holding a with statement.
Halstead and maintainability in metrics (G-53)
drag-lint metrics --halstead counts the tokens of every routine body (the begin..end block; header, local declarations and nested routines excluded, so nothing is counted twice) on the tree-sitter parse of its file, at metrics time -- no index fact, so no re-index is needed:
- operands: identifiers, numbers, string / character literals,
nil,True,False(identifiers compared without case); - operators: every other token (keywords such as
begin,if,:=,div, and punctuation such as;.(); - volume
V = (N1 + N2) * log2(n1 + n2), difficultyD = (n1 / 2) * (N2 / n2), effortE = D * V; - maintainability index (0..100, the Visual Studio form):
max(0, (171 - 5.2 ln V - 0.23 CC - 16.2 ln LOC) * 100 / 171)withCCthe cyclomatic value andLOCthebody_locof the routine.
Opt-in: without --halstead nothing is parsed and the output is the 1.62 one (no Halstead member, no mi column). With it, JSON rows gain halstead_routines, halstead_volume, halstead_difficulty, halstead_effort, maintainability, maintainability_min (2 decimals). Above routine level: volume and effort are SUMS, difficulty the MAX, maintainability the MEAN and maintainability_min the lowest over the measured routines; halstead_routines says how many were measured. A routine whose file is gone or no longer has the definition at the indexed line is unmeasured: halstead_routines 0 and every other Halstead member null. The text output gains an mi column and one line under the total: maintainability index (0..100): mean M, lowest L over N measured routine(s); Halstead volume V.
Cost, and why it is opt-in: every file with a routine in the selection is parsed once. Measured on a copy of drag-lint's own index (610 files, 605 parsed; median of 5 interleaved runs on a shared machine, lane METRICS-PERF 2026-12-01): plain metrics 1.8 s (1.62: 1.9 s); metrics --halstead 10.9 s, of which about 5-6 s is the tree-sitter parse and about 2.7 s the token walk. --in <path prefix> limits the parse to the selected files.
Tests
tests\autotest\run_v163_q2rules2.ps1 on tests\fixtures\v163\q2rules2\: SmellPos.pas holds the positive controls (// FIRE <rule> lines), SmellNeg.pas the near misses of every "not reported" case above, HalUnit.pas two routines whose Halstead counts are worked out by hand in the runner. The rule checks fail on an engine without the rules; the metrics checks fail on an engine without G-53 (null counts).