New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why no semicolons? #99

Closed
brennanMKE opened this Issue May 11, 2015 · 13 comments

Comments

Projects
None yet
4 participants
@brennanMKE

brennanMKE commented May 11, 2015

I am noticing that semicolons are omitted. I know they are optional yet I thought it was still best to include them to avoid potential problems. Still I see with Node.js and Express.js that semicolons are often omitted. Is there an explanation for it? Is there a coding standard for Node.js which explains how semicolons should be used.

Below is an archived discussion on Stackoverflow.

http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript

@dougwilson dougwilson added the question label May 11, 2015

@dougwilson dougwilson self-assigned this May 11, 2015

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson May 11, 2015

Member

Because it's personal preference. It's the same as "why commas last instead of first", "why use curly braces when unnecessary", "why 2 space indent instead of tabs", etc.

You can always use the "One Style to Rule Them All", which bans semicolons as well: https://github.com/feross/standard

Member

dougwilson commented May 11, 2015

Because it's personal preference. It's the same as "why commas last instead of first", "why use curly braces when unnecessary", "why 2 space indent instead of tabs", etc.

You can always use the "One Style to Rule Them All", which bans semicolons as well: https://github.com/feross/standard

@dougwilson dougwilson closed this May 11, 2015

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson May 11, 2015

Member

But just to highlight, after re-reading all your questions: the inclusion or omission of them is purely personal preference and style. As far as contributing to any project I oversee, PRs never have to match the original style, as style issues are superficial and I'll fix up for you on merge.

Member

dougwilson commented May 11, 2015

But just to highlight, after re-reading all your questions: the inclusion or omission of them is purely personal preference and style. As far as contributing to any project I oversee, PRs never have to match the original style, as style issues are superficial and I'll fix up for you on merge.

@Fishrock123

This comment has been minimized.

Show comment
Hide comment
@brennanMKE

This comment has been minimized.

Show comment
Hide comment
@brennanMKE

brennanMKE May 11, 2015

@dougwilson @Fishrock123

Thanks for the additional details.

I try to use linters (JSHint) or editors which show warnings as I work on the code which is why I tend to place the semicolons. I also like it when the editor automatically formats the code in a standard way so my commits end up being more consistent. I was using Atom and now I am using Visual Studio Code (based on Atom) which seems to have good support for formatting code and syntax warnings without having to configure JSHint or other settings. And the defaults all seem reasonable so far.

Since Express.js has been around a while and used by so many people I figured this would be a good place to ask. Thanks for the helpful links.

brennanMKE commented May 11, 2015

@dougwilson @Fishrock123

Thanks for the additional details.

I try to use linters (JSHint) or editors which show warnings as I work on the code which is why I tend to place the semicolons. I also like it when the editor automatically formats the code in a standard way so my commits end up being more consistent. I was using Atom and now I am using Visual Studio Code (based on Atom) which seems to have good support for formatting code and syntax warnings without having to configure JSHint or other settings. And the defaults all seem reasonable so far.

Since Express.js has been around a while and used by so many people I figured this would be a good place to ask. Thanks for the helpful links.

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson May 11, 2015

Member

It's no problem :) FWIW, I force everyone to always include semicolons when at work.

Member

dougwilson commented May 11, 2015

It's no problem :) FWIW, I force everyone to always include semicolons when at work.

@brennanMKE

This comment has been minimized.

Show comment
Hide comment
@brennanMKE

brennanMKE commented May 11, 2015

👍

@rlidwka

This comment has been minimized.

Show comment
Hide comment
@rlidwka

rlidwka May 15, 2015

Member

I thought it was still best to include them to avoid potential problems.

That's a common misconception, here is a response to it: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

Is there a coding standard for Node.js which explains how semicolons should be used.

Oh yeah, there are a lot of coding standards. And that's the problem. :)

I think I'll just explain why I moved away from using semicolons two years ago. It's easier to see potential errors this way.

I mean, if you use semicolons, one semicolon missing could go unnoticed. Because your eye is trained to treat them as end of line noise. Here is an example of an error that you could have:

function test() {
  var foo = '123';
  var bar = '456';
  var example = a_long_line_without_ending_semicolon
                                               //   ^^^ this would usually go unnoticed

  (function () {
    console.log(example); 
  })(example);
}

But if you don't use them, the semicolon at the end of the line never matters. You can't make the same error. You can of course forget to insert semicolon at the start of the line, but this error would be obvious for anybody who didn't use semicolons for a while:

function test() {
  var foo = '123'
  var bar = '456'
  var example = a_long_line_without_ending_semicolon

  (function () {
// ^^ no leading semicolon, when you see a code like this, that's an obvious error

    console.log(example)
  })(example)
}

But if you're using linters, they'll check this kind of errors for you, and it becomes just a matter of code style.

Member

rlidwka commented May 15, 2015

I thought it was still best to include them to avoid potential problems.

That's a common misconception, here is a response to it: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

Is there a coding standard for Node.js which explains how semicolons should be used.

Oh yeah, there are a lot of coding standards. And that's the problem. :)

I think I'll just explain why I moved away from using semicolons two years ago. It's easier to see potential errors this way.

I mean, if you use semicolons, one semicolon missing could go unnoticed. Because your eye is trained to treat them as end of line noise. Here is an example of an error that you could have:

function test() {
  var foo = '123';
  var bar = '456';
  var example = a_long_line_without_ending_semicolon
                                               //   ^^^ this would usually go unnoticed

  (function () {
    console.log(example); 
  })(example);
}

But if you don't use them, the semicolon at the end of the line never matters. You can't make the same error. You can of course forget to insert semicolon at the start of the line, but this error would be obvious for anybody who didn't use semicolons for a while:

