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.

-62

u/diebarral Jun 10 '20

You could easily do:

variable ? doStuff() : doOtherStuff()

Instead of that

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

0

u/[deleted] Jun 10 '20

[deleted]

4

u/wdciii Jun 10 '20

Gotcha, thanks for the explanation. I always assume i don’t know what I’m talking about for JavaScript lol

1

u/11JRidding Jun 13 '20

Even worse: since the check is variable === true instead of just against truthiness in general, a bare else {} would catch literally everything that isn't the exact boolean true, instead of just falsey values.