Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1universalStack: TypeScript
dryrefactoringcontrol-flow
Collapse repeated conditional branches into a helper plus a switch
When many if-blocks repeat the same shape with different values, extract the shared work into one function and drive it with a switch.
PR: hegnar-bellsheep-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codets
| 1 | if (value === 'day') { setRange(...); setCalendar(...); } |
| 2 | if (value === 'week') { setRange(...); setCalendar(...); } |
| 3 | if (value === 'month') { setRange(...); setCalendar(...); } |
| 4 | // ...repeated for every case |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const apply = (from?: Date, to?: Date) => { setRange(from, to); setCalendar({ from, to }); }; |
| 2 | switch (value) { |
| 3 | case 'day': apply(new Date(), new Date()); break; |
| 4 | case 'week': apply(subWeeks(new Date(), 1), new Date()); break; |
| 5 | default: break; |
| 6 | } |
Explanation (EN)
Objašnjenje (HR)