DEV Community

Cover image for Code Smell 115 - Return True
Maxi Contieri
Maxi Contieri

Posted on • Originally published at

Code Smell 115 - Return True

Booleans are natural code smells. Returning and casting them is sometimes a mistake

TL;DR: Don't return true or false. Be declarative.


  • Readability

  • Primitive Obsession

  • If/Else abuse


  1. Return truth value in a declarative way

  2. Replace IF With polymorphism.


Dealing with low-level abstractions, we usually return booleans.

When we create complex and mature software, we start to forget about this primitive obsession and care about real-world rules and identities.

Sample Code


boolean isEven(int num){
     if(num%2 == 0){
       return true;
    } else {
       return false;}        
Enter fullscreen mode Exit fullscreen mode


boolean isEven(int numberToCheck){
  //We decouple the what (to check for even or odd)
  //With how (the algorithm)
  return (numberToCheck % 2 == 0);     
Enter fullscreen mode Exit fullscreen mode


[X] Automatic

Many linters can check syntactic trees and look for explicit true/value returns


  • Primitive


Search on code libraries for return true statements and try to replace them when possible.


More Info


Photo by engin akyurt on Unsplash

The good news is: Anything is possible on your computer. The bad news is: Nothing is easy.

Ted Nelson

This article is part of the CodeSmell Series.

Top comments (1)

bn_geek profile image
Mohcin Bounouara

Helpful points of view, thank you!