Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 69 — Big Bang (JavaScript Ridiculous Castings)

Maximiliano Contieri
Dev Genius
Published in
2 min readMay 4, 2021

Photo by Greg Rakozy on Unsplash

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

Right

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

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

Alan J Perlis

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response