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 ruleP1universalStack: javascript
defensive-codingguardsbugs
Place dependent side effects inside their existence/crash guard
Code that depends on a global or object being present must sit inside the guard that checks for it, otherwise the crash-prevention check is bypassed. Split into separate conditions when one guard can't cover both concerns.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | const user = window.tp.pianoId.getUser(); // runs even when window/tp is undefined |
| 2 | if (window) { ... } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | if (window?.tp?.pianoId) { |
| 2 | const user = window.tp.pianoId.getUser(); |
| 3 | ... |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)