# Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)

> 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](https://maximilianocontieri.com/fail-fast)

4. Be Smarter than your compiler.

5. Stay loyal to the bijection.

%[https://maximilianocontieri.com/the-one-and-only-software-design-principle]

# Sample Code

## Wrong

%[https://gist.github.com/mcsee/2ba58f618ce930b3d8361596e1a76d28]

## Right

%[https://gist.github.com/mcsee/fef521fa00f16f2e302d2f7ef41bf178]

# 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](https://dev.to/tmaximini/typescript-bang-operator-considered-harmful-3hhi).

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

%[https://maximilianocontieri.com/code-smell-24-boolean-coercions]

%[https://maximilianocontieri.com/code-smell-06-too-clever-programmer]

# More info

%[https://www.reddit.com/r/ProgrammerHumor/comments/6erd7r/the_best_thing_about_a_boolean_is_that_even_if/]

%[https://www.bennadel.com/blog/3858-the-double-bang-operator-and-a-misunderstanding-of-how-javascript-handles-truthy-falsy-values.htm]

%[https://developer.mozilla.org/en-US/docs/Glossary/Truthy]

%[https://www.destroyallsoftware.com/talks/wat]

# Credits

Photo by <a href="https://unsplash.com/@grakozy">Greg Rakozy</a> on <a href="https://unsplash.com/s/photos/universe">Unsplash</a>  

* * *

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

_Alan J Perlis_
 
* * *
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
