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).
Use the injected value, don't re-derive it from another field
When a value is passed in as a parameter, store it directly rather than recomputing it from a related field.
Bad example
| 1 | class Widget { |
| 2 | constructor({ el, containerEl }) { |
| 3 | this.el = el; |
| 4 | // ignores the passed containerEl and re-derives it |
| 5 | this.containerEl = el.parentElement; |
| 6 | } |
| 7 | } |
Explanation (EN)
Re-deriving containerEl from el.parentElement ignores the explicitly passed value, creating two possible sources of truth that can diverge and obscuring what the constructor actually depends on.
Objašnjenje (HR)
Ponovno izvodenje containerEl iz el.parentElement ignorira eksplicitno proslijedenu vrijednost, stvara dva moguca izvora istine koji se mogu razici i zamagljuje o cemu konstruktor zapravo ovisi.
Good example
| 1 | class Widget { |
| 2 | constructor({ el, containerEl }) { |
| 3 | this.el = el; |
| 4 | this.containerEl = containerEl; // single source of truth |
| 5 | } |
| 6 | } |
Explanation (EN)
Storing the passed containerEl keeps a single source of truth, makes the dependency explicit, and lets the caller control which container is used.
Objašnjenje (HR)
Spremanje proslijedenog containerEl cuva jedan izvor istine, cini ovisnost eksplicitnom i pusta pozivatelja da kontrolira koji se kontejner koristi.
Notes (EN)
If a relationship genuinely must be derived (e.g. el.parentElement), centralize it in one named helper and guard the result with an invariant/assert so a missing parent fails loudly instead of producing null downstream.
Bilješke (HR)
Ako se odnos stvarno mora izvesti (npr. el.parentElement), centraliziraj ga u jedan imenovani helper i zastiti rezultat invariantom/assertom tako da nedostajuci roditelj glasno padne umjesto da proizvede null nizvodno.