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).
Inline trivial single-use wrappers instead of extracting them
Don't create a named utility for a one-liner that just wraps a built-in; inline it unless it's reused or hides real complexity.
Bad example
| 1 | // utils.ts |
| 2 | export const clearStorageKeys = (keys: readonly string[]) => { |
| 3 | keys.forEach(key => { |
| 4 | window.sessionStorage.removeItem(key); |
| 5 | }); |
| 6 | }; |
| 7 |
|
| 8 | // caller.ts — single use, no added value |
| 9 | clearStorageKeys(['token', 'profile']); |
Explanation (EN)
The utility adds an extra name, file, and import to read through, but does nothing a reader couldn't grasp instantly from the inline loop. It is used in only one place and hides no complexity.
Objašnjenje (HR)
Utility dodaje još jedno ime, datoteku i import koje treba pročitati, ali ne radi ništa što čitatelj ne bi odmah shvatio iz inline petlje. Koristi se samo na jednom mjestu i ne skriva nikakvu složenost.
Good example
| 1 | // caller.ts — inline, self-explanatory |
| 2 | ['token', 'profile'].forEach(key => { |
| 3 | window.sessionStorage.removeItem(key); |
| 4 | }); |
Explanation (EN)
Inlining keeps the logic where it is used, removes an indirection, and stays just as readable. Introduce a named helper only once the same operation is needed in multiple places or genuinely warrants an abstraction.
Objašnjenje (HR)
Inline zadržava logiku tamo gdje se koristi, uklanja indirekciju i ostaje jednako čitljiv. Uvedi imenovani helper tek kad je ista operacija potrebna na više mjesta ili stvarno zahtijeva apstrakciju.
Notes (EN)
Heuristics for keeping a wrapper: it is called from more than one place, it encapsulates non-obvious logic or edge cases, or its name communicates intent that the inline code does not. If none of these hold, prefer the inline form.
Bilješke (HR)
Heuristike za zadržavanje wrappera: poziva se s više mjesta, enkapsulira neočitu logiku ili rubne slučajeve, ili njegovo ime prenosi namjeru koju inline kod ne prenosi. Ako ništa od toga ne vrijedi, preferiraj inline oblik.
Exceptions / Tradeoffs (EN)
Keep the wrapper if the same loop/call is needed in several call sites, if it isolates a tricky API quirk, or if a descriptive name materially improves readability at the call site. Balance against extract-generic-ui-patterns: inline the wrapper when it appears once and wraps a single built-in with no added behavior.
Iznimke / Tradeoffi (HR)
Zadrži wrapper ako je ista petlja/poziv potrebna na više mjesta, ako izolira nezgodnu osobitost API-ja, ili ako opisno ime značajno poboljšava čitljivost na mjestu poziva.