Currently the most popular JavaScript code specification

Source: Internet
Author: User

What are the best JavaScript code programming specifications? This may be a public issue. So let's change the question. What code specifications are the most popular?

Sideeffect. kr has obtained some interesting results by analyzing the open source code hosted on GitHub. Let's take a look.

End of line, end of line, and end of line
Quotation marks at the end of the line:
Copy codeThe Code is as follows:
Var foo = 1,
Bar = 2,
Baz = 3;

Var obj = {
Foo: 1,
Bar: 2,
Baz: 3
};

Quotation marks at the beginning of the line:
Copy codeThe Code is as follows:
Var foo = 1
, Bar = 2
, Baz = 3;

Var obj = {
Foo: 1
, Bar: 2
, Baz: 3
};

Last Row, 92.345%; first row, 7.655%. (Based on 1,100,251 submissions .)

Space and Tab
This year, we all love to use spaces. Using space indentation ensures that the results are the same for different developers and editors.

Space, 81.1%; Tab, 18.9%. (Based on 2,019,550 submissions .)

Whether to add spaces after the Function
No space
Copy codeThe Code is as follows:
Function foo (){
Return "bar ";
}

Space
Copy codeThe Code is as follows:
Function foo (){
Return "bar ";
}

No space, 67.424%; space, 32.576%. (Based on 1,212,488 submissions .)

Whether there is a space between the parameter and the brackets
No space
Copy codeThe Code is as follows:
Function fn (arg1, arg2 ){
// Or
If (true ){

Space
Copy codeThe Code is as follows:
Function fn (arg1, arg2 ){
//...
}

If (true ){
//...
}

No space, 94.31%; space, 5.69%. (Based on 1,514,971 submissions .)

Whether there are spaces around the colon in the object literal
Space after colon
Copy codeThe Code is as follows:
{
Foo: 1,
Bar: 2,
Baz: 3
}

No space after colon
Copy codeThe Code is as follows:
{
Foo: 1,
Bar: 2,
Baz: 3
}

There are spaces before and after the colon
Copy codeThe Code is as follows:
{
Foo: 1,
Bar: 2,
Baz: 3
}

Rear space, 62.955%; no space, 22.891%; front space, 14.154%. (Based on 1,300,035 submissions .)

In my opinion, it is too crowded without spaces, which is not conducive to the quick distinction between key and value. If there are spaces before and after, I am afraid it is necessary to align the colon to make it look beautiful. From the statistical data, most programmers are too lazy to align the colon (or are most programmers less intelligent in IDE or editor ?)

Condition Statement
Space
Copy codeThe Code is as follows:
If (true ){
//...
}

While (true ){
//...
}

Switch (v ){
//...
}

No space
Copy codeThe Code is as follows:
If (true ){
//...
}

While (true ){
//...
}

Switch (v ){
//...
}

There are spaces, 78.276%; no spaces, 21.724%. (Based on 1,163,316 submissions .)

Single quotation marks and double quotation marks
Single quotes, 56.791%; double quotes, 43.209%. (Based on 1,705,910 submissions .)

Summary
Therefore, the most popular code specification is:

• End of line comma
• Space indent
• No space after function name
• There is no space between function parameters and parentheses
• A space is added after the colon of the object literal, and no space is added before the colon.
• A space is added after the Condition Statement keyword.

The popularity is not necessarily good (such as influenza), but from the perspective of communication, writing code according to the popular style can make your code look more accustomed to most people.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.