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).
Compute response fields instead of shipping hardcoded placeholders
Don't return a hardcoded placeholder (e.g. count: 0) for a response field — derive it from the data you already hold, or it silently lies to callers.
Bad example
| 1 | const buildFeedResult = (items: Item[], limit: number): FeedResult => { |
| 2 | const page = items.slice(0, limit); |
| 3 | return { |
| 4 | items: page, |
| 5 | total: items.length, |
| 6 | todayCount: 0, // TODO: hardcoded — no real value |
| 7 | }; |
| 8 | }; |
Explanation (EN)
todayCount is hardcoded to 0. Consumers cannot distinguish 'genuinely zero today' from 'not implemented', and the data needed to compute it (item timestamps) is already in memory.
Objašnjenje (HR)
todayCount je tvrdo kodiran na 0. Potrošači ne mogu razlikovati 'stvarno nula danas' od 'nije implementirano', a podaci potrebni za izračun (vremenske oznake stavki) već su u memoriji.
Good example
| 1 | const buildFeedResult = (items: Item[], limit: number): FeedResult => { |
| 2 | const page = items.slice(0, limit); |
| 3 | const startOfTodayMs = new Date().setHours(0, 0, 0, 0); |
| 4 | const todayCount = items.filter((i) => i.timestamp >= startOfTodayMs).length; |
| 5 | return { |
| 6 | items: page, |
| 7 | total: items.length, |
| 8 | todayCount, |
| 9 | }; |
| 10 | }; |
Explanation (EN)
todayCount is derived from data already in hand, so the field is truthful and consumers can rely on it.
Objašnjenje (HR)
todayCount se izvodi iz podataka koje već imamo, pa je polje istinito i potrošači se mogu osloniti na njega.
Notes (EN)
If a field genuinely cannot be computed yet, make that explicit in the type (optional/null) rather than faking a value.
Bilješke (HR)
Ako se polje stvarno još ne može izračunati, to učini eksplicitnim u tipu (opcionalno/null) umjesto da lažiraš vrijednost.