r/ProgrammerHumor 17h ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.4k Upvotes

262 comments sorted by

View all comments

146

u/ReefNixon 16h ago

We invented boolean types so that we could stop treating 0 as false.

1

u/UristMcMagma 13h ago

And so that we could stop treating 1 as true. The only real argument for type coersion is convenience. I don't see how it's more convenient to treat 0 as true rather than false.

5

u/ReefNixon 13h ago

If we take coercion as a given, what if your conditional is the index of an element in an array? If it doesn’t find it, it returns nil (which is falsey). If it’s the first element, it returns 0. Shouldn’t that be truthy?

0

u/UristMcMagma 12h ago

Yes. I'm not really a Ruby guy but after reading up I can see how it's more convenient to treat 0 as truthy in that lang. in js you never use findIndex since find is just better.

2

u/ReefNixon 11h ago

There are language agnostic examples too, like when tracking things that don’t stop at 0 e.g. temperatures.

It would be weird if temperatures under 0 degrees and temperatures over 0 degrees coerced into truthy, but exactly 0 degrees was falsey.

Coercion will always have some level of ambiguity, Ruby just chooses to only treat definitely falsey values as falsey (literal false and nil).

2

u/UristMcMagma 11h ago

Temperatures do stop at 0 if you're using the correct unit. ;)

But yeah I agree with you. There's always some level of ambiguity caused by coersion. Even treating nil and false the same for a boolean is technically wrong, but we can accept the ambiguity for the sake of convenience.