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...
For further actions, you may consider blocking this person and/or reporting abuse
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.Good fot both languages
I prefer GoLang aproach.
Booleans are Booleans
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
Shouldn't that be
if (i != 0)...?Depending on whether you want true or false. I usually test for error condition and return early.
what is 'i' ?
what real world entity does it represent ? the letter 'i' ? Why would you compare a letter with a number ?
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)
For what do you check them for booleanness?
What do you mean? Numbers aren't booleans, neither are arrays. What is there to check?
So is your vote to avoid any implicit check of them as booleans?
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.
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?
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?
I really prefer how this would work in ruby:
It's closer to human thinking.
i agree.
empty is much better than strlen()=0
I'm a bit confused here. You say to not use coercion, but your 'right' example treats an array as a boolean.
dev.to/mikesamuel/boolean-coercion... touches on this topic as well.