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).
Derive repeated values from a source field instead of hand-writing each one
When a field's value is always computable from another field, derive it with a shared function rather than typing it out per entry.
Bad example
| 1 | const BASE_URL = 'https://cdn.example.com/tags'; |
| 2 |
|
| 3 | export const tags = [ |
| 4 | { id: 1, name: 'Sports', urlname: 'sports', iconUrl: `${BASE_URL}/sports.png` }, |
| 5 | { id: 2, name: 'Finance', urlname: 'finance', iconUrl: `${BASE_URL}/finace.png` }, // typo, doesn't match urlname |
| 6 | ]; |
Explanation (EN)
Each `iconUrl` is manually typed out to mirror `urlname`, so nothing stops the two from drifting apart — as the typo `finace.png` shows. Every new entry also repeats the same base-URL-plus-slug pattern by hand.
Objašnjenje (HR)
Svaki `iconUrl` je ručno upisan da oponaša `urlname`, pa ništa ne sprječava da se ta dva podatka razmaknu — kao što pokazuje tipfeler `finace.png`. Svaki novi unos ponavlja isti obrazac base-URL-plus-slug ručno.
Good example
| 1 | const BASE_URL = 'https://cdn.example.com/tags'; |
| 2 | const tagIconUrl = (urlname: string): string => `${BASE_URL}/${urlname}.png`; |
| 3 |
|
| 4 | export const tags = [ |
| 5 | { id: 1, name: 'Sports', urlname: 'sports' }, |
| 6 | { id: 2, name: 'Finance', urlname: 'finance' }, |
| 7 | ].map(tag => ({ ...tag, iconUrl: tagIconUrl(tag.urlname) })); |
Explanation (EN)
`iconUrl` is computed from `urlname` through one shared function, so the two values can never drift apart and adding a new entry only requires the base data, not a hand-derived URL.
Objašnjenje (HR)
`iconUrl` se izračunava iz `urlname` kroz jednu zajedničku funkciju, pa se ta dva podatka nikad ne mogu razmaknuti, a dodavanje novog unosa zahtijeva samo osnovne podatke, a ne ručno izvedeni URL.
Exceptions / Tradeoffs (EN)
If the derived values genuinely don't follow a consistent pattern (e.g. external, arbitrary URLs per item), hand-writing them is appropriate.
Iznimke / Tradeoffi (HR)
Ako izvedene vrijednosti stvarno ne prate dosljedan obrazac (npr. vanjski, proizvoljni URL-ovi po stavci), ručno upisivanje je opravdano.