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).
Compose strings from optional parts via filter(Boolean).join(' ')
When concatenating optional segments (names, addresses), don't interpolate with ?? '' — that yields double spaces or 'undefined'. Put parts in an array, filter(Boolean), and join with a single space.
Bad example
| 1 | const name = `${person.firstName} ${person.middleName ?? ''} ${person.lastName}`; |
| 2 | // 'John Doe' (double space) when middleName is missing |
Explanation (EN)
When middleName is undefined the ?? '' leaves an extra space between the remaining parts, producing 'John Doe'.
Objašnjenje (HR)
Kad je middleName undefined, ?? '' ostavlja dodatni razmak izmedu preostalih dijelova, dajuci 'John Doe'.
Good example
| 1 | const name = [person.firstName, person.middleName, person.lastName] |
| 2 | .filter(Boolean) |
| 3 | .join(' '); |
Explanation (EN)
Falsy segments are dropped before joining, so the result always has exactly one space between present parts.
Objašnjenje (HR)
Falsy segmenti se izbacuju prije spajanja, pa rezultat uvijek ima tocno jedan razmak izmedu prisutnih dijelova.