Skip to main content

Command Palette

Search for a command to run...

Code Smell 292 - Missing Return

When your code loses its way

Updated
β€’3 min read
Code Smell 292 - Missing Return
M

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

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)
}

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

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.

Code Smells

Part 26 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.

Up next

Code Smell 291 - Mutable Keys

Changing Keys, Losing Values