All the Crappy Javascript


Javascript. One of the idioms I love in Javascript land is being able to do this:

https://gist.github.com/withinboredom/fb779c8b10cc89c9e69d6f3f4537bd90

And now something is the result of DefTrue() … A lot of libraries (and people also tend to write functions like this too:

https://gist.github.com/withinboredom/944d19cbfb8bddea1610e9b3173df387

The fact that these people (and I won’t call them out) check to make sure the things exist before they actually return the function is a Good Thing™. I just wish they checked it properly, because falsey is not exactly the same as existing.

For example, I could totally screw this up by accident:

https://gist.github.com/withinboredom/c495853eac8fab360095fc7ed96689dc

How long would it take you to find this bug? Because the error wouldn’t appear anywhere near this file. In fact, it might only intermittently appear many, many, many callbacks later after being passed around and around. I’ve had to chase down three of these exact bugs in the last couple of months … buried deep, deep in library code.

  1. I had a bug where an api response was a literal empty string (falsey) which killed the app.
  2. I had two bugs where the function was changed to return true and it was originally returning a callback. Like above, it magically kept working until that “callback” was called, minutes later after being set.

This is why Javascript is crappy.

It’s because we’re human and Javascript doesn’t care. Our customers do. So do our paychecks.

You can type check in Javascript. Do it when it’s feasible. !X is not an existence check, things can exist in a falsey state. undefined means it literally doesn’t exist. So check that.

For the love of God, the universe, Alan Turing, and Linus Torvalds … falsey IS A VALUE and IT DOES EXIST.

</rant>


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.