Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Define explicit failure contracts for pure helpers
Make helper failure contracts explicit; do not replace invalid inputs with misleading empty results.
Bad example
| 1 | function chunk<T>(items: T[], size: number) { |
| 2 | if (size <= 0) return []; |
| 3 | return chunkValidated(items, size); |
| 4 | } |
Explanation (EN)
An undocumented exception contract surprises callers, but silently returning fake emptiness is not a general fix.
Objašnjenje (HR)
Good example
| 1 | function chunk<T>(items: T[], size: number): T[][] { |
| 2 | if (!Number.isSafeInteger(size) || size < 1) throw new RangeError("Invalid chunk size"); |
| 3 | const chunks: T[][] = []; |
| 4 | for (let i = 0; i < items.length; i += size) chunks.push(items.slice(i, i + size)); |
| 5 | return chunks; |
| 6 | } |
Explanation (EN)
The helper documents invalid size through a precise exception instead of pretending the input collection was empty.
Objašnjenje (HR)
Notes (EN)
Specify whether invalid helper inputs are programmer errors or expected domain outcomes; choose throwing, Result or a sentinel according to that contract.
Exceptions / Tradeoffs (EN)
Pure functions can legitimately throw. Returning [] for an invalid chunk size can hide a defect and discard data; a catch is not required at every intermediate caller.