Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Short-circuit downstream calls when the id collection is empty
Return early with an empty result when a function receives an empty list of ids instead of calling a downstream API with it.
Bad example
| 1 | async fetchQuotes(ids: number[]): Promise<Quote[]> { |
| 2 | // ids may be empty -> downstream gets `ids=` and may error or return junk |
| 3 | const data = await this.client.fetchQuotes(ids); |
| 4 | return data.map((d) => transform(d)); |
| 5 | } |
Explanation (EN)
With an empty `ids` array the function still issues a request, building a malformed query (e.g. an empty `in (...)` clause or `ids=`). That wastes a round trip and can throw or return unexpected data.
Objašnjenje (HR)
S praznim poljem `ids` funkcija svejedno salje zahtjev i gradi neispravan upit (npr. prazan `in (...)` ili `ids=`). To trosi nepotreban poziv i moze baciti gresku ili vratiti neocekivane podatke.
Good example
| 1 | async fetchQuotes(ids: number[]): Promise<Quote[]> { |
| 2 | if (ids.length === 0) return []; |
| 3 | const data = await this.client.fetchQuotes(ids); |
| 4 | return data.map((d) => transform(d)); |
| 5 | } |
Explanation (EN)
An early `if (ids.length === 0) return []` guard avoids the pointless network/DB call and returns the correct empty result. Match guards that already exist in sibling methods so the behavior is consistent.
Objašnjenje (HR)
Rani guard `if (ids.length === 0) return []` izbjegava beskorisni mrezni/DB poziv i vraca ispravan prazan rezultat. Uskladi se s guardovima koji vec postoje u srodnim metodama radi konzistentnosti.
Notes (EN)
Prefer reusing an existing helper or matching an existing guard pattern in the same module rather than inventing a new convention.
Bilješke (HR)
Radije ponovno iskoristi postojeci helper ili uskladi postojeci guard obrazac u istom modulu nego da uvodis novu konvenciju.
Exceptions / Tradeoffs (EN)
If the downstream call has meaningful side effects for an empty input, or its contract explicitly handles empty collections cheaply, the guard may be unnecessary.
Iznimke / Tradeoffi (HR)
Ako downstream poziv ima smislene nuspojave za prazan ulaz, ili njegov ugovor eksplicitno i jeftino obraduje prazne kolekcije, guard mozda nije potreban.