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).
Prefer map over a push-into-array loop for independent transforms
When each iteration independently produces one output item, build the result with `map` instead of pushing into a pre-declared mutable array.
Bad example
| 1 | const result: DayDto[] = []; |
| 2 | for (const day of days) { |
| 3 | const items = events.filter(e => sameDay(e.date, day)).sort(byDate); |
| 4 | result.push(serialize<DayDto>(DayDto, { date: day, count: items.length, items }) as DayDto); |
| 5 | } |
Explanation (EN)
The mutable accumulator plus push adds noise; each iteration is independent and just maps one day to one result.
Objašnjenje (HR)
Promjenjivi akumulator i push dodaju sum; svaka iteracija je neovisna i samo preslikava jedan dan u jedan rezultat.
Good example
| 1 | const result = days.map(day => { |
| 2 | const items = events.filter(e => sameDay(e.date, day)).sort(byDate); |
| 3 | return serialize<DayDto>(DayDto, { date: day, count: items.length, items }); |
| 4 | }); |
Explanation (EN)
`map` expresses the one-to-one transform directly, removes the mutable array and the repeated cast, and is easier to read.
Objašnjenje (HR)
`map` izravno izrazava preslikavanje jedan-na-jedan, uklanja promjenjivi niz i ponovljeni cast te je citljiviji.
Exceptions / Tradeoffs (EN)
Keep an explicit loop when iterations depend on each other, when you need early `break`/`continue`, or when you conditionally skip items (use `flatMap`/`reduce` or filter first).
Iznimke / Tradeoffi (HR)
Zadrzite eksplicitnu petlju kada iteracije ovise jedna o drugoj, kada trebate rani `break`/`continue` ili kada uvjetno preskacete stavke (koristite `flatMap`/`reduce` ili prvo filtrirajte).