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 make parameters or fields optional without a real consumer that omits them
Mark a parameter, prop, or field optional only when an actual caller legitimately omits it; default to required to keep the contract strict and let the compiler catch missing arguments.
Bad example
| 1 | // Speculatively optional 'to keep it reusable', |
| 2 | // but every consumer actually passes it. |
| 3 | interface SectionProps { |
| 4 | hasActiveFilters: boolean; |
| 5 | label: string; |
| 6 | onClear?: () => void; // optional with no consumer that omits it |
| 7 | } |
| 8 |
|
| 9 | function Section({ hasActiveFilters, label, onClear }: SectionProps) { |
| 10 | // Now forced to guard a value that is in practice always present. |
| 11 | return hasActiveFilters && onClear ? <button onClick={onClear}>{label}</button> : null; |
| 12 | } |
Explanation (EN)
Marking onClear optional was justified as future-proofing, but every call site supplies it. The compiler can no longer flag a forgotten argument, and each consumer must handle an undefined that never actually occurs.
Objašnjenje (HR)
Oznaka onClear kao opcionalna opravdana je 'za buducu ponovnu upotrebu', no svaki pozivatelj ga ipak prosljeduje. Kompajler vise ne moze upozoriti na zaboravljeni argument, a svaki potrosac mora obraditi undefined koji se zapravo nikad ne pojavljuje.
Good example
| 1 | // Required because every consumer needs it. |
| 2 | interface SectionProps { |
| 3 | hasActiveFilters: boolean; |
| 4 | label: string; |
| 5 | onClear: () => void; // required: contract matches reality |
| 6 | } |
| 7 |
|
| 8 | function Section({ hasActiveFilters, label, onClear }: SectionProps) { |
| 9 | return hasActiveFilters ? <button onClick={onClear}>{label}</button> : null; |
| 10 | } |
Explanation (EN)
Since every consumer needs onClear, it is required. The compiler now enforces that every call site provides it, and the body no longer needs a defensive guard for a value that is always present.
Objašnjenje (HR)
Buduci da onClear treba svakom potrosacu, on je obavezan. Kompajler sada osigurava da ga svako mjesto poziva prosljeduje, a tijelo vise ne treba obrambenu provjeru za vrijednost koja je uvijek prisutna.
Notes (EN)
Applies equally to backend function signatures, request/response DTOs, service-method options objects, and config types. YAGNI: add optionality when a real consumer needs it, not before.
Bilješke (HR)
Jednako vrijedi za backend potpise funkcija, request/response DTO-ove, options objekte servisnih metoda i konfiguracijske tipove. YAGNI: opcionalnost dodajte kad ju stvarni potrosac treba, ne prije.
Exceptions / Tradeoffs (EN)
Keep a member optional when there is a genuine consumer that omits it (e.g. an opt-in callback, a config override with a sensible default, or a backward-compatible addition to a published API). Drive the decision from real call sites, not from imagined future reuse.
Iznimke / Tradeoffi (HR)
Zadrzite clan opcionalnim kad postoji stvarni potrosac koji ga izostavlja (npr. opcionalni callback, override konfiguracije sa smislenim zadanim vrijednostima ili unatrag kompatibilan dodatak objavljenom API-ju). Odluku temeljite na stvarnim pozivima, a ne na zamisljenoj buducoj ponovnoj upotrebi.