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
eventsdefensive-programmingoptional-chaining
Guard event handlers against shape changes and duplicate fires
When reacting to external events, defensively access nested fields and short-circuit when the relevant value hasn't actually changed, since events can fire repeatedly.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | player.on(e => { |
| 2 | this.setState({ title: e.event.video.title }); |
| 3 | }); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | player.on(e => { |
| 2 | const title = e?.event?.video?.title; |
| 3 | if ((e?.eventName === 'play' || e?.eventName === 'resume') && title !== this.state.title) { |
| 4 | this.setState({ title }); |
| 5 | } |
| 6 | }); |
Explanation (EN)
Objašnjenje (HR)