r/ProgrammerHumor 23h ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.6k Upvotes

277 comments sorted by

View all comments

452

u/deceze 23h ago

It's been a while since I've touched Ruby, but I think the stance was that only false and nil should be falsey? Which is not an unreasonable stance to take. The fewer falsey values, the easier it is to predict the behaviour. It's just surprising when coming from other languages.

Just off the top of my head, PHP treats "0", that is, specifically a string with a single zero, as falsey. Which has clearly gone too far to the other extreme.

7

u/PM_ME_BAD_ALGORITHMS 22h ago

I never used php, is that behaviour due to it implicitly converting "0" to 0 and as such it is falsey? Or is it straight up "0" the one that is falsey?

15

u/deceze 22h ago

It's straight up "0" as a special case.

Because PHP started as a sprinkle-it-on-your-HTML language, and any form submissions are always text, and 0-from-a-form should still evaluate like 0; or some such hare-brained nonsense reasoning.

The fact that such basic nonsense is still baked into the very core of the language, and can never be removed due to backwards compatibility, means I'll never respect PHP, regardless of how much it's improved over the last decade or so.

5

u/maweki 22h ago

I think it's not actually that special of a case, as strings that are exactly also numbers are auto-coerced basically whenever. It's just that 0 is the only number, as it should be, that then is falsy.

Numberly strings are a huge footgun in PHP.

6

u/deceze 20h ago

Strings are not coerced to numbers to then be coerced to booleans, that'd be entirely insane! There's a direct conversation path from strings to booleans!

They're only sometimes coerced to numbers to then be reduced to a boolean, if you're using the loose comparison operator, and depending on what the other operand is… https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types. 🥲