skull

Robert Tamayo

R
B
blog
skull

Modern JavaScript is Full of Glitch Hacks

Modern JavaScript is full of glitch hacks that aren't tolerated in other languages. 

A glitch hack is a video game term describing a glitch (bug) that has been exploited by the player to give them an advantage. One of the most famous glitch hacks is Wave Dashing from Super Smash Bros. Wave Dashing was so popular that Nintendo decided to intentionally include it in one of the game's sequels.

A JavaScript glitch hack looks like this:

true && doSomething();

The glitch hack works because JavaScript will return whatever the second expression evaluates as in a && statement if the first parameter is true. It's a "hack" because that's simply not the behavior you would expect. We were taught that the && operand required 2 boolean values on either side, but this glitch hack is abusing it to do something! Code is written like this to be concise, but the reality is that it is much more readable and human-understandable to use a traditional if-else statement.

This pattern is used a lot in React and other modern JavaScript projects. This "pattern" exists in other interpreted, typeless languages, such as PHP. In fact, the code above was copied and pasted from a PHP script I wrote as a test. The problem is that no one accepts code like this in PHP, yet in JavaScript this is seen as "forward" and "progressive".

The glitch hack above doesn't serve to make the code "better" or more "readable". It only serves the programmer's ego, by making them feel good that they "hacked" JavaScript.

A lot of developers use the above glitch hack as a convention, when in reality it should be bugfixed out of existence. If you think it shouldn't be bugfixed, ask yourself why. Is it because it is an important step in the evolution of programming? Or is it because it makes you feel cool when you write it?

Remember, the glitch hack above is standard practice in JavaScript but not PHP, and yet was written in PHP. It is acceptable in one place but not another. I suspect the reason is that JavaScript developers are insecure and need to use glitch hacks to feel awesome when coding. If you're using a glitch hack because it's a company standard, then whatever. You should never use a glitch hack to feel good about your code or your project. Your project should already be awesome enough and exciting enough that the language and style you use to write it doesn't matter.


Comments:
Leave a Comment
Submit