Rules Hub

Coding Rules Library

← Back to all rules
fullstack ruleStack: typescript
clean-codemaintainabilityrefactoringbest-practices

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.

PR: Feat/FCK-1867 - Add Bellsheep and Top 50 tabs as Standalone pages #1360Created: Dec 8, 2025

Bad example

Old codetsx
1function 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

New codetsx
1// Good: Single source of truth using a constant object or enum
2const TickerTabs = {
3 BELLSHEEP: 'bjellesauerApp',
4 HISTORY: 'historikkApp',
5} as const;
6
7function 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.