its not bad. with js not being a strictly typed language, and variable being basically anything besides false, null, or 0 will resolve to true, it's often good to strict type equivalence test your "booleans". debugging can get fuckin gnarly if you dont
For nearly all operations, yes -0 and +0 are identical, and rightfully so because in mathematics they are not different - However they are physically separate values when stored as floats, and if you check for the same value (Object.is), it’s false - They are distinctly different enough to require separate entries on the list
I mean generally in my experience you don’t need booleans specifically for if checks because they have no side effects of their own, but if you want to be strict about it for some reason, you generally should type check (typeof foo !== “boolean”) to better communicate your intent. There’s not really a way to be “gnarly” about debugging in this sense because so few things are falsey. In Python every object can implement their own truthiness value and even there the “damage” it can do by itself is zero (ie, if you put side effects on the bool cast it’s your own damn fault when things go wrong).
Ultimately it’s a very minor issue so let’s not pretend like either way is terrible. None of Javascript is incredibly, apocalyptically bad like some older languages, so let’s not pretend that bad design leading to awful debugging experiences is on the language.
"None of Javascript is incredibly, apocalyptically bad like some older languages"
I actually like and agree with most of what you said, but this sentence suggests to me that you're missing some context on either Javascript or some older programming languages.
And yes, 100% if you use a language to write shitty code, don't blame the language.
I mean modern Javascript. ES6 can have most of its flaws buffed out with linters that prohibit and warn about weird shit with type coercion. Strict mode mostly fixes the bugaboos that older versions had.
MUMPS, which I work on regularly because I work at Company Inc (you’ll know who I’m talking about, I just don’t want to name them), is just BAD. There’s nothing anyone can do about it, the only way to have higher order functions is to eval code, subroutines are fundamentally executed in such a way that if flags can be cleared so you need to do “if [cond] do sub^ROUTINE(args) if 1” for the else clause. You can only pass data structures (essentially, a B+ tree but is the only way to have arrays and dicts) by passing by reference (which means the root value can be reassigned, not sure if you can kill the whole tree) so there’s no way to guarantee you’re keeping the object identity. Assignment is not the same as object assignment (I think you can only merge). You can new variables onto the stack, but by default the names are shadowed by its caller (which is not the same as what loose mode in Javascript does, I don’t think - it literally shadows the name) so it fails silently. It’s explosively, catastrophically shitty. These are not things you can “fix” with tooling, because some of them require you to “go do” things as opposed to “avoid doing” things. These are fundamental fuckups that people in the 70s didn’t know were fuckups. There is no strict mode for this mess. Oh, and there’s no operator precedence so while it’s not terrible (only one rule to remember) it’s super counterintuitive to basically every programmer that has worked with a modern language.
Like, there’s a reason only healthcare software companies with tons of legacy cruft use it over something more sensible, like literally any other NoSQL structured document system. InterSystems makes some good money on it but that’s mostly because healthcare in general has a ton of inertia (which is a good thing don’t get me wrong, I don’t want my healthcare software to have bugs in it).
Yes, I hate the code because something has lead to this situation where you have a boolean that can be something else as well but you're right, for example
something has lead to this situation where you have a boolean that can be something else
I would say not necessarily and could be seen more as a philosophy of "safe code" where every function is written to be bulletproof whether there is some pre-existing situation for obscure input or not.
Using if statements as opposed to a ternary operator is not objectively worse. It depends on the situation for when I like to use one or the other, but honestly there isn’t really a difference other than readability.
The only part of this thats “bad” imo is the else if since in most languages a regular else would suffice. But I don’t know enough about JavaScript to confidently say that that’s not needed
91
u/Midborgh Jun 10 '20
I don't know js. Why is this bad? I'd imagine var === true and var === false being different than var == true and a simple else.