DEV Community

To Semicolon, Or Not To Semicolon;

EmNudge on August 15, 2019

"To Semicolon, Or Not To Semicolon" - Shakespeare or something The debate springs up and dies down every once in a while. I remember my first time...
Collapse
 
emnudge profile image
EmNudge

No? I could have sworn that's how Hamlet went.
To be fair, this is an overdone title. Second time I've used that format already. Had an article on my personal blog with a similar pattern (but a bit more clever): To Be or To Ben't

Collapse
 
axelledrouge profile image
AxelleDRouge

Interesting. I knew that I could not use semicolons, but I learn more about ASI. But I think of them more like the point at the end of a sentence.Like you said, it's more a stylistic preference. It's a good way to organize my code, and organize my thoughts.

Collapse
 
stereobooster profile image
stereobooster

I use prettier and don't worry about formatting.

Collapse
 
emnudge profile image
EmNudge

I use prettier too, but only for personal projects. It gets messy when working with teams where some people don't use it and abide by their own style guides.
Using prettier in those cases can lead to messy looking commit logs, making it hard to decipher what was actually changed.

Collapse
 
stereobooster profile image
stereobooster • Edited

Use pre commit hook for prettier. Code standard should be a team decision. My team uses prettier. We have two projects one with semicolons one without, but both use prettier. I can't care less about semicolons, I type any code I want and it gets formatted on save in VSCode

Thread Thread
 
aurelio profile image
Aurelio

Code standard should be a team decision.

Exactly, and if the decision is that Prettier is part of the tooling, then everyone touching the code needs to have it. In my team we make sure that every dev's IDE is configured to format on save.

If you have a pre-existing project and you don't want to get distracted by formatting changes on the diffs you can run prettier and then commit a mega-fix. From there on, no more distractions for formatting changes.

I'll be honest, I thought that Prettier was a nice-to-have for a long time, but now I can't imagine working on a project where it's not part of the tools.

Collapse
 
charlesj3 profile image
Charles Jones

I usually treat semi-colons in JS like space/tab indentation; I have my preferences, but if the team wants to do it one way, I'll adjust without trying to change the entire team's preference to my own.

Collapse
 
intricatecloud profile image
Danny Perez

I enforce semicolons via eslint. If I don't, then I find myself writing some lines with and some lines without which has made it more distracting for coworkers (Are you using it or not?!)

I did not know about ASI so this was very helpful. On larger teams, the rule has been - doesn't matter as long as we're consistent.

Collapse
 
emnudge profile image
EmNudge

Consistency among any styling choice is always going to be the most important decision!
Styling choices are irrelevant unless they're consistent.

I find myself preferring automatic formatters like Prettier over linters to avoid all the headache. When using auto-formatters, one should always make sure everyone else is using the same thing or else git commit logs will be obscured by a ton of semicolon insertions and whitespace, making it hard to read exactly what changed.

Collapse
 
ahkohd profile image
Victor Aremu

Nice article. I use semicolons always even though its optional. Some linters won't even let you code in peace without the use or semicolons.

Collapse
 
kachkaev profile image
Alexander Kachkaev • Edited

Turns out you can dim or hide javascript/typescript semicolons in VScode. I don’t recommend this in general, but this might be a solution for teams with mixed opinions about relying on ASI (automatic semicolon insertion).

medium.com/@kachkaev/dimming-or-hi...

Collapse
 
jzombie profile image
jzombie

In my opinion, not using semicolons is like not closing HTML tags. Just because you can do it doesn't mean you should.

Collapse
 
jzombie profile image
jzombie

I no longer hold this opinion. Whatever the linter is configured to, I'll run w/ it. So long as there is consistency throughout the entire project.

Collapse
 
awwsmm profile image
Andrew (he/him)

Wow, JavaScript sounds terrible.