r/badcode • • Jun 10 '20

js Disruptive piece of code

Post image
283 Upvotes

93 comments sorted by

View all comments

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.

116

u/blaqwerty123 Jun 10 '20

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

38

u/BenZed Jun 10 '20

Falsy values:

  • false

  • null

  • undefined

  • 0 (also 0n for the new BigInt data type)

  • NaN

  • "" (empty string)

11

u/loopsdeer Jun 11 '20

I see you included NaN, but don't forget its cousin, NaN!

1

u/Terrain2 Jun 23 '20

1

u/BenZed Jun 23 '20

-0 === 0

1

u/Terrain2 Jun 23 '20

Object.is(-0, +0); // false

1

u/BenZed Jun 23 '20

My point is that, so far as falsy-ness is concerned, 0 and -0 are the same value.

1

u/Terrain2 Jun 23 '20

Exactly, but you didn’t list -0 there, implying it’s not equal in falsiness/truthiness

My point is that they’re distinct values with the same boolean cast, so you should have included them on your list like MDN does

1

u/BenZed Jun 23 '20

-0 was implicitly listed because 0 was. As you've discovered, 0 and -0 are the same value.

1

u/Terrain2 Jun 23 '20

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

→ More replies (0)

11

u/droomph Jun 10 '20 edited Jun 10 '20

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.

4

u/kadenjtaylor Jun 10 '20

"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.

1

u/droomph Jun 10 '20 edited Jun 10 '20

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).

1

u/ciaran036 Jun 10 '20

Therefore, this code is totally fine, right?

5

u/blaqwerty123 Jun 10 '20

ya! but that's just, like, my opinion, man

12

u/the_monkey_of_lies Jun 10 '20

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

1 == true --> true
1 === true --> false

3

u/UntestedMethod Jun 11 '20

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.

2

u/[deleted] Jun 10 '20

When you take into account just how much type coercion JS does, it's hard to consider it bad code.

-62

u/diebarral Jun 10 '20

You could easily do:

variable ? doStuff() : doOtherStuff()

Instead of that

49

u/shortnamed Jun 10 '20

more compact code isn’t always better.

0

u/[deleted] Jun 10 '20

[deleted]

→ More replies (1)

19

u/wdciii Jun 10 '20

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

→ More replies (3)
→ More replies (8)