Today's most popular JavaScript code specification _ Basics

Source: Internet
Author: User

What is the best programming specification for JavaScript code? This may be a tread problem. So, to change the question, what code specification is the most popular?

sideeffect.kr a number of interesting results by analyzing the open source code hosted on GitHub. Let's take a look.

End of line comma to beginning comma
End of line quote:

Copy Code code as follows:

var foo = 1,
Bar = 2,
Baz = 3;

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

Line header quotation marks:
Copy Code code as follows:

var foo = 1
, bar = 2
, Baz = 3;

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

End of line, 92.345%; at the beginning, 7.655%. (Based on 1,100,251 submission statistics.) )

Spaces and Tab
We all love to use space these days. Using a space indent ensures that different developers and different editor settings see the same results.

Spaces, 81.1%;tab,18.9%. (Based on 2,019,550 submission statistics.) )

Whether to add spaces after a function
No spaces

Copy Code code as follows:

function foo () {
return "Bar";
}

There are spaces
Copy Code code as follows:

function foo () {
return "Bar";
}

No spaces, 67.424%; there are spaces, 32.576%. (Based on 1,212,488 submission statistics.) )

Whether there are spaces between the arguments and the parentheses
No spaces

Copy Code code as follows:

function fn (arg1, arg2) {
Or
if (true) {

There are spaces
Copy Code code as follows:

function fn (arg1, arg2) {
// ...
}

if (true) {
// ...
}

No spaces, 94.31%; there are spaces, 5.69%. (Based on 1,514,971 submission statistics.) )

Object literal with spaces around the colon
There are spaces after the colon

Copy Code code as follows:

{
Foo:1,
Bar:2,
Baz:3
}

No spaces after a colon
Copy Code code as follows:

{
Foo:1,
Bar:2,
Baz:3
}

There are spaces before and after the colon
Copy Code code as follows:

{
Foo:1,
Bar:2,
Baz:3
}

After space, 62.955%, no spaces, 22.891, before and after space, 14.154%. (Based on 1,300,035 submission statistics.) )

Personally feel, no space is too crowded, not conducive to quickly distinguish between key and value. Before and after the space, I'm afraid the need to align Colon, look beautiful, from the statistical data, most programmers are too lazy to align colon (or, most programmers of the IDE or editor is not smart?) )

Conditional statement
There are spaces

Copy Code code as follows:

if (true) {
//...
}

while (true) {
//...
}

Switch (v) {
//...
}

No spaces
Copy Code code as follows:

if (true) {
//...
}

while (true) {
//...
}

Switch (v) {
//...
}

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

Single quotes, double quotes
Single quotes, 56.791%; double quotes, 43.209. (based on 1,705,910 submissions.) )

Summarize
So the most popular code specification is:

• End of line comma
• Space indent
• No spaces after function name
• No spaces between function arguments and parentheses
• The literal number of the colon followed by a space, not before the colon
• Conditional statement keywords followed by spaces

Popular is not necessarily good (like influenza), but from the perspective of communication, writing code in popular style can make your code seem 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.