Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2universalStack: javascript
singletonglobalsnullish-coalescingreadability
Lazily initialize a shared global with nullish assignment
Create-or-reuse a shared global instance (like an IntersectionObserver) with ??= so multiple callers share one instance, expressed in a single readable line.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codejavascript
| 1 | if (!window.parallaxObserver) { |
| 2 | window.parallaxObserver = new IntersectionObserver(cb); |
| 3 | } |
| 4 | window.parallaxObserver.observe(el); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codejavascript
| 1 | window.parallaxObserver ??= new IntersectionObserver(cb); |
| 2 | window.parallaxObserver.observe(el); |
Explanation (EN)
Objašnjenje (HR)