DEV Community

Cover image for Code Smell 118 - Return False
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 118 - Return False

Checking for a boolean condition to return a boolean value is awkward

TL;DR: Don't return explicit booleans. Most boolean usages are code smells.

Problems

  • Declarativeness

  • Ninja Code

  • Implementative solutions

Solutions

  1. Return a boolean proposition instead of checking a negation.

  2. Answer must be a business logic formula, not an algorithm.

Context

When dealing with boolean formulas, it is more readable to show a business boolean formula than introduce a negated IF clause.

Programmers tend to return accidental implementative solutions instead of real business rules.

Sample Code

Wrong

function canWeMoveOn() {
  if (work.hasPendingTasks())
    return false;
  else
    return true;
}
Enter fullscreen mode Exit fullscreen mode

Right

function canWeMoveOn() {
  return !work.hasPendingTasks();
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

Based on syntax trees, we can safely refactor the code.

Tags

  • Boolean

Conclusion

Beware of returning booleans.

After the return, you will need an If statement which is also a code smell.

Relations

More Info

Credits

Photo by Morgan Housel on Unsplash

Thanks to Nico K. for this suggestion.


It's not at all important to get it right the first time. It's vitally important to get it right the last time.

Andrew Hunt


This article is part of the CodeSmell Series.

Top comments (5)

Collapse
 
grahamthedev profile image
GrahamTheDev

I need to stop reading this series…my code doesn’t smell it stinks I realise as I seem to break every rule 🤣❤️

Collapse
 
mcsee profile image
Maxi Contieri

Articles are guides and suggestions.
Don't take it too hard

Collapse
 
grahamthedev profile image
GrahamTheDev

I was kidding, I am enjoying the series, it was self deprecating humour! (Or meant to be 🤣)

Thread Thread
 
otumianempire profile image
Michael Otu • Edited

I have and I am learning a lot from these series. @mcsee a question please (your opinion)

say we have the code:

function canWeMoveOn() {
  if (work.hasPendingTasks())
    return false;
  else
    return true;
}
Enter fullscreen mode Exit fullscreen mode

All we want is the result of work.hasPendingTasks() and in this post, you suggested that it is better not to explicitly return a boolean.

So you refactored it to:

function canWeMoveOn() {
  return !work.hasPendingTasks();
}

Enter fullscreen mode Exit fullscreen mode

Now, the refactored code looks like a wrapper around work.hasPendingTasks(); and question is that, when it happens that we have a simple single lines like, it is okay to wrap them up? I mean, we could used the work.hasPendingTasks(); directly. What do you think or encourage? Thanks

Thread Thread
 
mcsee profile image
Maxi Contieri • Edited

canWeMoveOn the 'what'

hasPendingTaks the 'how'

they are accidentally coupled, now. But the caller to 'canWeMoveOn' does not know,
If you change your implementation or add new business rules or add a cache the caller should not be aware

I used this example here