Editor’s note: This article was published by Mihan in Denver and reprinted with permission from Qiwu Weekly.

No matter which team, the uniform code style debate is a topic of no solution, every time the debate comes up, each has its own reasons, or persistent or extreme or crazy to beat the table angrily leave 🙂

If the team is forced to use a code style, students who meet their own standards will be happy to laugh; Do not conform to the heart may be “captive” grass mud horse dark ran knock code; The worst case scenario is when the code you’ve written has been secretly formatted when the team process tool compiles the code in a unified way, and you’re no longer thinking of a garden of weeds, or at least an animal park.

We argue for uniform code style because we all know the importance of good code style: professional, maintainable, and effective teamwork.

However, I at the different arguments and online secretly observed in various wonderful debate, some reason code style supporters seem not based on the “importance” or its view of the nature of the more and more like to affix “personality code”, “the popular language style”, “I don’t like” and so on the label, For example, during the JavaScript semicolon war, some supporters of “Semicolon less” on a certain point point:

Let’s take a look at the most popular adults from Rainy Creek in the last year or two:

There is no should or shouldn’t, only you like it or not. Just because JavaScript syntax is c-like doesn’t mean it’s essentially the same language as C, and all the intuitive “certainly should have semicolons” are conservative, unthought out, and hasty conclusions. Since then, there have been a lot of other languages that can use semicolons but no one else does: Go, Scala, Ruby, Python, Swift, Groovy…

Big V is particularly wary of publishing good and bad students without a thorough understanding of the ASI mechanism, and seems to prefer popular language styles as a reference standard.

Personally, I don’t think it is very convincing, because I can easily think of an example: In 2012, Spain’s passing-and-controlling method amazed the world football world. Does Germany, which pursues simple and practical, have to follow the passing-and-controlling method to prove that it is a very good team? Doesn’t make sense.

Let’s take a look at he Laoshi from 2012 to 2014:

[Updated January 2014] I originally planned to revise this answer because it was too long and some “semicolons” might not catch my main point. However, due to time and energy factors have not been rewritten, and from the support of this answer, the purpose of the original semicolon has been achieved, so I will not revise this answer.

Here are just the main flaws with the “always write semicolons” style promoted by the Partiers:

It’s always possible to forget to write a semicolon. ASI makes it impossible to distinguish between accidentally forgetting and intentionally not writing (code folded).

2. Always write semicolons does not completely solve ASI defects (for example, semicolons are automatically inserted after a return).

