Rules Hub
Coding Rules Library
Avoid naming string identifiers as 'index'
Do not use the name 'index' for variables that hold string identifiers or keys, as it implies numeric position and order dependence.
Bad example
| 1 | const handleTabClick = (index: string) => { |
| 2 | // 'index' implies a numeric position (0, 1, 2) |
| 3 | // but here it actually holds a string ID like 'invoiceData' |
| 4 | if (index === 'invoiceData') { |
| 5 | openModal(); |
| 6 | } |
| 7 | }; |
Explanation (EN)
The variable name 'index' incorrectly suggests a numeric array position. This confuses the reader into thinking the logic depends on the order of elements, leading to unnecessary concerns about code fragility when reordering items.
Objašnjenje (HR)
Ime varijable 'index' pogrešno sugerira numeričku poziciju u nizu. To zbunjuje čitatelja koji može pomisliti da logika ovisi o redoslijedu elemenata, što izaziva nepotrebnu zabrinutost oko stabilnosti koda pri promjeni redoslijeda.
Good example
| 1 | const handleTabClick = (tabId: string) => { |
| 2 | // 'tabId' clearly indicates this is a unique string identifier |
| 3 | // The logic is visibly independent of the item's position |
| 4 | if (tabId === 'invoiceData') { |
| 5 | openModal(); |
| 6 | } |
| 7 | }; |
Explanation (EN)
Renaming the variable to 'tabId' (or 'key') clearly communicates that it holds a unique identifier. This confirms that the logic is based on identity, not position, making the code self-documenting and reducing ambiguity.
Objašnjenje (HR)
Preimenovanje varijable u 'tabId' (ili 'key') jasno komunicira da se radi o jedinstvenom identifikatoru. To potvrđuje da se logika temelji na identitetu, a ne na poziciji, čineći kod samo-dokumentirajućim i smanjujući dvosmislenost.