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).
Avoid magic strings in control flow
Use named constants or enums instead of raw string literals in conditional logic to prevent typos and ease refactoring.
Bad example
| 1 | function getTabComponent(tab: string) { |
| 2 | // Bad: Magic strings are brittle and hard to maintain |
| 3 | switch (tab) { |
| 4 | case 'bjellesauerApp': |
| 5 | return <BellsheepApp />; |
| 6 | case 'historikkApp': |
| 7 | return <HistoryApp />; |
| 8 | default: |
| 9 | return null; |
| 10 | } |
| 11 | } |
Explanation (EN)
Hardcoding string literals like 'bjellesauerApp' directly in the control flow makes the code error-prone. A single typo will break the logic, and searching for all occurrences during refactoring is manual and risky.
Objašnjenje (HR)
Tvrdo kodiranje stringova poput 'bjellesauerApp' izravno u logici čini kod podložnim greškama. Jedan tipfeler može slomiti logiku, a traženje svih pojavljivanja tijekom refaktoriranja je ručno i rizično.
Good example
| 1 | // Good: Single source of truth using a constant object or enum |
| 2 | const TickerTabs = { |
| 3 | BELLSHEEP: 'bjellesauerApp', |
| 4 | HISTORY: 'historikkApp', |
| 5 | } as const; |
| 6 |
|
| 7 | function getTabComponent(tab: string) { |
| 8 | switch (tab) { |
| 9 | case TickerTabs.BELLSHEEP: |
| 10 | return <BellsheepApp />; |
| 11 | case TickerTabs.HISTORY: |
| 12 | return <HistoryApp />; |
| 13 | default: |
| 14 | return null; |
| 15 | } |
| 16 | } |
Explanation (EN)
Defining values in a named constant object or enum creates a single source of truth. This prevents typos, improves readability, and allows for safe, automated refactoring across the codebase.
Objašnjenje (HR)
Definiranje vrijednosti u imenovanom objektu ili enumu stvara jedan izvor istine. To sprječava tipfelere, poboljšava čitljivost i omogućuje sigurno, automatizirano refaktoriranje kroz cijelu bazu koda.