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).
Drive behavior from a semantic prop, not a hardcoded identifier match
Trigger conditional logic via an explicit prop/flag the caller sets, instead of string-matching a specific id.
Bad example
| 1 | function AdSlot({ id }: { id: string }) { |
| 2 | // Behavior is hardwired to one specific id string. |
| 3 | const shouldScale = id === 'company_desktop-topbanner'; |
| 4 | return <Slot scaled={shouldScale} />; |
| 5 | } |
Explanation (EN)
Coupling behavior to a hardcoded id means the component must know every id that needs the behavior, and adding a new call site requires editing the component. The magic string is brittle: a typo or renamed id silently disables the feature.
Objašnjenje (HR)
Vezanje ponasanja uz fiksno zakodirani id znaci da komponenta mora poznavati svaki id kojem treba to ponasanje, a dodavanje novog mjesta poziva zahtijeva izmjenu komponente. Magicni string je krhak: tipfeler ili preimenovani id tiho onemoguci znacajku.
Good example
| 1 | function AdSlot({ variant }: { variant?: 'topbanner' }) { |
| 2 | // Caller opts in explicitly; component stays agnostic about ids. |
| 3 | const shouldScale = variant === 'topbanner'; |
| 4 | return <Slot scaled={shouldScale} />; |
| 5 | } |
| 6 |
|
| 7 | // Each call site that wants the behavior sets it: |
| 8 | // <AdSlot variant="topbanner" /> |
Explanation (EN)
An explicit prop makes the intent declarative and lets any call site opt in without changing the component. The component no longer carries a list of special-case ids, and the trigger is reusable and discoverable through the type.
Objašnjenje (HR)
Eksplicitni prop cini namjeru deklarativnom i omogucuje bilo kojem mjestu poziva da je ukljuci bez mijenjanja komponente. Komponenta vise ne nosi popis specijalnih id-ova, a okidac je ponovno iskoristiv i vidljiv kroz tip.
Exceptions / Tradeoffs (EN)
Matching on an id is acceptable for one-off, truly singleton cases where no other call site will ever need the behavior and introducing a prop would be over-engineering.
Iznimke / Tradeoffi (HR)
Podudaranje po id-u je prihvatljivo za jednokratne, stvarno jedinstvene slucajeve gdje nijedno drugo mjesto poziva nikada nece trebati to ponasanje i gdje bi uvodjenje propa bilo pretjerano inzenjerstvo.