# Code Smell 24 - Boolean Coercions

*Booleans should be just True and False*

> TL;DR: Don't do magic castings to boolean. You will regret it on a Friday night.
 
# Problems

- Hiding Errors

- Accidental complexity coupled with one particular language.

- Readability

- Difficulty hopping among languages.

- [IFs](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever)

# Solutions

1. Be explicit.

2. Work with booleans for boolean conditions. Not integers, not [nulls](https://maximilianocontieri.com/null-the-billion-dollar-mistake), not strings, not lists. Just booleans.

3. [Fail fast](https://maximilianocontieri.com/fail-fast)

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/1f5d0d7328e2e49f0695323e6c210c3e)
```python
virus = ['MERS', 'SARS']
vaccines = []
 
if vaccines:
	print ("let's get vaccinated")
else:
	print ("We have no vaccines yet. Keep researching")
    
if virus:
	print ("There are some virus around. Take extra care")
else:
	print ("We are free to get out. Not masks are necessary")

#equivalent     
    
if not vaccines:
	print ("We have no vaccines yet. Keep researching")
else:
	print ("let's get vaccinated")
    
if not virus:
	print ("We are free to get out. Not masks are necessary")
else:
	print ("There are some virus around. Take extra care")
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/0c8dd91896ff91852dfa0e8711093a06)
```python
if len(vaccines) == 0:
	print ("We have no vaccines yet. Keep researching")
else:
	print ("Let's get vaccinated")
    
                    
if len(virus) == 0:
	print ("We are free to get out. Not masks are necessary")
else:
	print ("There are some virus around. Take extra care")
```

# Detection

This is a language feature. Some strict languages show warnings with this magic wizardry.

# Tags

- Coercions

- Primitive

# Conclusion

Some languages encourage doing some magic abbreviations and automatic castings. This is a source of errors and a *Premature Optimization* warning.

We should always be as **explicit** as possible.

# Relations

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

%[https://maximilianocontieri.com/code-smell-69-big-bang-javascript-ridiculous-castings]

# More Info

%[https://maximilianocontieri.com/fail-fast]

* * *

> It is not the language that makes programs appear simple. It is the programmer that make the language appear simple!

_Robert Martin_

%[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]

*Last update: 2021/07/07*
