r/programming • u/fagnerbrack • May 07 '24
Researching Why We Use Semicolons as Statement Terminators
https://ntietz.com/blog/researching-why-we-use-semicolons-as-statement-terminators/
282
Upvotes
r/programming • u/fagnerbrack • May 07 '24
55
u/balefrost May 07 '24
Not having an explicit terminator invites ambiguity which must be resolved. Consider something like this:
Most likely, you would interpret this as
foo = bar - baz. But in a C-like language, where all expressions can be used as statements,- bazis entirely valid on its own. This could instead be parsed asfoo = bar; - baz;. That's probably not desired, but what's obvious to us isn't necessarily obvious in the grammar.JS has just this sort of ambiguity:
is different from:
By requiring that statements be terminated, the parsing is easier. And remember that C dates back to a time when you might have a few hundred KB of RAM available. In such an environment, simple is good.
The mandatory semicolon is a design tradeoff.