What are the best programming practices for JavaScript coding? This may be a difficult question to answer. So, let’s change the question, what coding standards are the most popular?
Sideeffect.kr came up with some interesting results by analyzing open source code hosted on GitHub. Let’s take a look.
Ending comma vs. beginning comma
End of line quotes:
Copy the code code as follows:
var foo = 1,
bar = 2,
baz = 3;
var obj = {
foo: 1,
bar: 2,
baz: 3
};
Starting quote:
Copy the code code as follows:
var foo = 1
, bar = 2
, baz = 3;
var obj = {
foo: 1
, bar: 2
, baz: 3
};
End of line, 92.345%; beginning of line, 7.655%. (Based on 1,100,251 commits.)
Space and Tab
Everyone loves using spaces these days. Using space indentation can ensure that different developers and different editor settings see the same results.
Space, 81.1%; Tab, 18.9%. (Based on 2,019,550 submissions.)
Whether to add a space after the function
no spaces
Copy the code code as follows:
function foo() {
return "bar";
}
There are spaces
Copy the code code as follows:
function foo () {
return "bar";
}
Without spaces, 67.424%; with spaces, 32.576%. (Based on 1,212,488 submissions.)
Are there spaces between parameters and parentheses?
no spaces
Copy the code code as follows:
function fn(arg1, arg2) {
//or
if (true) {
There are spaces
Copy the code code as follows:
function fn(arg1, arg2) {
// ...
}
if (true) {
// ...
}
Without spaces, 94.31%; with spaces, 5.69%. (Based on 1,514,971 submissions.)
Are there spaces around the colon in the object literal?
There is a space after the colon
Copy the code code as follows:
{
foo: 1,
bar: 2,
baz: 3
}
No space after colon
Copy the code code as follows:
{
foo:1,
bar:2,
baz:3
}
There are spaces before and after the colon
Copy the code code as follows:
{
foo : 1,
bar : 2,
baz : 3
}
Trailing space, 62.955%; no space, 22.891%; leading and trailing spaces, 14.154%. (Based on 1,300,035 submissions.)
Personally, I feel that no spaces are too crowded and not conducive to quickly distinguishing key and value. If there are spaces before and after, I'm afraid you need to align the colons to make it look beautiful. Judging from statistics, most programmers are too lazy to align colons (or is it that most programmers' IDEs or editors are not smart enough?)
conditional statement
There are spaces
Copy the code code as follows:
if (true) {
//...
}
while (true) {
//...
}
switch (v) {
//...
}
no spaces
Copy the code code as follows:
if(true) {
//...
}
while(true) {
//...
}
switch(v) {
//...
}
With spaces, 78.276%; without 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
Therefore, the most popular coding standards are:
•End of line comma
• Space indentation
•No space after function name
•No spaces between function parameters and parentheses
•In object literals, add a space after the colon, but not before the colon.
•Add a space after the conditional statement keyword
What is popular is not necessarily good (such as influenza), but from a communication perspective, writing code in a popular style can make your code look more familiar to most people.