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).
Dedup with a clear early-return, not timer/microtask tricks
Guard repeated work by checking an 'already done' set and returning early, instead of 0ms timers or microtask scheduling that are hard to follow.
Bad example
| 1 | const hasQueuedRef = useRef(false); |
| 2 |
|
| 3 | const register = (node) => { |
| 4 | cancelScheduledRelease(id); |
| 5 | if (queuedIds.has(id)) { |
| 6 | hasQueuedRef.current = true; // ignore the first run, schedule deferred cleanup |
| 7 | return; |
| 8 | } |
| 9 | queuedIds.add(id); |
| 10 | setTimeout(() => doRegister(id), 0); // 0ms timer to dodge the double run |
| 11 | }; |
Explanation (EN)
The timer plus extra ref flag obscures intent; you can't read the logic top-to-bottom because the real work is deferred and conditioned on which run this is.
Objašnjenje (HR)
Timer uz dodatni ref flag zamagljuje namjeru; logiku ne možeš čitati odozgo prema dolje jer je pravi posao odgođen i uvjetovan time koji je ovo prolaz.
Good example
| 1 | const register = (node) => { |
| 2 | if (!node) return; |
| 3 | if (registeredIds.has(id)) return; // already done -> no-op |
| 4 | registeredIds.add(id); |
| 5 | doRegister(id); |
| 6 | return () => releaseId(id); |
| 7 | }; |
Explanation (EN)
Reading top to bottom: skip if no node, skip if already registered, otherwise register and return cleanup. No timers, no run-counting flags.
Objašnjenje (HR)
Čitajući odozgo: preskoči ako nema nodea, preskoči ako je već registrirano, inače registriraj i vrati čišćenje. Bez timera, bez brojanja prolaza.