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).
Name a helper after everything it does, not just its primary transform
If a function does more than its name implies (e.g. also normalizes a protocol prefix on top of converting to absolute), rename it to reflect the full behavior — a narrow name hides intent from the next reader deciding whether it's safe to reuse.
Bad example
| 1 | const toAbsoluteUrl = (baseUrl: string, pathOrUrl: string): string => { |
| 2 | const normalized = /^https?:\/\//.test(pathOrUrl) ? pathOrUrl : `https://${pathOrUrl}`; |
| 3 | return new URL(normalized, `${baseUrl}/`).toString(); |
| 4 | }; |
Explanation (EN)
The name `toAbsoluteUrl` only describes the URL-resolution part. It silently also prepends `https://` to hostname-like strings (e.g. `www.example.com/a`) so `new URL()` doesn't misread them as a relative path — a caller reading the name alone would assume it's a pure resolver and could misuse it or duplicate the protocol-normalization elsewhere.
Objašnjenje (HR)
Ime `toAbsoluteUrl` opisuje samo dio koji rijesava URL. Tiho takoder dodaje `https://` na stringove nalik hostname-u (npr. `www.example.com/a`) kako `new URL()` ne bi krivo procitao kao relativnu putanju — pozivatelj koji cita samo ime pretpostavio bi da je cisti resolver i mogao bi ga krivo koristiti ili duplicirati normalizaciju protokola negdje drugdje.
Good example
| 1 | const normalizeToAbsoluteUrl = (baseUrl: string, pathOrUrl: string): string => { |
| 2 | const normalized = /^https?:\/\//.test(pathOrUrl) ? pathOrUrl : `https://${pathOrUrl}`; |
| 3 | return new URL(normalized, `${baseUrl}/`).toString(); |
| 4 | }; |
Explanation (EN)
`normalizeToAbsoluteUrl` signals that the function does more than URL resolution — it also normalizes the input — so callers know what to expect without reading the implementation.
Objašnjenje (HR)
`normalizeToAbsoluteUrl` signalizira da funkcija radi vise od rijesavanja URL-a — takoder normalizira ulaz — tako da pozivatelji znaju sto ocekivati bez citanja implementacije.
Notes (EN)
This is distinct from over-long names for simple functions — only rename when a secondary behavior is load-bearing and not obvious from the primary name.
Bilješke (HR)
Ovo je razlicito od predugih imena za jednostavne funkcije — preimenuj samo kada je sekundarno ponasanje bitno i nije ocito iz primarnog imena.