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).
Derive related boundary conditions from one constant so they cannot drift
Two branches that describe the same concept but hardcode their own boundary drift apart the moment one is edited, and the resulting off-by-one produces a plausible-looking output nobody flags. Name the boundary once and have both branches read it.
Bad example
| 1 | const background = rank < 4 ? RANK_BACKGROUNDS[rank] : DEFAULT_BG; |
| 2 | // ... |
| 3 | const placeholder = `empty-placeholder-[${rank < 3 ? rank : "default"}].png`; |
Explanation (EN)
Rank 3 gets its ranked background flattened onto the generic placeholder, and the asset authored for it is unreachable.
Objašnjenje (HR)
Rang 3 dobiva svoju rangiranu pozadinu na genericnom rezervnom elementu, a resurs napravljen za njega postaje nedostupan.
Good example
| 1 | const RANKED_POSITIONS = 3; // ranks 1..3 have their own assets |
| 2 | const isRanked = rank <= RANKED_POSITIONS; |
| 3 |
|
| 4 | const background = isRanked ? RANK_BACKGROUNDS[rank] : DEFAULT_BG; |
| 5 | const placeholder = `empty-placeholder-[${isRanked ? rank : "default"}].png`; |
Explanation (EN)
One named boundary drives both branches, so they cannot disagree and the intent is readable.
Objašnjenje (HR)
Jedna imenovana granica upravlja objema granama, pa se ne mogu razici, a namjera je citljiva.
Notes (EN)
Promoting duplicated logic into one shared helper is the natural moment to reconcile boundaries like this, because it is the first time both copies are visible side by side.
Bilješke (HR)
Prebacivanje udvostrucene logike u jednu dijeljenu funkciju prirodan je trenutak za uskladivanje ovakvih granica, jer su tada obje kopije prvi put vidljive jedna uz drugu.