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).
Favor readable map/filter over hand-rolled loops unless data size warrants it
Choose declarative, readable transformations over imperative single-pass loops unless the collection is large enough that the extra iteration actually matters.
Bad example
| 1 | // Imperative single-pass loop chosen purely to avoid two iterations, |
| 2 | // on a list that's known to be small. |
| 3 | const items: WidgetItem[] = []; |
| 4 | for (const order of orders) { |
| 5 | if (!isShipped(order)) continue; |
| 6 | items.push({ id: order.orderId, label: order.customerName }); |
| 7 | } |
| 8 | return items; |
Explanation (EN)
Reaching for a manual loop to save one pass over a small list is premature optimization: it adds boilerplate (mutable accumulator, continue) without a measurable benefit, hurting readability for nothing.
Objašnjenje (HR)
Posezanje za rucnom petljom da se ustedi jedan prolaz po maloj listi je preuranjena optimizacija: dodaje suvisni kod (promjenjivi akumulator, continue) bez mjerljive koristi, narusavajuci citljivost ni za sto.
Good example
| 1 | // Clear declarative pipeline; two passes are negligible on a small list. |
| 2 | return orders |
| 3 | .filter(isShipped) |
| 4 | .map(order => ({ id: order.orderId, label: order.customerName })); |
Explanation (EN)
A filter/map chain expresses the intent directly and the cost of two passes is negligible for typical collection sizes, so readability wins until profiling proves otherwise.
Objašnjenje (HR)
Lanac filter/map izravno iskazuje namjeru, a trosak dva prolaza je zanemariv za uobicajene velicine kolekcija, pa citljivost pobjeduje dok profiliranje ne dokaze suprotno.
Exceptions / Tradeoffs (EN)
For large or hot-path collections where profiling shows the extra iterations matter, a single-pass loop (or reduce) is the right call.
Iznimke / Tradeoffi (HR)
Za velike kolekcije ili vruce putanje gdje profiliranje pokaze da dodatne iteracije imaju utjecaja, jednoprolazna petlja (ili reduce) je pravi izbor.