Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Security Toolsholos v0.6.3(21.09.2026 um 12:28 Uhr)
IT Security NachrichtenSAML: A fractal of bad design(21.09.2026 um 13:00 Uhr)
Malware / Trojaner / VirenMacSync-Variante: Kaspersky warnt vor neuem macOS-Infostealer - BornCity(21.09.2026 um 11:12 Uhr)
IT Security NachrichtenShinyHunters hacks rival extortion gang and takes over its dark web site(21.09.2026 um 13:02 Uhr)
IT Security NachrichtenUS and China Discuss Alerting Each Other to AI National Security Threats(21.09.2026 um 13:02 Uhr)
IT Security Toolsholos v0.6.3(21.09.2026 um 12:28 Uhr)
IT Security NachrichtenSAML: A fractal of bad design(21.09.2026 um 13:00 Uhr)
Malware / Trojaner / VirenMacSync-Variante: Kaspersky warnt vor neuem macOS-Infostealer - BornCity(21.09.2026 um 11:12 Uhr)
IT Security NachrichtenShinyHunters hacks rival extortion gang and takes over its dark web site(21.09.2026 um 13:02 Uhr)
IT Security NachrichtenUS and China Discuss Alerting Each Other to AI National Security Threats(21.09.2026 um 13:02 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Have you considered using Object Literals in typescript as a replacement for the If..Else or Switch..Case.. ?

Using If Else is a super essential. But there are times when if becomes less readable and hard to scale as the number of conditions increase. As a replacement, we tend to use Switch Cases. But, with the inception of typescript the chances…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Image description



Using If Else is a super essential. But there are times when if becomes less readable and hard to scale as the number of conditions increase. As a replacement, we tend to use Switch Cases. But, with the inception of typescript the chances to write cleaner and readable code has shifted to another level. At least I find it more readable ( Maybe this is opinionated..Feel free to present your views on this ).



Now, let’s get straight to the heart of this article. Let me show you an implementation of a piece of code in three different versions.



Basically, this code transforms any sentence into different cases ( smallcase, uppercase, titlecase etc ).






Version — 1 ( If..Else.. )






export const transformCase = ({ text, format }: TransformCaseTypeProps) => {
if (format === "small") { //smallcase
return text.toLocaleLowerCase();
} else if (format === "title") { //titlecase
return text
.split(" ")
.map(
(word) => word.toLowerCase().charAt(0).toUpperCase() + word.slice(1)
)
.join(" ");
} else if (format === "upper") { //uppercase
return text.toUpperCase();
} else {
return text; // default case
}
};






This if…else code will work just fine. Also, I am not saying that using this isn’t a valid choice. But, there is also a better way with less code and a cleaner layout. Moreover, there are ways to stop the repetition of code ( like in here the if..else will just tend to increase in numbers as the conditions grow ).






Version — 2 ( Switch..Case.. )






export const transformCase = ({ text, format }: TransformCaseTypeProps) => {
switch (format) {
case "small": // smallcase
return text.toLocaleLowerCase();
case "title": // titlecase
return text
.split(" ")
.map(
(word) => word.toLowerCase().charAt(0).toUpperCase() + word.slice(1)
)
.join(" ");
case "upper": // uppercase
return text.toUpperCase();
default: // default case
return text;
}
};






This version with the switch case can surely be considered a bit cleaner because of group related cases in a structured and organized format, making it easier to follow the logic for multiple conditions in a better way. Additionally, it is easy to add new cases ( just add a new case block ) comparing to the if..else version. Nevertheless, this still requires multiple case ( repetition of code ) statements, as well as, explicit return for each case. All these making it harder to focus on the core functionality.






Version — 3 ( Object Literal )






export const transformCase = ({ text, caseName }: TransformCaseTypeProps) => {
const formatters = {
small: () => text.toLocaleLowerCase(), //smallcase
title: () => //titlecase
text
.split(" ")
.map(
(word) => word.toLowerCase().charAt(0).toUpperCase() + word.slice(1)
)
.join(" "),
upper: () => text.toUpperCase(), //uppercase
default: () => text,
};

const transform = formatters[caseName];
if (!transform) {
throw new Error(
`Invalid format: "${caseName}". Allowed formats are: ${Object.keys(
formatters
).join(", ")}`
);
}






Now, this is my suggested approach. Following are the key points based on my point of views —






Separation of Concerns



Each cases (small, title, upper, etc.) is encapsulated in its own function within the object, demonstrating the separation of concern.






Extensibility



Adding new cases is as simple as appending a new key-value pair in the object. Each new case is self-contained and doesn’t require modifying control flow logic where on the other versions, adding a new case would mean to repeat the a block ( new if..else OR new switch..case .. ), increasing code complexity as more cases are added. On top of that, this is cleaner, minimal, and better structured.



Hope you will be able to find more better aspects of this approach or maybe an entire way which is better. Feel free to share your thoughts. Ciao!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Have you considered using Object Literals in typescript as a replacement for the If..Else or Switch..Case.. ?

Thematisch verwandte Begriffe: Have, considered, using, Object · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94040 | A flaw has been found in vas3k TaxHacker up to 0.8.5. Affected by this v…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick