# Code Smell 292 - Missing Return

> TL;DR: Missing return statements cause unexpected behavior.

# Problems 😔

- Silent failures
- Unreliable results
- Hard debugging
- Inconsistent and misleading behavior
- Broken logic

# Solutions 😃

1. Always return values
2. Use clear flow
3. Validate conditions
4. Test all return paths
5. Use [early returns](https://maximilianocontieri.com/code-smell-156-implicit-else)
6. Remove [IFs](https://maximilianocontieri.com/refactoring-014-remove-if)

# Refactorings ⚙️

%[https://maximilianocontieri.com/refactoring-014-remove-if]

# Context 💬

When you forget to return a value, your function keeps executing and your app might show incomplete or wrong information.

# Sample Code 📖

## Wrong ❌

<!-- [Gist Url](https://gist.github.com/mcsee/85ddf9b20f667119b5a7d57a512ee1c1) -->

```kotlin
fun totalDistance(activity: Activity): Double {
    if (activity.type == "Running") {
        activity.calculateDistance() 
        // Missing return here
    } else {
        return 0.0
    }
    // Other options are omitted for simplicity
    // Some languages raise a runtime error 
    // If the function does not return a value
    // of the correct type (in this case a Double)
}
```

## Right 👉

<!-- [Gist Url](https://gist.github.com/mcsee/f979ec3c11052161c87d9498a9e641a9) -->

```kotlin
fun totalDistance(activity: Activity): Double {
    if (activity.type == "Running") {
        return activity.calculateDistance() 
        // Now it returns the value
    } else {
        return 0.0
    }
}
```

# Detection 🔍

[X] Automatic 

You can detect this smell when your function lacks a return statement in certain branches. 

Most static analyzers and linters often catch this.

# Tags 🏷️

- IFs

# Level 🔋

[x] Beginner

# Why the Bijection Is Important 🗺️

it's important to maintain a clear and predictable relationship
between your code and the [Real World](https://maximilianocontieri.com/the-one-and-only-software-design-principle).

If a function is intended to calculate and return a value, it should always do so.  

Failing to return a value breaks the [MAPPER](https://maximilianocontieri.com/what-is-wrong-with-software), leading to inaccurate behavior and unreliable results.

# AI Generation 🤖

AI tools usually don't generate this smell.

# AI Detection 🥃

Most AI-powered linters quickly catch missing returns with static analysis or by examining your code's [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree).

## 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%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [ChatGPT](https://chat.openai.com/?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) |
| [Claude](https://claude.ai/new?q=Correct+and+explain+this+code%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [Claude](https://claude.ai/new?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) |
| [Perplexity](https://www.perplexity.ai/?q=Correct+and+explain+this+code%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [Perplexity](https://www.perplexity.ai/?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%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%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) |
| [Gemini](https://gemini.google.com/?q=Correct+and+explain+this+code%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [Gemini](https://gemini.google.com/?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | 
| [DeepSeek](https://chat.deepseek.com/?q=Correct+and+explain+this+code%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [DeepSeek](https://chat.deepseek.com/?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | 
| [Meta AI](https://www.meta.ai/chat?q=Correct+and+explain+this+code%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | [Meta AI](https://www.meta.ai/?q=correct+the+missing+return%3A+%60%60%60kotlin%0D%0Afun+totalDistance%28activity%3A+Activity%29%3A+Double+%7B%0D%0A++++if+%28activity.type+%3D%3D+%22Running%22%29+%7B%0D%0A++++++++activity.calculateDistance%28%29+%0D%0A++++++++%2F%2F+Missing+return+here%0D%0A++++%7D+else+%7B%0D%0A++++++++return+0.0%0D%0A++++%7D%0D%0A++++%2F%2F+Other+options+are+omitted+for+simplicity%0D%0A++++%2F%2F+Some+languages+raise+a+runtime+error+%0D%0A++++%2F%2F+If+the+function+does+not+return+a+value%0D%0A++++%2F%2F+of+the+correct+type+%28in+this+case+a+Double%29%0D%0A%7D%0D%0A%60%60%60) | 

# Conclusion 🏁

A missing return statement breaks your code’s flow and produces unreliable results. 

Always ensure every branch in your function returns something meaningful.

# Relations 👩‍❤️‍💋‍👨

%[https://maximilianocontieri.com/code-smell-73-exceptions-for-expected-cases]

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

%[https://maximilianocontieri.com/code-smell-115-return-true]

%[https://maximilianocontieri.com/code-smell-118-return-false]

%[https://maximilianocontieri.com/code-smell-186-hardcoded-business-conditions]

%[https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements]

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

# 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 [Tim Johnson](https://unsplash.com/@mangofantasy) on [Unsplash](https://unsplash.com/photos/white-and-gray-animal-on-gray-rocky-mountain-during-daytime-ywIZ8ZYxzWU)
         
* * *

> A bug is never just a mistake. It represents something bigger.

_Sergey Zefirov_
 
%[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]
