Find the Routines That Nest Too Deep
To list the Delphi routines whose control flow nests too deep, drag-lint has a rule, deep-nesting, that counts only the statements that really add a level and reports the same routines every run.
The rule exists since earlier releases; the counting change described here shipped in drag-lint 1.51.1-alpha and is in 1.51.2-alpha, the current release. Every output below is real output of 1.51.2-alpha on the small unit in section 01 (path shortened).
Before a refactoring we want a list: which routines are so deeply nested that nobody dares touch them? Counting leading spaces does not answer it, because indentation is a style, not a structure. Micronite, the quality and production-control software we build for manufacturers, has routines grown over many years, and this rule is how we pick the ones worth splitting first.
One routine that nests, one that only looks it
Walk nests an if, a for, an if, a while, an if and a case. Calm has two try..finally blocks and one if, so it is indented deeply but does not branch deeply.
procedure Walk(pA, pB: Integer);
...
begin
S := if pA > 0 then 'p' else 'n';
try
if pA > 0 then
for I := 0 to pA do
if I > pB then
while pB > 0 do
if I = 3 then
case pB of
1: Dec(pB);
end;
finally
Writeln(S);
end;
end;
procedure Calm(pA: Integer);
...
begin
S := if pA > 0 then 'p' else 'n';
try
try
if pA > 1 then
Writeln(S);
finally
Writeln(S);
end;
finally
Writeln(S);
end;
end;
Ask for the rule, read the finding
Step 1. See what the rule is and where its limit sits.
drag-lint rules --rule deep-nesting
1 rules across 1 categories
[complexity]
deep-nesting builtin (info; threshold=5) [CWE-1124]
Step 2. Lint the unit with only that rule. Add --json for a machine-readable list.
drag-lint lint Nest.pas --rule deep-nesting
Nest.pas:10:1 [info] deep-nesting: Routine Walk nests control structures 6 deep (max 5) -- flatten with early exits or sub-routines
1 finding(s)
The JSON finding carries "rule": "deep-nesting", "start_line": 10 and "metric": 6, so a script can sort a whole project by depth.
Step 3. Read what was not reported. Calm is absent: its two try..finally blocks and the if-expression in S := if ... then ... else ... add no level. Since 1.51.1 the rule counts control statements only: if, case, the loops, with and try..except. Before that, resource cleanup could push a routine over the limit.
The tree decides, not the text
drag-lint parses the unit into a syntax tree and walks it; the depth is the number of control statements on the deepest path through the routine body. There is no estimate and no model involved: the same unit gives the same number on any machine, in the IDE plugin as on the command line. The finding is a node in a tree, which is why it can name the routine and the exact depth.
What it does not see
- The run above was a single file with no index (
lintsays so on its second line), so store-backed and project-wide rules did not run.deep-nestingis a built-in tree rule and does not need one. - The limit of 5 is the default and is a style choice. It measures depth, not difficulty: a flat routine can still be hard to read.
- The rule has no automatic fix. Flattening needs a human decision about early exits and sub-routines.