DEV Community

Cover image for Code Smell 24 - Boolean Coercions

Code Smell 24 - Boolean Coercions

Maxi Contieri on November 14, 2020

Booleans should be just True and false TL;DR: Don't do magic castings to boolean. You will regret on a friday night. Problems Hidin...
Collapse
 
yoursunny profile image
Junxiao Shi

Go doesn't allow conversions from and to boolean type altogether.

Meanwhile in C, it's normal to test an integer as boolean, but I'd rather write if (i == 0) although I know the compile will optimize the comparison away.

Collapse
 
mcsee profile image
Maxi Contieri

Good fot both languages

I prefer GoLang aproach.

Booleans are Booleans

Collapse
 
mcsee profile image
Maxi Contieri

Exactly ! Let the compiler do it for you if it is safe.
Go is much stricter than C. I think Go lacks several important features like Exceptions or Full Closures. Once it catches up with older languages it would be an excellent option

Collapse
 
eljayadobe profile image
Eljay-Adobe

Shouldn't that be if (i != 0) ...?

Collapse
 
yoursunny profile image
Junxiao Shi

Depending on whether you want true or false. I usually test for error condition and return early.

Collapse
 
mcsee profile image
Maxi Contieri

what is 'i' ?

what real world entity does it represent ? the letter 'i' ? Why would you compare a letter with a number ?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I hate languages that treat "empty" values as falsey. It's dumb and leads to unexpected errors. An empty array is still an array, an empty string is still a string, zero is still a number. Neither of them has anything "false" about them and languages that treat them as such are just poorly designed (low-level languages like C being the exception, for obvious reasons)

Collapse
 
netch80 profile image
Valentin Nechayev

I hate languages that treat "empty" values as falsey. It's dumb and leads to unexpected errors.

For what do you check them for booleanness?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

What do you mean? Numbers aren't booleans, neither are arrays. What is there to check?

Thread Thread
 
netch80 profile image
Valentin Nechayev

So is your vote to avoid any implicit check of them as booleans?

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Depends on the language and how it handles typing. In a language like JavaScript, any value that is not explicitly falsey or void should be considered truthy, kind of how it is in Lua. Empty data structures are empty, but that doesn't mean they don't exist.

Thread Thread
 
netch80 profile image
Valentin Nechayev

So you still provide some border between values which are implicitly treated as false, and those as true, but your border is so only "void" and "false" are false. What about "undef", "null", etc., whatever they are named in different languages?

Empty data structures are empty, but that doesn't mean they don't exist.

But they are empty. Just no data. If this is, for example, a list, you have no values in it => nothing to process.
Why don't you consider boolean checking here as a handy manner to check its state?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I really prefer how this would work in ruby:

if vaccines.empty?
   puts "We have no vaccines yet. Keep researching"
else
   puts "Let's get vaccinated"
Enter fullscreen mode Exit fullscreen mode

It's closer to human thinking.

Collapse
 
mcsee profile image
Maxi Contieri

i agree.

empty is much better than strlen()=0

Collapse
 
alainvanhout profile image
Alain Van Hout

I'm a bit confused here. You say to not use coercion, but your 'right' example treats an array as a boolean.

Collapse
 
mikesamuel profile image
Mike Samuel

dev.to/mikesamuel/boolean-coercion... touches on this topic as well.