Repository navigation
Description
Parent: #6
Readability metrics that measure how hard code is to read and slip past the current four.
Calibrated 2026-08-31 against 17,414 functions (measurement comment on #6). ternary_depth was cut; widget_depth default moved 7 → 8.
No longer depends on the metric registry: bool_ops is an all-language metric of exactly the same shape as the existing four, and widget_depth is the only language-specific one. Build widget_depth as a Dart-specific field and extract a registry when a second language-specific metric appears.
1. widget_depth (Dart, applies to build methods)
The current depth metric counts control-flow nesting, so a branch-free Flutter build can nest eight visual layers and stay green. This is the largest true gap found in the whole survey.
Evidence — 156 wrapper edges across the Dart corpus (86 in a private Flutter app, 70 in 3d_portfolio):
rg -n 'child: (const )?(SafeArea|SingleChildScrollView|Center|ConstrainedBox|Column|Padding|Row|Stack|Material|InkWell|DecoratedBox)\(' \
<private-flutter-app>/lib Personal/3d_portfolio/lib -g '*.dart' | wc -l # 1563d_portfolio/lib/src/features/quest/quest_zone_page.dart:723—TweenAnimationBuilder → Column → ClipRRect → ColoredBox → Align → FractionallySizedBox → SizedBox → DecoratedBox3d_portfolio/lib/src/features/home/quick_scan_page.dart:29—Scaffold → DecoratedBox → SafeArea → SingleChildScrollView → Center → ConstrainedBox → Column3d_portfolio/lib/src/features/quest/quest_zone_page.dart:35and.../home/project_detail_page.dart:35repeat the same seven-level page shell inline.
Rule. In methods named build, define a constructor-like node as: a const_object_expression / new_expression, or a call_expression whose function is an identifier starting with an ASCII uppercase letter (Column(...)) or a member expression whose leftmost identifier is uppercase (EdgeInsets.all(...)). Walk every return expression including lexically contained builder callbacks; increment on entering a constructor-like node; preserve depth through argument, collection, conditional, cascade, and property-access nodes; do not descend into nested named function declarations. Report the maximum on any ancestor path.
Default: 8 (fail at 9+). Calibrated: 163 Dart build methods measured — median 5, p90 8, p95 10, max 11. Limit 8 fails 13 methods (8%); limit 7 would fail 23 (14%); limit 6 would fail 37 (23%). Worst: two screens in the private Flutter app at 11. Syntax-only: yes, as a proxy — without type resolution the analyzer cannot prove Foo(...) returns a Widget. FP risk — quantified: of 2,475 constructor-like nodes counted, ~22% are non-widget value constructors: Theme.of (174), EdgeInsets (111), BorderRadius (52), BoxDecoration (47), InputDecoration (32), Border (23). Most are leaves and seldom sit on the max-depth path, but the rule must either carry an exclusion list or require the node to occupy a child: / children: / body: argument position. Decide this before implementing. flutter_lints and dart analyze do not cover this; third-party DCM's avoid-deeply-nested-widgets overlaps.
2. bool_ops — logical operators per expression (all languages)
A function can sit far below cyclomatic 15 and still contain one opaque five-clause condition, because cyclomatic spreads the count across the whole function while readability suffers at a single expression.
Evidence — 32 lines with 3+ same-line logical operators across the TS corpus (a lower bound; multi-line expressions are not counted):
rg -n --glob '*.{ts,tsx,svelte}' '&&.*&&.*&&|\|\|.*\|\|.*\|\|' \
<private-svelte-app>/src <private-ts-repo-a> <private-ts-repo-b>/src \
Pickforge/picklab/packages Pickforge/pickforge/src | wc -l # 32- a retry helper in the private SvelteKit app — five retry-status alternatives in one expression
pickforge/src/stores/runLaunch.ts:197—sameRemotecombines identity plus four remote-field checks- a usage adapter in a private TS repo — four usage counters compared in one condition
pickforge/src/lib/remoteContext.ts:100— four path-validity clauses in one guard
Rule. For each condition or value expression, count descendant && / || operators, stopping at nested function boundaries. Default: 3 (fail at 4). Calibrated: 59 functions exceed 3, and 39 of them are caught by no existing metric — the strongest new signal in the survey. Max observed 9 (pickforge/src/components/chat/Composer.tsx:190), then 8 at pickgauge-playwright-sidecar.mjs:265 and picklab/packages/core/src/redact.ts:95. Counts exclude generated files, which otherwise dominate. Independent of the enclosing function's total complexity. Syntax-only: yes. FP risk: low-medium — validation guards legitimately reach four clauses, but that is also where a named predicate helps.
3. ternary_depth — CUT
ternary_depthMeasured and rejected. Only 12 functions in 17,414 exceed depth 2, and just 4 are not already caught by an existing metric. That does not justify a metric, a config key, and fixtures across eight languages. The worst case (pickforge/src/screens/Settings.tsx:971, depth 6) already fails today. Lowering the limit to 1 would flag 85 functions but starts failing ordinary two-branch ternaries.
The underlying gap in opens_depth (statements only, so expression nesting opens no depth) is real but not costing anything measurable in this corpus. Record it in the spec as a known limitation; do not build it.
Checklist
- Calibrate defaults against the corpus (see Metric expansion: cognitive complexity and Svelte template scoping #6 measurement comment)
-
docs/spec.md:bool_opsrule, all languages - Implement
bool_ops+ fixtures - Decide the
widget_depthnon-widget exclusion approach (list vs. argument-position) -
docs/spec.md:widget_depth— constructor-like node definition, Dart only - Implement
widget_depth+ fixtures - Re-scan the corpus; confirm the flagged functions are ones a human agrees are hard to read
- PR/review
Validation
cargo test --workspace --locked --all-targets
cargo clippy --workspace --all-targets -- -D warnings
cargo llvm-cov
cargo run -- check crates
Plus a before/after corpus scan across ~/Projects/Personal and ~/Projects/Pickforge.
Status: Planned
Next action: bool_ops spec + implementation — unblocked, no dependency on #7
Activity
ElbertePlinio commentedon Aug 31, 2026
Depends on #7 (metric registry). Full evidence and the rejected-rules rationale are in the #6 body.
[-]Readability metrics: widget_depth, bool_ops, ternary_depth[/-][+]Readability metrics: bool_ops + widget_depth[/+]ElbertePlinio commentedon Aug 31, 2026
PR: #10 — in review.
docs/spec.md:bool_opsrule, all languagesbool_ops+ fixtureswidget_depthnon-widget exclusion approach — widget-slot filter applied during descent, not an exclusion listdocs/spec.md:widget_depth— constructor-like node definition, Dart onlywidget_depth+ fixturesDefaults shipped:
bool_ops: 3,widget_depth: 7.No metric registry was needed.
bool_opshas the same shape as the existing four, andwidget_depthis the only language-specific metric so far, so it is a Dart-specific field. Extract a registry when a second one appears — the abstraction was not carrying its weight yet.Decisions made during implementation, all now in the spec:
_Card(...)is idiomatic for sub-widgets and nests like any other widget.Text('x').animate().fadeIn()counts once. Otherwiseflutter_animateand extension styles get penalised for nesting they do not create.(a && b) ? (c || d) : escores 1, not 2.Side fix: Dart method names no longer resolve to their annotations.
descendant_fieldwas descending into@overrideand returning it as the name, which hid everybuildmethod fromwidget_depthand mislabelled 429 functions across the Flutter corpus.Status: In review
Next action: CI, then merge
feat: add bool_ops and widget_depth readability metrics (#10)