Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Reuse a single lookup result instead of searching repeatedly
Find an item once and derive all needed fields from that result rather than calling find multiple times for the same element.
Bad example
| 1 | const emoji = categories.find(c => c.id === category.id)?.emoji; |
| 2 | const name = categories.find(c => c.id === category.id)?.name; |
| 3 | const url = categories.find(c => c.id === category.id)?.urlname; |
Explanation (EN)
Repeated identical searches traverse the same collection for fields of one result.
Objašnjenje (HR)
Good example
| 1 | const matchedCategory = categories.find(item => item.id === category.id); |
| 2 | const emoji = matchedCategory?.emoji; |
| 3 | const name = matchedCategory?.name; |
| 4 | const url = matchedCategory?.urlname; |
Explanation (EN)
One lookup supplies all fields without inventing a new emoji default.
Objašnjenje (HR)
Notes (EN)
Reuse one find result when the collection and pure predicate are unchanged between the reads, preserving the original field defaults.
Exceptions / Tradeoffs (EN)
Repeated evaluation may differ for mutable collections, getters or stateful predicates. Extracting a lookup does not justify introducing a new fallback value.