News for hackers. Stuff that matters.
Re:A Modern JavaScript Tutorial by msie on Monday December 07, 2020 @03:37PM (#25337123)
Remember when some people advocated not using semicolons to end statements? Good times...

https://javascript.info/structure

Re:A Modern JavaScript Tutorial by stingraycharles on Monday December 07, 2020 @04:04PM (#25337501)
https://news.ycombinator.com/item?id=2024328

Here’s an example great bike shed discussion from ~10y ago about whether or not to use semicolons with JavaScript.

“If you don’t understand how statements in JavaScript are terminated, then you just don’t know JavaScript very well, and shouldn’t write JavaScript programs professionally without supervision, and you definitely should not tell anyone else how to write their JavaScript programs.”

Those were the fun days! :)

Re:A Modern JavaScript Tutorial by wwweston on Tuesday December 08, 2020 @03:01AM (#25343017)
Something I always thought was interesting about that argument is how a similar line of reasoning could apply to the `==` and `===` comparison operators.

Just about anyone who's ever written JS probably knows about the heavy lean towards "always use `===`". And it's not without good reason, because there are more than handful of not-entirely-intuitive implicit coercions/comparison corners associated with `==`. Personally, I think it's OK to use it, but I understand that it requires knowing the rules associated with use and most of all comes with the overhead of keeping them in mind when you do so. Most of the time it will be OK if you know even most of the rules. Sometimes it will not be OK. More often the overhead will cost you attention to other things. Some people have made the call "wouldn't it be simpler if we just did strict comparison and saved our attention for other things?" and while it's not my first choice, I can understand and respect it.

The statement termination thing strikes me as pretty much the same. Sure, if you know the rules, you can do it freely. But it takes something explicit that you don't have to think about and turns it into something implicit that you do. It isn't going to be a problem most of the time, but sometimes it will not be OK. A little more often the overhead will cost you attention to other things. Following the rule that semicolons terminate statements hasn't ever cost me anything.

The weird thing in my experience was the frequent overlap between antisemicolonists and always===ists. My theory is that mostly these people came from Ruby and Python, liked less punctuation for subjective aesthetic reasons, and didn't actually know the coercion rules well or particularly like JS, but that's speculation.

Re:A Modern JavaScript Tutorial by randomsearch on Tuesday December 08, 2020 @04:49AM (#25343531)
“Explicit is better than implicit.”
Re:A Modern JavaScript Tutorial by dagw on Tuesday December 08, 2020 @06:25AM (#25343968)
Reminds me of a CS teacher for our intro Haskell course at university who would dock points for unnecessary use of parentheses, since he considered that it showed we didn't understand order of operations or how our program was parsed.
Re:A Modern JavaScript Tutorial by heja2009 on Tuesday December 08, 2020 @07:40AM (#25344386)
The programming language Occam, in its time the primary language to program Transputers, did not have operator precedence, so you had to use parentheses for anything. It was a huge pain, but probably still better than avoiding the edge cases of what you describe...

https://en.wikipedia.org/wiki/Occam_(programming_language)

Re:A Modern JavaScript Tutorial by Timpy on Tuesday December 08, 2020 @06:57AM (#25344142)
That sounds like a bad professor. It doesn't cost anything to add parenthesis for clarity. I enjoy well written code but we're not writing literature here, it's okay to be explicit.
Re:A Modern JavaScript Tutorial by dagw on Tuesday December 08, 2020 @07:19AM (#25344251)
Yea it was very weird. I think most people realized that his way wasn't the right way.
Re:A Modern JavaScript Tutorial by gtsop on Monday December 07, 2020 @06:31PM (#25339238)
I used to be on the "always use semicolons" side of this argument. Having shipped multiple projects using both methods my verdict is clear: It doesn't matter. Why it doesn't matter you ask? Because 1) I am enforcing either decision with eslint rules and 2) i write unit tests 3) code is transpiled (thus semicolons are handled perfectly at the output). Having these three in place, I have yet to encounter a single "gotcha" out of omitting semicolons.

If for any reason you absolutelly can't have these in place, better be safe and use semicolons.

Edit: for the "gotcha" explained in the link, prettier will automatically insert a semicolon before the array.

Re:A Modern JavaScript Tutorial by ux-app on Tuesday December 08, 2020 @01:07AM (#25342406)
>I have yet to encounter a single "gotcha" out of omitting semicolons.

keyword "yet". It seems like such a insignificant win, with the downside being some very hard to debug runtime error because one element of your build chain has a bug or misses an edge case.

I honestly don't see the logic.

Re:A Modern JavaScript Tutorial by gtsop on Tuesday December 08, 2020 @04:09PM (#25351384)
I explained the logic: By having tooling and tests it place, I am protected against gotchas and I haven't ever encountered a problem. Thus, i do not advocate you must ommit semicolons. I am saying "it doesn't matter what you do" provided you have the tooling in place. Do whatever feels best, what you like, what you feel comfortable with.

And here is the upside: I've minimized the time I spend arguing about this detail with my colleagues to 0s. Because it doesn't matter :)

Re:A Modern JavaScript Tutorial by oweiler on Tuesday December 08, 2020 @01:56AM (#25342650)
It just makes the code slightly easier to read. The semicolon is just noise.
Re:A Modern JavaScript Tutorial by burlesonK on Tuesday December 08, 2020 @02:40AM (#25342893)
Semicolons aren't noise; you've read the statement before you even get to them so how can they be noise? Noise would be type info added to languages that didn't have it before, such as TypeScript. Generics can get pretty gnarley too, when you have nested typing info involved.
Re:A Modern JavaScript Tutorial by goatlover on Tuesday December 08, 2020 @09:18AM (#25345395)
Noise as in new line already terminates. That’s one reason Python and Ruby code are cleaner to read.
Re:A Modern JavaScript Tutorial by dotancohen on Tuesday December 08, 2020 @09:02AM (#25345160)
I disagree that the semicolon is noise Thought the Javascript interpretter (usually) does not need it the semicolon is good for humans who read the code after you Sure it is possible to read the code without the semicolons with some practice but the semicolons are standard in many other related languages among them Java C++ PHP and MySQL Now you personally might not use those other languages very often but many of us especially those who occasionally wear a devops hat do And if the code is not meant to be read by humans also then why are we not writing in assembly
Re:A Modern JavaScript Tutorial by madeofpalk on Tuesday December 08, 2020 @06:38AM (#25344045)
"It doesn't matter" really is the perfect summation of this.

We have tooling that can solve this to prevent pointless bike shedding.

Re:A Modern JavaScript Tutorial by JoBrad on Tuesday December 08, 2020 @08:17AM (#25344639)
Exactly. I almost always use TS instead of JS, for example, which flags gotcha scenarios (which I usually rewrite to avoid) and inserts semicolons for me in the final code. It’s just noise in my source.
Re:A Modern JavaScript Tutorial by weeksie on Monday December 07, 2020 @04:19PM (#25337698)
I've run into that in a few places and it always baffles me that people would intentionally choose to add even more gotchas to javascript. Don't get me wrong, I think aesthetics in code are important but not to the point of deliberately increasing the chance of hard to find bugs.
Re:A Modern JavaScript Tutorial by pknopf on Monday December 07, 2020 @03:56PM (#25337392)
Yeah...

There was a big push for that a while back. The JavaScript community can be silly.

Re:A Modern JavaScript Tutorial by sneak on Monday December 07, 2020 @10:02PM (#25341185)
I don't know whether I use them or not, to be honest (and I just finished a project in js). My code looks like whatever the almost-default (tab-width: 4) output of prettier is.

It's nice to not waste a single braincycle on these things anymore.

Re:A Modern JavaScript Tutorial by Jowsey on Tuesday December 08, 2020 @06:17AM (#25343926)
This. I just use an auto-formatter for the Standard style. If the Standard linter says it looks ok, it looks ok to me, and I move on with my day.
Re:A Modern JavaScript Tutorial by flippinburgers on Tuesday December 08, 2020 @10:25PM (#25354980)
Debate over one character whose presence in code has no downside other than that a subset of programmers get to feel less arrogant. Sometimes I cannot stand this industry.
Re:A Modern JavaScript Tutorial by austincheney on Monday December 07, 2020 @11:23PM (#25341764)
So what changed this? This was a hill that people were willing to die on.
Re:A Modern JavaScript Tutorial by simplify on Monday December 07, 2020 @09:41PM (#25341011)
I still don't use them :)