DEV Community

Cover image for Code Smell 292 - Missing Return
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

1

Code Smell 292 - Missing Return

When your code loses its way

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
  6. 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)
}
Enter fullscreen mode Exit fullscreen mode

Right 👉

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

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay