Rules Hub
Code Ruleset
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: typescript
typescriptnull-safetyapiservice-layer
Return an empty collection rather than null for list-returning functions
Return [] for successful empty list results; do not conceal a failed fetch as an empty list.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | function selectUsers(users: User[], activeOnly: boolean): User[] | null { |
| 2 | const matches = users.filter(user => !activeOnly || user.active); |
| 3 | return matches.length ? matches : null; |
| 4 | } |
Explanation (EN)
Using null for ordinary successful emptiness forces redundant caller branches.
Objašnjenje (HR)
Good example
New codets
| 1 | function selectUsers(users: User[], activeOnly: boolean): User[] { |
| 2 | return users.filter(user => !activeOnly || user.active); |
| 3 | } |
Explanation (EN)
A deterministic successful selection returns a list even when no users match.
Objašnjenje (HR)
Notes (EN)
Use an empty collection for a successful list query with no matching records when absence has no separate domain meaning.
Exceptions / Tradeoffs (EN)
Do not turn fetch failures, unauthorized access or not-loaded state into a successful empty list. Preserve explicit failure and loading contracts.