DEV Community

Adam Crockett 🌀
Adam Crockett 🌀

Posted on

Why you should use semicolons in spite of ASI

What side of the road do you drive on?

In England we say it's the right side which is of course left. This is enforced by the police, if I decided to try a different strategy, middle or the right hand side, I would either die, or be detained, but before that, I could in fact drive for miles if nobody was on the road.

I will see some commenters saying follow the lint rules of your project (the police program of your codebase), end of. But I am going to look at it from the poor parsers perspective.

foo: {

}

// Vs

{
    foo: {
    }
}
Enter fullscreen mode Exit fullscreen mode

The grammer highlighter above is having a hard time with this. JavaScripts grammer is ambiguous as it is.

I could argue that it's polite to help the parser out with semicolons but I don't think that's enough, what I will say is when you don't uses semicolons you are relying on ASI automatic semicolon insertion to fill in the semicolons because you didn't bother, I get that, why write optional code? You got me yeah, write less sure, but what about context switching, you want to learn another language, I don't know, say Java, C++, C, Rust, PHP among others, these languages do not have ASI and you will need to add semis. So why not just get into the habit now?

I used to be the opposite, I have for a long time omitted semicolons, and in that time I learned the cases that I would need to use them. But why remember this at all, it's far simpler to train your muscle memory to just add semicolons.

Did you know that CSS has optional semi colons? But you didn't know that (probably) because it's under a very special circumstance, it's easier to just add them everywhere to make this parae and get on with your life, I have never seen anyone actually do it irl, not the case with JavaScript.

The last case, I have worked in 2 large companies, both use semi's as lint rules that say use them, so regardless of your preference, you are going to be using them. I feel there is a stronger case to enforce them to prevent errors during authoring and if a company told me otherwise I would fight hard to explain the cognitive load of remembering when and where to add semicolons in order for the parser to not mistake a () as a function call.

Tell me what your thoughts are down in the comments.

Top comments (11)

Collapse
 
chasm profile image
Charles F. Munat

No.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

How dare you 😂. I can't even remember writing this must have been a long time ago. Good that you know your mind

Collapse
 
daniel13rady profile image
Daniel Brady • Edited

💯 agree. When writing, we should always be optimizing for clear communication, not less words. Writing code is hard in large part because we're optimizing for clear communication to both humans and machines.

If you care about communicating in a particular way, then write in a language that embraces that style of communication. Using the handle of a screwdriver to bang in a nail when you've got a hammer within reach will work, but might make people go 🤨🤔when they see what you're doing.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I think that, on the safe side, always add semicolon; but the only real time I need semicolon, is when the line starts with a bracket

  • IIFE for scope declaration
  • Type casting in TypeScript, which requires brackets to be able to add .

In the end, it is more about being consistent, but it is easier to be consistent on the safe side.

Collapse
 
xowap profile image
Rémy 🤖

I totally agree. Not deciding if I should put semicolons is one less problem in my life, if there is cases where I need them then I always need them.

Collapse
 
osde8info profile image
Clive Da

OMG ; are optional in CSS ! must learn those rules ASAP (as an ex PHPer i hate ;)

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Here is how that works

.not-in-cansis {
    background: red;
    color: green
}

But seriously don't.
Collapse
 
osde8info profile image
Clive Da

too late i LOVE it i will write some prettier rules straight away

Collapse
 
lamka02sk profile image
lamka02sk

Only after the last property and it is not a good idea to leave them out. Your colleagues are going to eat you alive.

Collapse
 
dbshanks profile image
Derek Shanks

I always pop my semi-colons in. I do run back and forth between PHP and JS it just makes sense. I let prettier handle the moments when I am dolt that forgets to put one in.

This is also largely why I struggle with languages like Ruby and Elixr, I love my braces and brackets to go along with semi-colons too. Makes me feel snug as a bug.

Collapse
 
evanplaice profile image
Evan Plaice

Understanding how a language parses code is difficult enough without ambiguity like ASI

I always use SemiStandard to lint my OSS projects

github.com/standard/semistandard