🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 2 Min Lesezeit
0

Clean code: why boolean flags in function parameters are a code smell

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Boolean flags in function parameters can make your code harder to read and maintain. Let's see why you should avoid them and what you can do instead.






The problem with boolean flags



Using a boolean parameter often means your function does two different things, breaking the Single Responsibility Principle (SRP). Here's a typical example:




CODE
function createFile(name, isTemp) {
if (isTemp) {
fs.create(`./temp/${name}`);
} else {
fs.create(name);
}
}






This might look simple, but it has several problems:





  1. Unclear function calls: Reading the code, it's hard to know what the boolean means:




CODE
   createFile("log.txt", true);  // What does 'true' mean here?







  1. Two functions in one: The boolean works like a switch, making the function do different things


  2. Testing gets harder: You need to check both ways the function can work


  3. Hard to add features: If you need a third option later, you might add another boolean, making things worse







A better way to write it



Split the function into two separate ones, each doing one thing:




CODE
function createFile(name) {
fs.create(name);
}

function createTempFile(name) {
createFile(`./temp/${name}`);
}






This gives you:




  1. Clear names: createTempFile("log.txt") tells you exactly what it does


  2. Simple logic: Each function does just one thing


  3. Easy testing: You only need to test one thing per function


  4. Simple to add features: Need something new? Add a new function without changing the old ones







More examples



This idea works in many situations. Here are some cases:






Login system






CODE
// ❌ Bad
function authenticate(user, isAdmin) {
if (isAdmin) {
// Admin login logic
} else {
// Regular user login logic
}
}

// ✅ Good
function authenticateUser(user) {
// Regular user login logic
}

function authenticateAdmin(user) {
// Admin login logic
}









Email system






CODE
// ❌ Bad
function sendEmail(user, isHtmlFormat) {
if (isHtmlFormat) {
// Send HTML email
} else {
// Send plain text email
}
}

// ✅ Good
function sendPlainTextEmail(user) {
// Send plain text email
}

function sendHtmlEmail(user) {
// Send HTML email
}









To sum up



Boolean flags in function parameters often show that a function tries to do too much. Making separate, focused functions creates code that is:




  • Easy to read

  • Easy to test

  • Easy to fix

  • Easy to change



Next time you want to add a boolean parameter, think about making two functions instead.






Have you tried splitting functions like this in your code? Did it help? Let me know in the comments!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Clean code: why boolean flags in function parameters are a code smell

Thematisch verwandte Begriffe: Clean, code, boolean, flags · 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 ...