3 ms·
What's terrible about truthy and falsy? How do you even have a language without a definition of what is truthy and what is falsy?
by mcphage 5y ago
What's terrible about truthy and falsy? How do you even have a language without a definition of what is truthy and what is falsy?
- coldtea 5y agoThere are several languages that only allow booleans in such comparisons. There you can't do e.g. a bare: if (a) for things that e.g. in C/Java etc would be "truphy" (like 0, null, etc). Instead you need to do: if (a != null) (that is, check with an expression that returns an actual boolean true or false, not anything truthy/falsy that "coerces" to true/false). In the same way, where in Python you'd do: a = "" if not a: print "this will be printed" there you'd do it like: if not (a == "") -- or if not a.isEmpty(): ... or some such.
- mcphage 5y agoSure, but then "truthy" for that language is just true, and "falsy" is just false. It's still relevant to understand the concept of "what is considered true, and what is considered false", even if the answer is simple for certain langauges.
- coldtea 5y ago>Sure, but then "truthy" for that language is just true, and "falsy" is just false. Sure, but this stretches the meaning of "truphy" and "falsy", which was invented to describe the extra coercions to true/false. A language only allowing explicit true and false just has a concept of true and false, not of truthy and falsy.
- mcphage 5y agoTruthy is "what is considered true in a conditional". It’s still a meaningful question, with a meaningful answer, even in languages where the only thing truthy is the value true.
- IshKebab 5y agoIf "what is considered true in a conditional" is literally only `true` then a language has no "truthy" values. I mean, you're free to interpret it how you want but nobody else would agree with your definition.
- mcphage 5y agoWell, the people who read or write for this site would agree: https://developer.mozilla.org/en-US/docs/Glossary/Truthy https://developer.mozilla.org/en-US/docs/Glossary/Truthy. So not nobody.
- mcphage 5y ago> this stretches the meaning of "truphy" and "falsy", which was invented to describe the extra coercions to true/false Do you have a citation for this? I’m looking for a source which doesn’t consider true to be truthy, or false to be falsy, and I’m not finding one.
- IshKebab 5y ago> What's terrible about truthy and falsy? Basically it's very error prone. It's difficult to remember the rules which are usually quite complicated which leads to mistakes. Much better just to be explicit. > How do you even have a language without a definition of what is truthy and what is falsy? You make it so that `if` only works on `bool` types, and don't implicitly convert other types to `bool`. For example if you want to check if string is non-empty, then you have to do `if (the_string != "") {` rather than `if (the_string) {`, which may cause your code to unexpectedly fail if e.g. the string is "false".