function test() {
  var foo = '123'
  var bar = '456'
  var example = a_long_line_without_ending_semicolon

  (function () {
// ^^ no leading semicolon, when you see a code like this, that's an obvious error

    console.log(example)
  })(example)
}

But if you're using linters, they'll check this kind of errors for you, and it becomes just a matter of code style.

@brennanMKE

This comment has been minimized.

Show comment
Hide comment
@brennanMKE

brennanMKE May 18, 2015

@rlidwka You are suggesting that I would use the following code?

var example = a_long_line_without_ending_semicolon
;(function () {
    console.log(example); 
  })(example);

That looks weird to me.

The reason I asked is that I have been seeing semicolons used inconsistently. See line 10 in the source file linked below. In other node modules there are many more examples of very sloppy uses of semicolons. Either use them or don't. It should be consistent and should include tests to ensure the code works as intended.

Personally with JS I am inclined to just include semicolons. After years of Perl, Java, C# and Objective-C I find it easier to read code with semicolons. Swift was made to leave out semicolons which will be an adjustment for me. JS was built with semicolons in mind and there are edge cases where bugs can be caused by not having a semicolon where it needs to be. Maybe Swift will have some edge cases as well. I definitely do not want whitespace to be significant like with Python.

https://github.com/expressjs/body-parser/blob/master/test/body-parser.js

brennanMKE commented May 18, 2015

@rlidwka You are suggesting that I would use the following code?

var example = a_long_line_without_ending_semicolon
;(function () {
    console.log(example); 
  })(example);

That looks weird to me.

The reason I asked is that I have been seeing semicolons used inconsistently. See line 10 in the source file linked below. In other node modules there are many more examples of very sloppy uses of semicolons. Either use them or don't. It should be consistent and should include tests to ensure the code works as intended.

Personally with JS I am inclined to just include semicolons. After years of Perl, Java, C# and Objective-C I find it easier to read code with semicolons. Swift was made to leave out semicolons which will be an adjustment for me. JS was built with semicolons in mind and there are edge cases where bugs can be caused by not having a semicolon where it needs to be. Maybe Swift will have some edge cases as well. I definitely do not want whitespace to be significant like with Python.

https://github.com/expressjs/body-parser/blob/master/test/body-parser.js

@rlidwka

This comment has been minimized.

Show comment
Hide comment
@rlidwka

rlidwka May 18, 2015

Member

You are suggesting that I would use the following code?

No, you put two extra semicolons there. You might not notice them, if you are so accustomed to see them in every statement, but I would immediately recognize it as an error.

Either use semicolons at the end of every statement, or use it only when it's important. If you use latter style, then yes leading semi (as in ;(function ...) is the way to go.

edit: continued conversation in an email

Member

rlidwka commented May 18, 2015

You are suggesting that I would use the following code?

No, you put two extra semicolons there. You might not notice them, if you are so accustomed to see them in every statement, but I would immediately recognize it as an error.

Either use semicolons at the end of every statement, or use it only when it's important. If you use latter style, then yes leading semi (as in ;(function ...) is the way to go.

edit: continued conversation in an email

@Fishrock123

This comment has been minimized.

Show comment
Hide comment
@Fishrock123

Fishrock123 May 19, 2015

Member

See https://github.com/jshttp/style-guide/blob/master/javascript.md

It is advised to avoid starting lines with [ or (. For the former, just declare a var; the latter also has no real benefits over alternatives.

Member

Fishrock123 commented May 19, 2015

See https://github.com/jshttp/style-guide/blob/master/javascript.md

It is advised to avoid starting lines with [ or (. For the former, just declare a var; the latter also has no real benefits over alternatives.

@brennanMKE

This comment has been minimized.

Show comment
Hide comment
@brennanMKE

brennanMKE May 19, 2015

And how is a developer supposed to know when a semicolon is critically important and when it is not? It seems much safer to end each statement with a semicolon. There are edge cases where a semicolon has to be used but I don't know them since I have always used semicolons to complete each statement.

Why not just write your code in CoffeeScript if you do not like using semicolons and then use Grunt/Gulp to create JavaScript files?

brennanMKE commented May 19, 2015

And how is a developer supposed to know when a semicolon is critically important and when it is not? It seems much safer to end each statement with a semicolon. There are edge cases where a semicolon has to be used but I don't know them since I have always used semicolons to complete each statement.

Why not just write your code in CoffeeScript if you do not like using semicolons and then use Grunt/Gulp to create JavaScript files?

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson May 19, 2015

Member

I don't mean to be mean here, but can this conversation be taken somewhere else if it's not about an actual issue with this module? It seems to have just turned into a general discussion of semi colon vs no semi colon. Sadly JavaScript has been defied where semicolons are optional (see ECMA-262 section 7.9: "Automatic Semicolon Insertion") and there is nothing we can do about it except accept it's part of the language this module (and others) are written in.

Member

dougwilson commented May 19, 2015

I don't mean to be mean here, but can this conversation be taken somewhere else if it's not about an actual issue with this module? It seems to have just turned into a general discussion of semi colon vs no semi colon. Sadly JavaScript has been defied where semicolons are optional (see ECMA-262 section 7.9: "Automatic Semicolon Insertion") and there is nothing we can do about it except accept it's part of the language this module (and others) are written in.

@expressjs expressjs locked and limited conversation to collaborators May 19, 2015

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson May 19, 2015

Member

The above comment applies to everyone in this thread and is not directed at any one person.

Member

dougwilson commented May 19, 2015

The above comment applies to everyone in this thread and is not directed at any one person.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.