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

Retur…


This content originally appeared on DEV Community and was authored by DEV Community

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

Right

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

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.


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-05T15:29:25+00:00) Code Smell 118 – Return False. Retrieved from https://www.scien.cx/2022/03/05/code-smell-118-return-false/

MLA
" » Code Smell 118 – Return False." DEV Community | Sciencx - Saturday March 5, 2022, https://www.scien.cx/2022/03/05/code-smell-118-return-false/
HARVARD
DEV Community | Sciencx Saturday March 5, 2022 » Code Smell 118 – Return False., viewed ,<https://www.scien.cx/2022/03/05/code-smell-118-return-false/>
VANCOUVER
DEV Community | Sciencx - » Code Smell 118 – Return False. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/05/code-smell-118-return-false/
CHICAGO
" » Code Smell 118 – Return False." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/05/code-smell-118-return-false/
IEEE
" » Code Smell 118 – Return False." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/05/code-smell-118-return-false/. [Accessed: ]
rf:citation
» Code Smell 118 – Return False | DEV Community | Sciencx | https://www.scien.cx/2022/03/05/code-smell-118-return-false/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.