# Code Smell 274 - Cascaded Returns

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

# Problems

- Confusing flow
- Hard to debug
- Buried logic
- Low readability
- Risk of errors
- Abuse of IF Sentences

# Solutions

1. Early returns
2. Clear conditions
3 Use guard clauses
4 Repace IFs with [Polymorphism](https://maximilianocontieri.com/refactoring-014-remove-if)

# Refactorings

%[https://maximilianocontieri.com/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

[Gist Url]: # (https://gist.github.com/mcsee/edb3a375c0b798f14447fe4c7230d2e8)

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

## Right

[Gist Url]: # (https://gist.github.com/mcsee/b3b1ce40384367e300872e989faf6ead)

```javascript
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

- IF

# 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*

| Without Proper Instructions    | With Specific Instructions |
| -------- | ------- |
| [ChatGPT](https://chat.openai.com/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | [ChatGPT](https://chat.openai.com/?q=Replace+the+Cascaded+IF+sentences+with+Polymorphism%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) |
| [Claude](https://claude.ai/new?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | [Claude](https://claude.ai/new?q=Replace+the+Cascaded+IF+sentences+with+Polymorphism%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) |
| [Perplexity](https://perplexity.ai/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | [Perplexity](https://perplexity.ai/?q=Replace+the+Cascaded+IF+sentences+with+Polymorphism%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) |
| [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=Replace+the+Cascaded+IF+sentences+with+Polymorphism%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) |
| [Gemini](https://gemini.google.com/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | [Gemini](https://gemini.google.com/?q=Replace+the+Cascaded+IF+sentences+with+Polymorphism%3A+%60%60%60javascript%0D%0Afunction+discount%28price%2C+isMember%29+%7B%0D%0A++if+%28price+%3C+20%29+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+5%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+2%3B%0D%0A++++%7D%0D%0A++%7D+else+%7B%0D%0A++++if+%28isMember%29+%7B%0D%0A++++++return+10%3B%0D%0A++++%7D+else+%7B%0D%0A++++++return+0%3B%0D%0A++++%7D%0D%0A++%7D%0D%0A%7D%0D%0A%60%60%60) | 

# 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

%[https://maximilianocontieri.com/code-smell-119-stairs-code]

%[https://maximilianocontieri.com/code-smell-102-arrow-code]

%[https://maximilianocontieri.com/code-smell-78-callback-hell]

%[https://maximilianocontieri.com/code-smell-156-implicit-else]

# More Info

%[https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Mike Lewis HeadSmart Media](https://unsplash.com/@mikeanywhere) on [Unsplash](https://unsplash.com/photos/waterfall-at-daytime-waAAaeC9hns)
    
* * *

> 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
