Skip to content

Latest commit

 

History

History
179 lines (116 loc) · 9.9 KB

File metadata and controls

179 lines (116 loc) · 9.9 KB

Code Smell 274 - Cascaded Returns

Code Smell 274 - Cascaded Returns

Stop the Return Roller-coaster

TL;DR: Prevent chaining return statements for better code readability and flow.

Problems 😔

  • Confusing flow
  • Debugging Difficulty
  • Buried logic
  • Low readability
  • Risk of errors
  • Overuse of IF Sentences

Solutions 😃

  1. Early return
  2. Clear conditions
  3. Use guard clauses
  4. Replace IFs with Polymorphism

Refactorings ⚙️

Refactoring 014 - Remove IF

Context 💬

When you chain multiple return statements within a function, you create a confusing flow.

This leads to spaghetti code where understanding the exit points becomes hard.

Cascaded returns can hide important logic deep within the function, making it harder to follow and debug.

You read through multiple branches to determine when and where the function ends.

Sample Code 💻

Wrong 🚫

function discount(price, isMember) {
  if (price < 20) {
    if (isMember) {
      return 5;
    } else {
      return 2;
    }
  } else {
    if (isMember) {
      return 10;
    } else {
      return 0;
    }
  }
}

Right 👉

class Member {
  discount(price) {
    return price < 20 ? 5 : 10;
    // This ternary is an essential IF
    // And you should NOT remove it
  }
}

class NonMember {
  discount(price) {
    return price < 20 ? 2 : 0;
    // This ternary is an essential IF
    // And you should NOT remove it
  }
}

function discount(price, status) {
  return status.discount(price);
}

const member = new Member();
const nonMember = new NonMember();

Detection 🔍

[X] Automatic

You can spot cascaded returns by looking for multiple nested return statements.

If you see deep indentation or many layers of conditions, that's a sign of this code smell.

Tags 🏷️

  • IFs

Level 🔋

[X ] Beginner

AI Generation 🤖

AI generators might create this smell when tasked with solving complex problems quickly.

Cascaded returns often happen when the generator handles multiple conditions without optimizing the flow.

AI Detection 🥃

With clear instructions, AI tools can avoid cascaded returns.

You can ask the AI to use guard clauses, polymorphism and simplify returns for a cleaner solution.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Replace the Cascaded IF sentences with Polymorphism

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

Conclusion 🏁

Avoid cascaded returns to make your code more readable, maintainable, and easier to debug.

Stick to early returns and guard clauses to prevent unnecessary complexity.

Relations 👩‍❤️‍💋‍👨

Code Smell 119 - Stairs Code

Code Smell 102 - Arrow Code

Code Smell 78 - Callback Hell

Code Smell 156 - Implicit Else

More Information 📕

How to Get Rid of Annoying IFs Forever

Disclaimer 📘

Code Smells are my opinion.

Credits 🙏

Photo by Mike Lewis HeadSmart Media on Unsplash


Even when a module is old and stable, bad code may be a time bomb and we might defuse it by isolating that code in its own library

Adam Tornhill

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of Your Code