denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.75k stars 2.55k forks source link

Wording in "Why you should use semicolons" can be improved #213

Closed qgustavor closed 3 years ago

qgustavor commented 3 years ago

The wording in "Why you should use semicolons" section is weird. The section, introduced in 7488b34 after issue #112, was based on a Tweet and kept the almost same wording. In the other hand the section title is not consistent with the rest of text: while other titles are simple assertions, such as "true is false" or "function is not a function", or questions such as "Is this multiple inheritance?", the title of this one is an recommendation.

Also, why the "standard JavaScript" wording? It's way too similar to the JavaScript Standard Style which avoids using semicolons. I see that it comes from the Tweet, but it could be better worded to avoid this reference. In fact, if it's a reference to this style guide, then it's a bad example as this guide explicitly says would should use semicolons when starting lines with [ and lists other cases where it's needed.

I like Standard because it makes programmers think about the actual JavaScript issue: automatic semicolon insertion. Teaching "you should use semicolons" for me is just a workaround for that and which may fail if not learned properly. If someone thinks "I will just use semicolons like in other languages" then it will lead to some pitfalls like adding a line break after a return: works in PHP, works in C, not in JavaScript.

The wording can be improved. I suggest to change the title to something similar to "Semicolons are automatically added, right?" or "Are semicolons optional?" or "Automatically added semicolons", something to fit the style used in other examples. The current example that shows a situation where a semicolon was expected to be added (by someone which don't knows ASI) can be kept, but would be nice if an example where ASI adds a semicolon where it's not expected was added, maybe this one:

// Returns 4 in PHP, returns undefined in JavaScript due to ASI
function getRandomNumber () {
  return // chosen by fair dice roll.
    4; // guaranteed to be random.
}

Of course a linter would detect the early return, but there isn't a single example in the text that recommends using one.

Issue written after this comment.

denysdovhan commented 3 years ago

Please open a PR with your suggestions.