Skip to content

divide-by-length ​

A division (or remainder) by the size of a collection the function itself made by selecting, before anything checks that it isn't empty: rows.length / rows.filter(isOk).length, the share of none. On by default, as a warning.

Why: 0 / 0 is NaN and 1 / 0 is Infinity, and both are numbers: no type is broken, nothing throws, and the value goes on into arithmetic, comparisons (all false for NaN) and what the user sees ("NaN%", an average of NaN ms). A filter, or a loop that pushes only some of the elements, can keep none of them, and that is a fact of the function's own code: whatever the caller passes, the selection can come out empty. Guide: Testing with generated data.

✕ Rejecteddivide-by-length
ts
type Run = { readonly ok: boolean; readonly ms: number };

export function meanPassTime(runs: readonly Run[]): number {
  const passed = runs.filter((run) => run.ok);
  return passed.reduce((sum, run) => sum + run.ms, 0) / passed.length;
}

✅ Accepted: no mean of nothing, said in the type

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Run = { readonly ok: boolean; readonly ms: number };

export function meanPassTime(runs: readonly Run[]): Option<number> {
  const passed = runs.filter((run) => run.ok);
  if (passed.length === 0) return undefined;
  return passed.reduce((sum, run) => sum + run.ms, 0) / passed.length;
}

✅ Also accepted: a value that means "none" where it is shown

✓ Accepted0 diagnostics
ts
type Row = { readonly ok: boolean };

export function passRate(rows: readonly Row[]): string {
  const passed = rows.filter((row) => row.ok);
  return passed.length > 0 ? `${Math.round((passed.length / rows.length) * 100)}%` : "–";
}

Don't answer with Math.max(1, passed.length): that turns the visible NaN into a number that looks right and isn't. Decide what "none" means, and say it.

What is a collection made by selecting: a const of the function (not a parameter, a field or a module-level list) made with filter or flatMap (also followed by map, toSorted, toReversed, sort, reverse), or made empty ([], new Set(), new Map()) and filled only under a condition: every push, unshift, add or set of it is inside an if, a ?:, the right of && / ||, a switch case or a catch, or after a continue in its loop (a return in its callback). A collection also passed to a call, stored elsewhere or written by index isn't one (something else may fill it). The divisor is its length or size, or a const holding one.

What shows it isn't empty, between the check and the division (the checks no-unchecked-cast uses for an index): if (passed.length === 0) return, if (!passed.length) return, passed.length > 0 ? … : …, passed.length !== 0 && …, assert.ok(passed.length > 0), the body of a for...of over it and a callback of its map, forEach, reduce, ... (they run for an element), and for a Set or Map the same checks of size. Not reported: xs[i % xs.length] (for an empty xs the read is undefined, which the type makes the code handle).

What the rule can't see:

  • Any other divisor. A parameter's length (sum / values.length in average(values)), a field's, a module-level list's, a number parameter (done / total), an imported constant: they can be 0 too, but whether they are is a contract with the caller, which the code doesn't show. Measured on an adopter's project of about 700 files: every divisor that isn't a literal or checked was about 1,700 places, none of the ones read could be 0; every list's length was 29 places, 2 of them real, the others lists a caller always fills. The selections this rule reads were 1 place, and it was real. Give such a function a tuple type with an element at least (readonly [number, ...number[]]), or check where the value comes in; a property test (check(arbitrary<T>(), ...)) tries the empty list for you.
  • A selection by another means: Array.from(set) of a filtered set, reduce into a list, a helper function that filters, a push behind a condition the code expresses otherwise (a break before it, a try around it).
  • A collection stored under another name, or passed to a call between the check and the division: the check no longer counts, as for an index.

Related: number-parse-nan (the other NaN); not-found-index; unbounded-spread (Math.max(...[]) is -Infinity).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra