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).
Make implicit unit/currency assumptions explicit in calculations
When combining monetary or unit-bearing values, don't silently assume they share the same unit; normalize or validate before combining.
Bad example
| 1 | // price is in `currency`, commission's unit is never checked — assumed to match |
| 2 | const priceInBase = new Decimal(dto.price).mul(exchangeRate); |
| 3 | const total = priceInBase.add(dto.commission); // commission silently treated as base currency |
Explanation (EN)
Adding commission directly to a converted price assumes commission is already in the same currency. If a caller supplies commission in the instrument currency, the total is wrong with no error.
Objašnjenje (HR)
Dodavanje provizije izravno na konvertiranu cijenu pretpostavlja da je provizija vec u istoj valuti. Ako pozivatelj posalje proviziju u valuti instrumenta, ukupni iznos je pogresan bez greske.
Good example
| 1 | // Normalize every component to the base currency before combining |
| 2 | const priceInBase = new Decimal(dto.price).mul(exchangeRate); |
| 3 | const commissionInBase = new Decimal(dto.commission ?? 0).mul(exchangeRate); |
| 4 | const total = priceInBase.add(commissionInBase); |
| 5 | // Assumption documented: commission is provided in the instrument currency, same as price. |
Explanation (EN)
Converting each unit-bearing value into a common unit before combining makes the assumption explicit and the result correct regardless of input currency.
Objašnjenje (HR)
Pretvaranje svake vrijednosti s jedinicom u zajednicku jedinicu prije kombiniranja cini pretpostavku eksplicitnom i rezultat ispravnim bez obzira na ulaznu valutu.
Notes (EN)
Even when you cannot normalize, document the assumed unit at the function boundary so callers know what to pass.
Bilješke (HR)
Cak i kad ne mozes normalizirati, dokumentiraj pretpostavljenu jedinicu na granici funkcije kako bi pozivatelji znali sto poslati.