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).
Don't pass values a function can already reach from its scope
If a function can read a value from config, closure, or module scope, don't also accept it as a parameter. The extra parameter is redundant and can drift out of sync with the real source.
Bad example
| 1 | import { config } from './config'; |
| 2 |
|
| 3 | function buildUrl(path: string, isLocal: boolean) { |
| 4 | return isLocal ? `http://localhost${path}` : `https://prod${path}`; |
| 5 | } |
| 6 |
|
| 7 | buildUrl('/x', config.isLocal); |
Explanation (EN)
isLocal is already reachable via config, so passing it in duplicates the source of truth and forces every caller to supply it.
Objašnjenje (HR)
isLocal je vec dostupan preko configa, pa njegovo prosljedivanje duplicira izvor istine i prisiljava svakog pozivatelja da ga dostavi.
Good example
| 1 | import { config } from './config'; |
| 2 |
|
| 3 | function buildUrl(path: string) { |
| 4 | return config.isLocal ? `http://localhost${path}` : `https://prod${path}`; |
| 5 | } |
| 6 |
|
| 7 | buildUrl('/x'); |
Explanation (EN)
The function reads isLocal from the single source it already has access to, so callers stay simple and the value can't disagree with config.
Objašnjenje (HR)
Funkcija cita isLocal iz jedinog izvora kojem vec ima pristup, pa pozivatelji ostaju jednostavni i vrijednost se ne moze razlikovati od configa.
Exceptions / Tradeoffs (EN)
Keep the parameter when the value genuinely varies per call or when you want the function pure/testable without reaching into shared config.
Iznimke / Tradeoffi (HR)
Zadrzi parametar kada vrijednost stvarno varira po pozivu ili kada zelis cistu/testabilnu funkciju bez posezanja u dijeljeni config.