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).
Give same-typed ids entity-qualified names so the wrong one cannot be compared by accident
Several numeric ids for different entities in one scope, one of them named just `id`, type-check against each other perfectly - so a comparison against the wrong entity produces a silent logic bug no tool can catch.
Bad example
| 1 | const Dialog = ({ id, currentCategory, categories }: Props) => ( |
| 2 | <Autocomplete |
| 3 | options={categories} |
| 4 | // `id` here is the instrument id - comparing it to a category id is always wrong |
| 5 | renderOption={(props, option) => <li {...props} aria-disabled={option.id === id} />} |
| 6 | /> |
| 7 | ); |
Explanation (EN)
Both values are numbers, so the compiler accepts the comparison. The prop that was meant for the check is passed in but never used, and the option is disabled in the wrong cases.
Objašnjenje (HR)
Obje vrijednosti su brojevi, pa kompajler prihvaca usporedbu. Prop namijenjen provjeri se prosljeduje, ali se nikad ne koristi, i opcija se onemogucuje u krivim slucajevima.
Good example
| 1 | const Dialog = ({ instrumentId, currentCategoryId, categories }: Props) => ( |
| 2 | <Autocomplete |
| 3 | options={categories} |
| 4 | renderOption={(props, option) => ( |
| 5 | <li {...props} aria-disabled={option.id === currentCategoryId} /> |
| 6 | )} |
| 7 | /> |
| 8 | ); |
Explanation (EN)
Each id names the entity it belongs to, so reading the comparison is enough to see whether it is right, and an unused prop stands out immediately.
Objašnjenje (HR)
Svaki id imenuje entitet kojem pripada, pa je citanje usporedbe dovoljno da se vidi je li ispravna, a neiskoristen prop odmah upada u oci.
Notes (EN)
Branded or nominal id types make this a compile error, but naming alone fixes most of it. A prop that is passed and never read is a strong hint the wrong variable is being used somewhere.
Bilješke (HR)
Brendirani ili nominalni tipovi id-eva ovo pretvaraju u gresku prevodenja, ali samo imenovanje rjesava vecinu. Prop koji se prosljeduje, a nikad ne cita, jak je nagovjestaj da se negdje koristi kriva varijabla.