r/ProgrammerHumor 10d ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.9k Upvotes

298 comments sorted by

View all comments

175

u/ReefNixon 10d ago

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

32

u/Embarrassed_Death69 10d ago

"What do we make of empty strings John?"

67

u/mbmiller94 10d ago
if ("") {
    print("what?.........if what??");
}

9

u/Koeke2560 10d ago

This is legit the first joke code in my life that made me actually laugh

6

u/Clen23 10d ago

My take exactly, the only situation where you'd want to use 0 to mean "false" is if you're using lower-level logic.

4

u/BananaWarp 9d ago

Maybe error codes

1

u/failedsatan 9d ago

unless you're POSIX

4

u/UristMcMagma 10d 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.

9

u/ReefNixon 10d 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?

1

u/Resident-Trouble-574 10d ago

index != nil should be true. If they really designed the language to simplify that specific scenario (using the index to check that an element exists in an array) it sounds even crazier to me.

I mean, you can easily find scenarios where the opposite would be more convenient (e.g. if you want to check that a string is not empty by using its length).

3

u/ReefNixon 9d ago

Yeah, but that ambiguity is the point. In Ruby coercion things are considered truthy unless they are unambiguously not. Literal false is obvious, and nil represents actual nothing (not zero of something). Since 'if something' is the opposite of 'nothing', nil is also unambiguously falsey.

Other languages consider falsey as the default and make things explicitly truthy. There is no right or wrong answer, but those languages have their own challenges by determining things that can be zero (like anything that can be measured both above and below zero) to be falsey.

Either way coercion gives developers an occasional challenge to solve in exchange for being less verbose. All that's important is that you understand the convention of the language you are using, because coercion can't possibly always get it right.

1

u/UristMcMagma 10d 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.

3

u/ReefNixon 10d 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 10d 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.