4 ms·
..and why shouldn't: if (someStr.indexOf("John") !== -1) { ... } vs if (someStr.indexOf("John")) { ... } Or when _teaching_ it's kind of funny saying "So, w
by stared 11y ago
..and why shouldn't:
if (someStr.indexOf("John") !== -1) { ... }
vs
if (someStr.indexOf("John")) { ... }
Or when _teaching_ it's kind of funny saying "So, we take the first element, that is the zeroth, then the third (I mean the real third, not the one with index three)...".
OK, but to be entirely honest, I both sides have pros and cons. And as long as a language/program/person is reasonably consistent (vide https://xkcd.com/163/ https://xkcd.com/163/), there is not a big difference.
- icebraining 11y agoYour example should really be (using a polyfill if necessary) if(someStr.includes('John')) {...} While I don't mind it for containers, I've never liked using Boolean coercion for numbers. Zero is obviously a special case, but I don't think it maps well with the True/False dichotomy.
- jewel 11y agoZero doesn't have to be a special case. In ruby 0 is a truthy value, as well as the empty string. The only things that are falsey are false and nil. Truthy/falsey means when used in a conditional or after a unary !. So you still get strong typing, e.g. false == nil is not a true statement.
- ufo 11y agoOption #2 assumes that 0 is falsy. Its an OK assumption for a language like C but for languages that let lots of different things go into an if condition I prefer for zero to be truthy and just false / null / etc to be falsy.
- JoshTriplett 11y agoBanning implicit integer conversions, such that neither 0 nor 1 can be used in a conditional, isn't unreasonable; then you have to write a boolean operator or conversion explicitly. However, if you allow integers in conditionals, yet have 0 mean true, you will break the default expectations of far too many programmers.
- jfarmer 11y agoThe first several languages I learned treated 0 as falsey and I can't really recall it tripping me up when I learned a language (Ruby) where that's not the case. I suppose always thought of 0-as-false as an unfortunate consequence of C not having a native boolean type, anyhow.
- ufo 11y agoIn a statically typed language, restricting if conditionals to just booleans is the approach I like the most: simple and safe. On the other hand, in dynamic languages, where you can pass anything to conditional I think that treating 0 as falsy is very error prone. For example, its very common to use conditionals to implement default values: function foo(name){ name = name || "default name"; } In this setting, having 0 and "" act as falsy is a bug waiting to happen. On the other hand, having 0 act as truthy might confuse you a bit the first time you come across it but its an easy fix (just write x != 0 instead) and its not very likely to hit you when you don't expect it.
- JoshTriplett 11y agoIf your language has something like "None" or "null", then you should be checking against that to implement a default, rather than checking a truth value. After all, what if you wanted to implement a boolean parameter with a default value? Personally, I'm a big fan of languages with a "Maybe" type for this kind of thing.
- deleted 11y ago[deleted]
- ufo 11y agoThe `||` pattern is super common in a bunch of languages though and to code it with a `== null` test you would need to expand it to a full blown ternary operator name != null ? name : "default name" Some languages have a "null-coalescing" operator you can use in these cases but its a niche thing. (And I agree with you on the Maybe thing but, as I said previously, 0 in conditionals is not a problem when conditionals have to be boolean)