Code Smell 145 - Short Circuit Hack

Photo by Michael Dziedzic on Unsplash

Code Smell 145 - Short Circuit Hack

Don't use boolean evaluation as a readability shortcut

Maxi Contieri
·Jun 30, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems
  • Solutions
  • Context
  • Sample Code
  • Detection
  • Tags
  • Conclusion
  • Relations
  • Credits

TL;DR: Don't use Boolean comparison for side effect functions.


  • Readability

  • Side Effects


  1. Convert short circuit into IFs


Smart programmers like to write hacky and obscure code even when there is no strong evidence for this improvement.

Premature optimization always hurts readability.

Sample Code


userIsValid() && logUserIn();

// this expression is short circuit
// Does not value second statament
// Unless the first one is true

functionDefinedOrNot && functionDefinedOrNot();

// in some languages undefined works as a false
// If functionDefinedOrNot is not defined does
// not raise an erron and neither runs


if (userIsValid()) {

if(typeof functionDefinedOrNot == 'function') {  
// Checking for a type is another code smell


[X] Semi-Automatic

We can check if the functions are impure and change the short circuit to an IF.

Some actual linters warn us of this problem


  • Premature Optimizacion


Don't try to look smart.

We are not in the 50s anymore.

Be a team developer.



Photo by Michael Dziedzic on Unsplash

A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match.

Bill Bryson

This article is part of the CodeSmell Series.

Share this