4 ms·
Omitting semicolons in JavaScript is like omitting braces from one line if blocks. It's all well and good until someone turns this: if (conditional) sta
by mustpax 16y ago
Omitting semicolons in JavaScript is like omitting braces from one line if blocks.
It's all well and good until someone turns this:
if (conditional)
statement;
Into this without realizing what they've just done:
if (conditional)
statement;
statement2;
Yes, you can skip semicolons. And yes it will work the vast majority of the time. But the amount of time you save will be eclipsed by that one time someone makes a change to the code that breaks in an unpredictable subtle way due to your purely aesthetic shorthand.
Writing maintainable code is about learning humility. There's no need to show off your understanding of the deeper points of JavaScript semicolon insertion in your production code base. Just insert the semicolons, I promise you you will learn to not be distracted in no time.
- dionidium 16y agoI've never understood this argument for always including braces in one-line if-blocks. Won't this error be immediately obvious? I mean, after making the change, won't even the most minimal test catch the mistake? If developer #2 isn't doing the most basic test to verify that "statement2" runs when "conditional" is true, then you've got way bigger problems than your style guide.
- mustpax 16y agoAnecdotally I've seen smart and capable developers make this mistake numerous times. It usually gets caught by code review, automation etc. But not always. It's basically the birthday paradox. You take the probability of a very unlikely event and raise it to a big enough power (enough people, enough lines of code) it becomes a non-negligible outcome.
- mathgladiator 16y agodeveloper #2 probably doesn't care because it passes the unit tests and no one is the wiser. I've seen a lot of bugs during some consulting work that was simply the result of either intellectually laziness or un-disciplined coding.
- gte910h 16y ago> Won't this error be immediately obvious? No, smart people miss it all the time.
- roryokane 16y agoWhat kind of “unpredictable subtle way” do you think leaving out semicolons can break things? The only way I know is what was mentioned in the article “Semicolons in JavaScript are optional”, where this code is incorrect: a = b + c (d + e).print() . The author of the article says you can fix this by always putting a semicolon before any line-starting parentheses, like ;(d + e).print() . Are you saying that you expect that rule to be too hard to remember for developers? If not, you should be able to give an example of another case where leaving out semicolons breaks things. Also, I agree that showing off your understanding of JavaScript parsing is a bad reason to leave out semicolons, but you imply that that is the reason everyone leaves out semicolons, and that is false. As you said, there are aesthetic reasons to do it – but having good aesthetics of code can (slightly) help one understand code more quickly, so it’s not pointless.