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).
Don't silently default missing inputs to neutral values in calculations
When a required value for a financial or numeric calculation is missing, fail loudly or skip — never silently substitute a plausible-looking neutral default (e.g. exchange rate 1) that produces wrong results.
Bad example
| 1 | function toBaseCurrency(amount: number, currency: string, date: string): number { |
| 2 | // Falls back to 1 when the rate is unknown — produces a wrong total silently |
| 3 | const rate = rateCache.get(`${currency}:${date}`) ?? 1; |
| 4 | return amount * rate; |
| 5 | } |
Explanation (EN)
If the exchange rate is missing, multiplying by 1 yields a number that looks valid but is wrong. The error is invisible and pollutes every aggregate built on top of it.
Objašnjenje (HR)
Ako tečaj nedostaje, množenje s 1 daje broj koji izgleda ispravno, ali je pogrešan. Greška je nevidljiva i kvari sve agregate izgrađene na njoj.
Good example
| 1 | function toBaseCurrency(amount: number, currency: string, date: string): number { |
| 2 | const rate = rateCache.get(`${currency}:${date}`); |
| 3 | if (rate == null) { |
| 4 | throw new Error(`Missing exchange rate for ${currency} on ${date}`); |
| 5 | // or: log + skip this record so it can be reprocessed, instead of writing a wrong value |
| 6 | } |
| 7 | return amount * rate; |
| 8 | } |
Explanation (EN)
An absent required input is treated as an exceptional condition: throw, skip, or surface it — never coerce it into a silent neutral default that corrupts the result.
Objašnjenje (HR)
Nedostajući obavezni ulaz tretira se kao iznimno stanje: baci grešku, preskoči ili ga eksplicitno prijavi — nikad ga ne pretvaraj u tihi neutralni default koji kvari rezultat.
Notes (EN)
Applies to any value whose absence changes the math: tax rates, conversion factors, quantities, prices. Ask 'what produces a missing value here?' and handle that path deliberately.
Bilješke (HR)
Vrijedi za svaku vrijednost čije odsustvo mijenja izračun: porezne stope, faktore konverzije, količine, cijene. Pitaj 'što uzrokuje nedostajuću vrijednost?' i namjerno obradi taj slučaj.
Exceptions / Tradeoffs (EN)
A neutral default is fine when the value is genuinely optional and its absence semantically means the identity element (e.g. an optional discount that defaults to 0, or same-currency conversion that is legitimately 1).
Iznimke / Tradeoffi (HR)
Neutralni default je u redu kad je vrijednost stvarno opcionalna i njezino odsustvo semantički znači neutralni element (npr. opcionalni popust koji je 0, ili konverzija iste valute koja legitimno iznosi 1).