Rules Hub
Coding Rules Library
← 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
Prefer returning an empty array over null from functions whose callers expect a list, so consumers do not need null checks.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | const getUsers = async (): Promise<User[] | null> => { |
| 2 | try { |
| 3 | return await fetchUsers(); |
| 4 | } catch { |
| 5 | return null; // callers must null-check before mapping |
| 6 | } |
| 7 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const getUsers = async (): Promise<User[]> => { |
| 2 | try { |
| 3 | return await fetchUsers(); |
| 4 | } catch { |
| 5 | return []; |
| 6 | } |
| 7 | }; |
Explanation (EN)
Objašnjenje (HR)