Code Smell 292 - Missing Return

Table of contents

TL;DR: Missing return statements cause unexpected behavior.
Problems π
- Silent failures
- Unreliable results
- Hard debugging
- Inconsistent and misleading behavior
- Broken logic
Solutions π
- Always return values
- Use clear flow
- Validate conditions
- Test all return paths
- Use early returns
- Remove IFs
Refactorings βοΈ
Context π¬
When you forget to return a value, your function keeps executing and your app might show incomplete or wrong information.
Sample Code π
Wrong β
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 π
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.
If a function is intended to calculate and return a value, it should always do so.
Failing to return a value breaks the MAPPER, 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.
Try Them! π
Remember: AI Assistants make lots of mistakes
Without Proper Instructions | With Specific Instructions |
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
DeepSeek | DeepSeek |
Meta AI | Meta AI |
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 π©ββ€οΈβπβπ¨
Disclaimer π
Code Smells are my opinion.
Credits π
Photo by Tim Johnson on Unsplash
A bug is never just a mistake. It represents something bigger.
Sergey Zefirov
This article is part of the CodeSmell Series.
Subscribe to my newsletter
Read articles from Maxi Contieri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Maxi Contieri
Maxi Contieri
Iβm a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.