DEV Community

Cover image for Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

3 1

Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)

This handy operator is a trouble maker.

TL;DR: Don't mix booleans with non-booleans.

Problems

  • Not Declarative Code

  • Hard to debug

  • Magic Castings

  • Accidental Complexity

Solutions

  1. Be Explicit

  2. Don't mix Booleans with non-booleans.

  3. Fail Fast

  4. Be Smarter than your compiler.

  5. Stay loyal to the bijection.

Sample Code

Wrong

!true // returns false
!false // returns true

isActive = true 
!isActive // returns false

age = 54
!age // returns false
array = []
!array // returns false
obj = new Object;
!obj // returns false

!!true // returns true
!!false // returns false

!!isActive // returns true
!!age // returns true
!!array // returns true
!!obj // returns true
Enter fullscreen mode Exit fullscreen mode

Right


!true // returns false
!false // returns true

isActive = true 
!isActive // returns false

age = 54
!age // should return type mismatch (or 54 factorial!)
array = []
!array // should return type mismatch
obj = new Object;
!obj // should return type mismatch (what is an obejct negated in a real domain?)

!!true // returns true - it is idempotent
!!false // returns false - it is idempotent

!!isActive // returns true - it is idempotent
!!age // nonsense
!!array // nonsense
!!obj // nonsense
Enter fullscreen mode Exit fullscreen mode

Detection

Since this is a "feature" in some languages it would be hard to test. We can set programming policies or choose more strict languages.

We should detect ! !! usages in non-boolean objects and warn our programmers.

Tags

  • Casting

  • Coercion

  • Javascript

Conclusion

Languages like JavaScript divide their whole universe into true or false values. This decision hides errors when dealing with non booleans.

We should be very strict and keep booleans (and their behavior), far away from non booleans.

Relations

More info

Reddit

Double Bang

Mozilla

Credits

Photo by Greg Rakozy on Unsplash


It is easier to write an incorrect program than understand a correct one.

Alan J Perlis


Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay