DEV Community

Discussion on: Better lessons from [No] semicolons in Javascript

Collapse
 
jdforsythe profile image
Jeremy Forsythe

I like the point of the article, which boils down to a way to challenge your presuppositions.

I disagree with your stance on semicolons and implicit returns, though.

Setting aside personal preference, there are 2 reasons to use semicolons. First, the parser never has to run the checks for ASI. Second, your code always does what you think it does. If you don't like typing them, just set up your linter to insert them, and you remove the chance of any bugs from ASI rules. So, to me, it's a simple comparison. Not using them adds a chance of bugs. Using them doesn't add any chance of bugs. That's a clear winner in my book.

The same goes with implicit returns. The benefit of not having to type the word "return" is very minor compared to the chance of a bug. The number of keystrokes you're saving is minuscule compared to the chance of bugs from returning the wrong thing. Additionally, it makes your code harder to read. Seeing "return" makes it very obvious what is happening. Implicit returns take up more discrete quanta in your mind, thus making it more difficult to keep the code in your mind. Again, the small benefit compared to the chance for (potentially really bad) bugs and making code hard to read makes this a no brainers for me.

I'm all about saving keystrokes, which is why we default to single quotes for strings (no shift key), but not at the expense of readability or possibly introducing a new class of bugs that can't exist otherwise.