3. Whether to add a semicolon after “} “requires semantic judgment (whether it is a function expression) before the corresponding” {“, which is much more expensive than the pre-semicolon judgment (as long as the token check for the first character of the line: whether it is [(+ – / one of the five symbols).

With a very long very long space to answer, old wet this spirit is worth us to learn. However, Lao Shi seems to be magnifying the various shortcomings of the semicolon, and then give yourself reasons to make a balance, and then make a choice.

I don’t seem to accept this argument either, because in actual coding, the 1, 2, and 3 flaws he listed are not a problem if good coding habits are developed.

Other points, many of which are made as they please, such as:

I don’t add semicolons, which makes the code cleaner and less visually taxing

Is there any real point of view? Yes, of the Great Jade Uncle:

Look at the project, if the project is not added, do not add, such as Zepto

If the project is plus, add, such as jquery

The same goes for 4 Spaces and 2 Spaces. I’m used to both styles

The pragmatic answer is no. In the actual work, it should be Like this, know how to “hold life”, know how to respect, for the jade uncle point Like. Unfortunately, this is not an answer that will convince you whether to use a semicolon or not.

These debates are nothing more than a matter of style. Style must be a matter of personal preference. Discussion around style is always a war of words, with no result.

So is the “semicolon problem” really just a matter of style?

The ASI mechanism is the most immediate cause of the semicolon controversy, so the easiest way to understand why ASI happens is to look at what Brenden Eich, the inventor of JavaScript, has to say about the problem. There happened to be an article he published in 2012 on this subject, The Infernal Semicolon.

The first sentence of the article is a tough one

Most of the comments in this semicolons in JS exchange make me sad.

Most of the comments in this JS semicolon debate make me sad

The title of the argument is “bootstrap-dropdown.js clearMenus() needs; at the end,” and the code that prompts the argument is as follows:

clearMenus()

! isActive && $parent.toggleClass('open')

Copy the code

It was pointed out that the JSMin at the time was buggy and that compressing the above code would cause errors.

At this time, Creator of JSON, JSLint, JSMin and ADSafe, member of the ECMA JavaScript 2.0 standardization Committee, called JavaScript by The father of JavaScript Brendan Eich Douglas Crockford, author of JavaScript: The Good Parts, is The grandmaster of JavaScript.

That is insanely stupid code. I am not going to dumb down JSMin for this case.

TC39 is considering the use of ! as an infix operator. This code will break in the future. Fix it now. Learn to use semicolons properly. ! is not intended to be a statement separator. ; is.

This code is crazy and stupid. I am not going to lower the JSMin level for this stupid case

TC39 is considering a “! Used as the infix operator, this code will not run in the near future. Fix it and learn how to use semicolons correctly. “!” The “; “does not separate the statement. Is.

Brendan Eich politely agreed with Douglas Crockford’s answer and tough attitude, but kept his thoughts to himself. Here’s what he thought:

The original intention of ASI design was described in a number of paragraphs:

ASI is (formally speaking) a syntactic error correction procedure. If you start to code as if it were a universal significant-newline rule, you will get into trouble

ASI is a syntactic error correcting program that can be very difficult if you start coding as if it were a universal new line rule

“Syntactic error correction program”, I understand is a fault tolerance scheme, not a grammar rule! To put it another way, when I (Brendan Eich, of course) invented the language, it was grammatically clear that a sentence should end with “; “. As the end, but your coding style is really out of control, there may be a semicolon missing, in order to prevent you from forgetting to add “; “due to personal reasons in the process of coding. So I added ASI to make the code work as well as possible!

This seems to be a good intention of the author, but now it has become a controversial topic, which is not what the author hopes to see. For this phenomenon, the author shows a little regret for the design of ASI:

I wish I had made newlines more significant in JS back in those ten days in May, 1995

I wish I could have made the meaning of JS sentences more important in those 10-plus days back in May 1995 (Brendan Eich was designing JavaScript in 10 days).

However, from the perspective of language designer, the existence of ASI seems to be reasonable. The author believes that a programming language can not control the freedom of coding style, and the design of ASI can make it have more freedom:

Since when does any programming language not have syntax arguments? All living, practical languages that I know of, even those with indentation-based block structure and similar restrictions, have degrees of freedom of expression that allow abusage as well as good usage

Is there a programming language that doesn’t have syntactic arguments? All the languages I know have decent freedom of expression, even some based on indented block structures or with similar constraints

Language designers can try to reduce degrees of freedom, but not eliminate them completely.

Language designers can try to reduce freedom, but they cannot completely limit it

Finally, the author gives a clear point of view:

My two cents: Be careful not to use ASI as if it gave, JS significant newlines, And both please don ‘t abuse && And | | the where the mighty the if statement serves better.

Be careful not to use ASI, although he made the pausing to JS judgment, if powerful if statements can be normal use, please don’t abuse && and | |

From the perspective of language designer, the author also expresses his own attitude towards the control of language freedom:

I’ll also say that if it were up to me, in view of JS’s subtle and long history, I’d fix JSMin. But I would still log a grumpy comment or two first!

If the author is Douglas Crockford, the author will fix JSMin throughout the subtle and long history of JS, but if the author is Douglas Crockford’s stupid code, the author will still be the first to say something.

In our own words, let’s sort out the author’s (below me) point:

  • ASI is a syntactic error correction program, which is my attitude towards freedom of language design control

  • Unfortunately, ASI has ignited a debate over whether to add a semicolon to JS encoding style, and it saddens me to see fans overreact

  • For this I feel regret, hope oneself in 95 May more than 10 days of design time can make JS sentence meaning appear more important!

  • But I am a language designer with a good attitude. ASI gives more freedom to language design. In this sense, ASI is not wrong!

  • However, I do not recommend using ASI when it is possible to pass code normally, since ASI is designed to handle syntactic error corrections

  • I still hate stupid code that doesn’t respect the syntax of the language, but I still insist on the freedom of language design. Therefore, my dear Douglas Crockford, if I were you, I would still hate stupid code. But at the same time I will fix JSMin to show you the attitude you should have as a language designer.

OK, watching the wonderful “semicolon war” in China, I also express two views of my own:

  • You can play roughshod over others, but you have to respect the language itself.

  • Don’t be too superstitious, should have their own research and judgment;

So is the semicolon plus or minus at the end?

I

I swear to God, I

It’s up to you 🙂

About Weird Dance Weekly

Qiwu Weekly is a front-end technology community operated by qiwu Group, a professional front-end team of 360 Company. After paying attention to the public number, directly send the link to the background can contribute to us.