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).
Select records by key with .find(), not by array index, when order isn't guaranteed
Don't treat data[0] as the newest/selected item when the source doesn't guarantee ordering; locate the record with .find() on a meaningful field so a backend reorder can't surface stale data.
Bad example
| 1 | const latest = data.financialData?.[0]; |
| 2 | // assumes index 0 is the most recent period |
Explanation (EN)
Index 0 is only 'latest' by coincidence of current API ordering; if the backend returns records in a different order the UI silently shows the wrong year.
Objašnjenje (HR)
Indeks 0 je 'najnoviji' samo slucajno zbog trenutnog redoslijeda API-ja; ako backend vrati zapise drugim redoslijedom, UI tiho prikazuje krivu godinu.
Good example
| 1 | const latest = data.financialData?.find((fd) => |
| 2 | fd.period.fromDate.startsWith(targetYear), |
| 3 | ) ?? data.financialData?.[0]; |
Explanation (EN)
The record is chosen by the field that actually defines 'latest', with a safe fallback, so it stays correct regardless of array order.
Objašnjenje (HR)
Zapis se bira prema polju koje stvarno definira 'najnoviji', uz sigurnu rezervnu vrijednost, pa ostaje ispravan bez obzira na redoslijed niza.
Exceptions / Tradeoffs (EN)
If the API contract explicitly guarantees ordering (e.g. documented 'sorted descending by date'), indexing is acceptable — reference that guarantee in a comment.
Iznimke / Tradeoffi (HR)
Ako API ugovor izricito jamci redoslijed (npr. dokumentirano 'sortirano silazno po datumu'), indeksiranje je prihvatljivo — referenciraj to jamstvo u komentaru.